Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "node"
- 9
script:
- yarn run test
after_script:
Expand Down
1,326 changes: 668 additions & 658 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hapiness/core",
"version": "1.5.1",
"version": "1.5.2",
"description": "Project to have a HapiJS (https://hapijs.com/) based framework to create easier NodeJS back-end with some awesome features",
"main": "commonjs/index.js",
"types": "index.d.ts",
Expand Down
31 changes: 26 additions & 5 deletions src/core/hapiness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,21 @@ export class Hapiness {
.ignoreElements()
.subscribe(
null,
_ => reject(_),
_ => {
this.logger.debug(`bootstrap error catched [${_.message}]`);
this
.shutdown()
.subscribe(
() => reject(_),
err => {
this.logger.debug(`bootstrap error catched [${err.message}], shutting down extensions...`);
reject(err);
process.exit(1);
}
);
},
() => resolve()
)
);
});
}

Expand Down Expand Up @@ -88,7 +100,7 @@ export class Hapiness {
*/
private static getShutdownHooks(): Observable<ExtensionShutdown[]> {
return Observable
.from(this.extensions)
.from([].concat(this.extensions).filter(e => !!e))
.filter(_ => !!_ && HookManager
.hasLifecycleHook(
ExtentionHooksEnum.OnShutdown.toString(),
Expand Down Expand Up @@ -119,13 +131,12 @@ export class Hapiness {
return Observable
.from([].concat(extensions).filter(_ => !!_))
.map(_ => this.toExtensionWithConfig(_))
.flatMap(_ => this
.concatMap(_ => this
.loadExtention(_, moduleResolved)
.timeout(options.extensionTimeout || this.defaultTimeout)
.catch(err => Observable.throw(extensionError(err, _.token.name)))
)
.toArray()
.do(_ => this.extensions = _)
.flatMap(_ => this.instantiateModule(_, moduleResolved, options));
}

Expand Down Expand Up @@ -256,6 +267,16 @@ export class Hapiness {
instance,
[ module, extension.config ]
)
.catch(_ => {
this.extensions = [].concat(this.extensions, instance);
return this
.shutdown()
.flatMap(() => Observable.throw(_));
})
)
.do(_ => this.extensions = []
.concat(this.extensions, _)
.filter(__ => !!__)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/extensions/socket-server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class WebSocketServer {
*/
public stop(): Observable<boolean> {
return Observable
.from(this.getSockets())
.from([].concat(this.getSockets()).filter(_ => !!_))
.do(_ => _.close())
.toArray()
.flatMap(_ => Observable
Expand Down
60 changes: 59 additions & 1 deletion test/integration/core.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { suite, test } from 'mocha-typescript';
import { Hapiness, HapinessModule, OnStart, OnRegister, Lib, Injectable } from '../../src/core';
import { Hapiness, HapinessModule, OnStart, OnRegister, Lib, Injectable, ExtensionShutdownPriority } from '../../src/core';
import * as unit from 'unit.js';
import { Observable } from 'rxjs';

Expand Down Expand Up @@ -204,6 +204,64 @@ export class ModuleTestSuite {
.object(_)
.isInstanceOf(Error)
.hasProperty('message', '[TestExtension] Timeout has occurred');

done();
});

}

@test('Bootstrap - Extension timeout call onShutdown on first extension')
testBootstrap8(done) {

let enterred = false;

class TestExtension1 {
onExtensionLoad() {
return Observable.of({
value: 'test',
instance: this,
token: TestExtension1
});
}

onShutdown() {
enterred = true;
return {
priority: ExtensionShutdownPriority.NORMAL,
resolver: Observable.of(true)
};
}
}

class TestExtension2 {
onExtensionLoad() {
return Observable
.of('')
.delay(500);
}
}

@HapinessModule({
version: ''
})
class Module1 implements OnStart {

onStart() {
throw new Error('Oops');
}

}

Hapiness
.bootstrap(Module1, [ TestExtension1, TestExtension2 ], { extensionTimeout: 100 })
.catch(_ => {
unit
.object(_)
.isInstanceOf(Error)
.hasProperty('message', '[TestExtension2] Timeout has occurred');

unit.bool(enterred).isTrue();

done();
});

Expand Down
11 changes: 8 additions & 3 deletions test/unit/hapiness.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class TestSuite {
.withArgs(EmptyModule)
.returns(Observable.throw(new Error('Oops')));

const stub2 = unit
.stub(Hapiness, 'shutdown')
.returns(Observable.of(false));

Hapiness
.bootstrap(EmptyModule)
.catch(_ => {
Expand All @@ -50,7 +54,11 @@ export class TestSuite {
.isInstanceOf(Error)
.hasProperty('message', 'Oops');

unit.number(stub2.callCount).is(1);

stub1.parent.restore();
stub2.restore();

done();
});
}
Expand Down Expand Up @@ -143,9 +151,6 @@ export class TestSuite {
.returns(Observable.of(module))
.withArgs('onRegister', EmptyModule2, getModulesRes[1].instance)
.returns(Observable.of(module));
// stub3
// .withArgs('onStart', EmptyModule, getModulesRes[0].instance, null, false)
// .returns(Observable.of(null));

Hapiness['callRegister'](module)
.subscribe();
Expand Down
Loading