Skip to content

Commit

Permalink
Add shutdown support
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinegomez committed Mar 27, 2018
1 parent 1ed0061 commit f8f62d4
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 70 deletions.
130 changes: 70 additions & 60 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapiness/redis",
"version": "1.0.2",
"version": "1.1.0",
"description": "Hapiness module for redis",
"main": "commonjs/index.js",
"types": "index.d.ts",
Expand Down Expand Up @@ -77,23 +77,23 @@
"redis-commands": "^1.3.1"
},
"devDependencies": {
"@hapiness/core": "1.3.0",
"@hapiness/core": "~1.5.0",
"@types/fs-extra": "^5.0.0",
"coveralls": "^3.0.0",
"fs-extra": "^5.0.0",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^4.0.1",
"mocha-typescript": "^1.1.12",
"rimraf": "^2.6.2",
"rxjs": "^5.5.6",
"rxjs": "^5.5.7",
"ts-node": "^3.3.0",
"tslint": "^5.8.0",
"typescript": "^2.6.2",
"unit.js": "^2.0.0"
},
"peerDependencies": {
"@hapiness/core": "1.3.0",
"rxjs": "^5.5.6"
"@hapiness/core": "^1.5.0",
"rxjs": "^5.5.7"
},
"engines": {
"node": ">=7.0.0"
Expand Down
17 changes: 12 additions & 5 deletions src/module/redis.extension.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { CoreModule, Extension, ExtensionWithConfig, OnExtensionLoad } from '@hapiness/core';
import { CoreModule, Extension, ExtensionWithConfig, OnExtensionLoad, ExtensionShutdownPriority, OnShutdown } from '@hapiness/core';

import { Observable } from 'rxjs/Observable';

import { RedisConfig } from './interfaces';
import { RedisClientManager } from './managers';

export class RedisExt implements OnExtensionLoad {
export class RedisExt implements OnExtensionLoad, OnShutdown {

public static setConfig(config: RedisConfig): ExtensionWithConfig {
return {
Expand All @@ -26,11 +26,18 @@ export class RedisExt implements OnExtensionLoad {
onExtensionLoad(module: CoreModule, config: RedisConfig): Observable<Extension> {
return Observable
.of(new RedisClientManager(config))
.switchMap(_ => _.createClient().map(__ => _))
.map(_ => ({
.switchMap(redisClient => redisClient.createClient().map(() => redisClient))
.map(redisClient => ({
instance: this,
token: RedisExt,
value: _
value: redisClient
}));
}

onShutdown(module, redisClient: RedisClientManager) {
return {
priority: ExtensionShutdownPriority.NORMAL,
resolver: Observable.bindNodeCallback(redisClient.client.quit)()
};
}
}
53 changes: 53 additions & 0 deletions test/integration/shutdown.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { test, suite } from 'mocha-typescript';

import { Observable } from 'rxjs';

import { Hapiness, HapinessModule, OnStart } from '@hapiness/core';

import { FakeRedisClient, mockRedisCreateConnection } from '../mocks';
import { RedisExt, RedisModule } from '../../src';

@suite('- Integration tests of RedisModule')
export class RedisModuleIntegrationTest {

@test('- Test shutdown')
testRedisModule(done) {
const fakeInst = new FakeRedisClient();
const redisStub = mockRedisCreateConnection();
redisStub.returns(<any> fakeInst);

Observable
.of(fakeInst)
.delay(new Date(Date.now() + 1500))
.map(_ => _.emit('ready'))
.subscribe();

@HapinessModule({
version: '1.0.0',
providers: [],
imports: [RedisModule]
})
class RedisShutdownTest implements OnStart {
constructor() {}

onStart(): void {
const extension = Hapiness['extensions'].find(item => item.token === RedisExt);
extension.instance.onShutdown(<any>{}, extension.value).resolver
.do(() => {
redisStub.restore();
})
.subscribe(() => done(), err => done(err));
}
}

Hapiness.bootstrap(RedisShutdownTest, [
RedisExt.setConfig(
{
url: '//test.com',
password: 'mdp',
db: '1'
}
)
]);
}
}

0 comments on commit f8f62d4

Please sign in to comment.