Skip to content

Commit

Permalink
Merge pull request #19 from nestjs-community/fix/module-destroy
Browse files Browse the repository at this point in the history
Fix/module destroy
  • Loading branch information
bashleigh authored Sep 26, 2018
2 parents 6192680 + cf42544 commit 17df9c1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
11 changes: 8 additions & 3 deletions src/__tests__/amqp.inject.decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ describe('InjectAmqpConnection', () => {
providers: [TestProvider],
}).compile();

const app = module.createNestApplication();
await app.init();

const provider = module.get(TestProvider);

expect(provider.getConnection()).toBeInstanceOf(ChannelModel);
provider.getConnection().close();
await app.close();
});

it('Connection should inject with name', async () => {
Expand Down Expand Up @@ -64,11 +67,13 @@ describe('InjectAmqpConnection', () => {
providers: [TestProvider],
}).compile();

const app = module.createNestApplication();
await app.init();

const provider = module.get(TestProvider);

expect(provider.getConnection()).toBeInstanceOf(ChannelModel);
expect(provider.getConnection0()).toBeInstanceOf(ChannelModel);
provider.getConnection().close();
provider.getConnection0().close();
await app.close();
});
});
47 changes: 31 additions & 16 deletions src/__tests__/amqp.module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,41 @@ describe('AmqpModule', () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
AmqpModule.forRoot({
name: 'instance',
hostname: process.env.HOST,
retrys: 1,
}),
],
}).compile();

const app = module.createNestApplication();
await app.init();

const amqpModule = module.get(AmqpModule);

expect(amqpModule).toBeInstanceOf(AmqpModule);

module.get(createConnectionToken('default')).close();
await app.close();
});

it('Instace Amqp Connection provider', async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
AmqpModule.forRoot({
name: 'first',
hostname: process.env.HOST,
retrys: 1,
}),
],
}).compile();

const amqpConnection = module.get(createConnectionToken('default'));
const app = module.createNestApplication();
await app.init();

expect(amqpConnection).toBeInstanceOf(ChannelModel);
const amqpConnection = module.get(createConnectionToken('first'));

amqpConnection.close();
expect(amqpConnection).toBeInstanceOf(ChannelModel);
await app.close();
});

it('Multiple connection options', async () => {
Expand All @@ -59,22 +66,23 @@ describe('AmqpModule', () => {
],
}).compile();

const app = module.createNestApplication();
await app.init();

const amqpConnectionTest = module.get(createConnectionToken('test'));
const amqpConnection1 = module.get(createConnectionToken('1'));

expect(amqpConnectionTest).toBeInstanceOf(ChannelModel);
expect(amqpConnection1).toBeInstanceOf(ChannelModel);

amqpConnection1.close();
amqpConnectionTest.close();
await app.close();
});

it('Connection options', async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
AmqpModule.forRoot({
hostname: process.env.HOST,
name: 'test',
name: 'test2',
port: 5673,
username: 'user',
password: 'pass',
Expand All @@ -83,11 +91,13 @@ describe('AmqpModule', () => {
],
}).compile();

const amqpConnectionTest = module.get(createConnectionToken('test'));
const app = module.createNestApplication();
await app.init();

expect(amqpConnectionTest).toBeInstanceOf(ChannelModel);
const amqpConnectionTest = module.get(createConnectionToken('test2'));

amqpConnectionTest.close();
expect(amqpConnectionTest).toBeInstanceOf(ChannelModel);
await app.close();
});

it('Connection available in submodule', async () => {
Expand All @@ -99,20 +109,23 @@ describe('AmqpModule', () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
AmqpModule.forRoot({
name: 'subModule',
hostname: process.env.HOST,
retrys: 1,
}),
SubModule,
],
}).compile();

const app = module.createNestApplication();
await app.init();

const provider = module
.select<SubModule>(SubModule)
.get(createConnectionToken('default'));
.get(createConnectionToken('subModule'));

expect(provider).toBeInstanceOf(ChannelModel);

provider.close();
await app.close();
});

it('Connections should build with AmqpAsyncOptionsInterface', async () => {
Expand All @@ -137,10 +150,12 @@ describe('AmqpModule', () => {
providers: [TestProvider],
}).compile();

const app = module.createNestApplication();
await app.init();

const provider = module.get(TestProvider);

expect(provider.getAmqp()).toBeInstanceOf(ChannelModel);

provider.getAmqp().close();
await app.close();
});
});
18 changes: 16 additions & 2 deletions src/amqp.module.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import {Module, DynamicModule, Provider} from '@nestjs/common';
import {Module, DynamicModule, Provider, OnModuleDestroy} from '@nestjs/common';
import { AmqpOptionsInterface, AmqpAsyncOptionsInterface, AmqpOptionsObjectInterface } from './interfaces';
import { createConnectionToken, createOptionsToken } from './utils/create.tokens';
import {from} from 'rxjs';
import * as amqp from 'amqplib';
import retry from './utils/retry';
import { AMQP_OPTIONS_PROVIDER, } from './amqp.constants';
import { ModuleRef } from '@nestjs/core';

@Module({})
export default class AmqpModule {
export default class AmqpModule implements OnModuleDestroy {
private static connectionNames: string[] = [];

constructor(private readonly moduleRef: ModuleRef) {}

public static forRoot(options: AmqpOptionsInterface | AmqpOptionsInterface[]): DynamicModule {

Expand Down Expand Up @@ -39,6 +43,8 @@ export default class AmqpModule {
}

public static forRootAsync(options: AmqpAsyncOptionsInterface): DynamicModule {

AmqpModule.connectionNames.push(createConnectionToken('default'));

const connectionProviders = [
{
Expand Down Expand Up @@ -78,6 +84,7 @@ export default class AmqpModule {
}

private static createConnectionProvider(options: AmqpOptionsInterface): Provider {
AmqpModule.connectionNames.push(createConnectionToken(options.name));
return {
provide: createConnectionToken(options.name),
//TODO resolve host url: do I need to? Seems to work aready? Just verify
Expand All @@ -101,4 +108,11 @@ export default class AmqpModule {

return options;
}

async onModuleDestroy(): Promise<void> {
AmqpModule.connectionNames.forEach(async connectionName => {
const connection = this.moduleRef.get<amqp.Channel>(connectionName);
connection !== null && await connection.close();
});
}
}

0 comments on commit 17df9c1

Please sign in to comment.