Skip to content

Commit

Permalink
Release 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hisorange committed Dec 3, 2022
1 parent fed9b7c commit db40355
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 433 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hisorange/kernel",
"version": "2.0.0",
"version": "1.4.0",
"description": "Async application kernel with context and modular loading support",
"main": "build/index.js",
"repository": "https://github.com/hisorange/kernel",
Expand Down
45 changes: 33 additions & 12 deletions src/kernel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,38 @@ describe('Kernel', () => {
expect(() => new Kernel()).not.toThrow();
});

test('should create a logger', () => {
test('should create a logger', async () => {
const kernel = new Kernel();

expect(kernel.logger).toBeDefined();
expect(kernel.logger).toHaveProperty('debug');
expect(kernel.logger).toHaveProperty('info');
expect(kernel.logger).toHaveProperty('warn');
expect(kernel.logger).toHaveProperty('error');

await kernel.stop();
});

test('should create a context', () => {
test('should create a context', async () => {
const kernel = new Kernel();

expect(kernel.context).toBeDefined();
expect(kernel.context).toBeInstanceOf(Context);

await kernel.stop();
});

test('should define the "' + KERNEL_BINDING + '" binding', () => {
test('should define the "' + KERNEL_BINDING + '" binding', async () => {
const kernel = new Kernel();

expect(kernel.context.contains(KERNEL_BINDING)).toBe(true);
expect(kernel.context.getSync(KERNEL_BINDING)).toBe(kernel);

await kernel.stop();
});

describe('Module Registration', () => {
test('should register module providers', () => {
test('should register module providers', async () => {
const kernel = new Kernel();

@Service()
Expand All @@ -54,9 +60,11 @@ describe('Kernel', () => {

expect(kernel.context.contains(MyService.name)).toBe(true);
expect(kernel.context.getSync(MyService.name)).toBeInstanceOf(MyService);

await kernel.stop();
});

test('should inject dependencies', () => {
test('should inject dependencies', async () => {
const kernel = new Kernel();

@Service()
Expand All @@ -82,6 +90,8 @@ describe('Kernel', () => {
expect(
kernel.context.getSync<ServiceB>(ServiceB.name).serviceA,
).toBeInstanceOf(ServiceA);

await kernel.stop();
});
});

Expand All @@ -93,12 +103,14 @@ describe('Kernel', () => {
class ModuleWithOnBootHook implements IModule {
async onBoot(injectedKernel: IKernel): Promise<void> {
expect(injectedKernel).toBe(kernel);
done();
}
}

kernel.register([ModuleWithOnBootHook]);
kernel.boostrap();
kernel
.boostrap()
.then(() => kernel.stop())
.then(() => done());
});

test('should call the "onStart" hook', done => {
Expand All @@ -108,12 +120,15 @@ describe('Kernel', () => {
class ModuleWithOnStartHook implements IModule {
async onStart(injectedKernel: IKernel): Promise<void> {
expect(injectedKernel).toBe(kernel);
done();
}
}

kernel.register([ModuleWithOnStartHook]);
kernel.boostrap().then(() => kernel.start());
kernel
.boostrap()
.then(() => kernel.start())
.then(() => kernel.stop())
.then(() => done());
});

test('should call the "onStop" hook', done => {
Expand All @@ -123,15 +138,15 @@ describe('Kernel', () => {
class ModuleWithOnStopHook implements IModule {
async onStop(injectedKernel: IKernel): Promise<void> {
expect(injectedKernel).toBe(kernel);
done();
}
}

kernel.register([ModuleWithOnStopHook]);
kernel
.boostrap()
.then(() => kernel.start())
.then(() => kernel.stop());
.then(() => kernel.stop())
.then(() => done());
});
});

Expand Down Expand Up @@ -180,11 +195,13 @@ describe('Kernel', () => {
expect(myClass.myService).toBeInstanceOf(MyService);
expect(myClass.one).toBe(1);
expect(myClass.two).toBe(2);

await kernel.stop();
});
});

describe('Providers', () => {
test('should resolve a provider by value', () => {
test('should resolve a provider by value', async () => {
const kernel = new Kernel();

class TheProduct {}
Expand Down Expand Up @@ -212,6 +229,8 @@ describe('Kernel', () => {
const resolve2 = kernel.context.getSync(TheProduct.name);

expect(resolve1).toBe(resolve2);

await kernel.stop();
});
});

Expand Down Expand Up @@ -241,6 +260,8 @@ describe('Kernel', () => {
expect(product.logger).toHaveProperty('info');
expect(product.logger).toHaveProperty('warn');
expect(product.logger).toHaveProperty('error');

await kernel.stop();
});
});

Expand Down
15 changes: 10 additions & 5 deletions src/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,14 @@ export class Kernel implements IKernel {
this.context.bind(KERNEL_BINDING).to(this);

this.logger.info('Context is ready!');

this.registerSignalHandlers();
}

/**
* Hook to allow the kernel to register the signals it wants to listen to.
*/
protected registerSignalHandlers(): void {
process.on('SIGINT', this.handleShutdown.bind(this));
process.on('SIGTERM', this.handleShutdown.bind(this));
process.once('SIGINT', this.handleShutdown.bind(this));
process.once('SIGTERM', this.handleShutdown.bind(this));
}

/**
Expand Down Expand Up @@ -248,6 +246,8 @@ export class Kernel implements IKernel {
* @inheritdoc
*/
async boostrap(): Promise<void> {
this.registerSignalHandlers();

this.logger.debug('Boostrap request received');
this.logger.debug('Invoking the module bootstrap sequence...');

Expand Down Expand Up @@ -434,7 +434,12 @@ export class Kernel implements IKernel {
* @inheritdoc
*/
create<T>(concrete: Constructor<T>, params?: any[]): ValueOrPromise<T> {
return instantiateClass(concrete, this.context, undefined, params);
return instantiateClass(
concrete as Constructor<object>,
this.context,
undefined,
params,
) as ValueOrPromise<T>;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/signal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Kernel } from './kernel';

describe.skip('Signal management', () => {
test('should register SIG handlers', async () => {
const sigIntListeners = process.listenerCount('SIGINT');
const sigTermListeners = process.listenerCount('SIGTERM');

const kernel = new Kernel();
await kernel.boostrap();

expect(process.listenerCount('SIGINT')).toBeGreaterThan(sigIntListeners);
expect(process.listenerCount('SIGTERM')).toBeGreaterThan(
sigTermListeners + 1,
);

await kernel.stop();

expect(process.listenerCount('SIGINT')).toBe(sigIntListeners);
expect(process.listenerCount('SIGTERM')).toBe(sigTermListeners);
});
});
Loading

0 comments on commit db40355

Please sign in to comment.