Skip to content

Commit

Permalink
Release 1.0.0-alpha.7.2 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan committed Mar 20, 2018
1 parent d74d94a commit 62b6e75
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 93 deletions.
4 changes: 2 additions & 2 deletions src/core/app.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Constructor,
AppConstructor,
} from '../interfaces';
import {OnixRPC, Injector} from '../core';
import {Injector} from '../core';
import {getObjectMethods} from '../utils';
/**
* @class AppFactory
Expand Down Expand Up @@ -39,7 +39,7 @@ export class AppFactory {
*/
constructor(private Class: AppConstructor, private config: IAppConfig) {
// First of all create a new class instance
if (!this.app) this.app = new this.Class(new OnixRPC(this.Class));
if (!this.app) this.app = new this.Class();
// Now setup its modules
this.setupModules();
// Once finished send the schema back
Expand Down
8 changes: 5 additions & 3 deletions src/core/app.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ export class AppServer {
apps.map(
(name: string) =>
new Promise<boolean>(async (resolve, reject) => {
const result: boolean = await this.factory.app.rpc
.topic(`${name}.isAlive`)
.call();
const result: boolean = await this.responser.process(<ICall>{
uuid: '1',
rpc: `${name}.isAlive`,
request: {metadata: {}, payload: {}},
});
resolve(result);
}),
),
Expand Down
16 changes: 8 additions & 8 deletions src/core/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IApp, OnixRPC, IAppConfig, IModuleDirectory} from '../index';
import {IApp, IAppConfig, IModuleDirectory} from '../index';
/**
* @class App
* @author Jonathan Casarrubias
Expand Down Expand Up @@ -27,14 +27,14 @@ export class Application implements IApp {
* Receives optional configurations as parameter.
* Otherwise will use platform default configuration.
*/
constructor(public rpc: OnixRPC) {}
constructor() {}
/**
* @method start
* @description Mock start method, this might be replaced
* by custom App level functionality
*/
async start(): Promise<null> {
return new Promise<null>((resolve, reject) => {
async start(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
Object.keys(this.modules).forEach((moduleName: string) => {
Object.keys(this.modules[moduleName]).forEach(
(componentName: string) => {
Expand All @@ -46,16 +46,16 @@ export class Application implements IApp {
},
);
});
resolve();
resolve(true);
});
}
/**
* @method stop
* @description Mock stop method, this might be replaced
* by custom App level functionality
*/
async stop(): Promise<null> {
return new Promise<null>((resolve, reject) => {
async stop(): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
Object.keys(this.modules).forEach((moduleName: string) => {
Object.keys(this.modules[moduleName]).forEach(
(componentName: string) => {
Expand All @@ -68,7 +68,7 @@ export class Application implements IApp {
},
);
});
resolve();
resolve(true);
});
}
}
4 changes: 3 additions & 1 deletion src/core/call.streamer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export class CallStreamer {
const segments: string[] = message.rpc.split('.');
// Component level method, RPC Exposed
if (segments.length !== 4) {
return handler(new Error(`RPC Call is invalid ${message.rpc}`));
return handler(
new Error(`OnixJS Error: RPC Call is invalid "${message.rpc}"`),
);
}
// Execute main hook, might be app/system or module level.
this.factory.app.modules[segments[1]][segments[2]][segments[3]](handler);
Expand Down
3 changes: 0 additions & 3 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export * from './app';
export * from './rpc';
//xport * from './roles';
//export * from './acl.rule';
export * from './injector';
export * from './lifecycle';
export * from './connection';
Expand Down
70 changes: 0 additions & 70 deletions src/core/rpc.ts

This file was deleted.

8 changes: 3 additions & 5 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ChildProcess} from 'child_process';
import {OnixRPC} from '../index';
import * as http from 'http';
/**
* @interface IAppConfig
Expand All @@ -20,7 +19,7 @@ export interface IAppConfig extends DomainConfig {
* that receives a OnixRPC class.
*/
export interface AppConstructor {
new (rpc: OnixRPC): IApp;
new (): IApp;
}
/**
* @interface IModuleConfig
Expand Down Expand Up @@ -111,9 +110,8 @@ export interface IInjectable {
*/
export interface IApp {
modules: IModuleDirectory;
rpc: OnixRPC;
start(): Promise<null>;
stop(): Promise<null>;
start(): Promise<boolean>;
stop(): Promise<boolean>;
isAlive(): boolean;
}
/**
Expand Down
166 changes: 165 additions & 1 deletion test/core.unit.ts → test/onixjs.core.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import {
isJsonString,
OperationType,
IAppOperation,
Component,
} from '../src';
import * as path from 'path';
import {CallResponser} from '../src/core/call.responser';
import * as WebSocket from 'uws';
import {CallStreamer} from '../src/core/call.streamer';
// Test AppFactory
test('Core: AppFactory creates an Application.', async t => {
class MyApp extends Application {}
Expand All @@ -23,7 +25,6 @@ test('Core: AppFactory creates an Application.', async t => {
t.truthy(instance.app.stop);
t.truthy(instance.app.isAlive);
t.truthy(instance.app.modules);
t.truthy(instance.app.rpc);
});
// Test AppFactory
test('Core: AppFactory fails on installing invalid module.', async t => {
Expand Down Expand Up @@ -165,7 +166,149 @@ test('Core: CallResponser invalid call.', async t => {
'OnixJS Error: RPC Call is invalid "MyApp.MyModule.MyComponent.NotExistingMethod"',
);
});
// Test CallResponser invalid call
test('Core: CallResponser invalid call.', async t => {
class MyComponent {
@RPC()
testRPC() {}
@Stream()
testSTREAM() {}
}
@Module({
models: [],
services: [],
components: [MyComponent],
})
class MyModule {}
class MyApp extends Application {}
const factory: AppFactory = new AppFactory(MyApp, {
disableNetwork: true,
modules: [MyModule],
});
const responser: CallResponser = new CallResponser(factory, MyApp);
const error = await t.throws(
responser.process({
uuid: '1',
rpc: 'MyApp.MyModule.MyComponent.NotExistingMethod',
request: <IRequest>{},
}),
);
t.is(
error.message,
'OnixJS Error: RPC Call is invalid "MyApp.MyModule.MyComponent.NotExistingMethod"',
);
});
// Test CallResponser Hooks
test('Core: CallResponser Hooks.', async t => {
@Component({
lifecycle: async function(app, metadata, method) {
const methodResult = await method();
return methodResult;
},
})
class MyComponent {
@RPC()
test(payload) {
return payload;
}
}
@Module({
models: [],
services: [],
components: [MyComponent],
})
class MyModule {}
class MyApp extends Application {}
const factory: AppFactory = new AppFactory(MyApp, {
disableNetwork: true,
modules: [MyModule],
});
const responser: CallResponser = new CallResponser(factory, MyApp);
const result = await responser.process({
uuid: '1',
rpc: 'MyApp.MyModule.MyComponent.test',
request: <IRequest>{
metadata: {stream: false},
payload: {
text: 'Hello Responser',
},
},
});
t.is(result.text, 'Hello Responser');
});

// Test CallResponser Hooks
test('Core: CallResponser Hooks.', async t => {
@Component({
lifecycle: async function(app, metadata, method) {
const methodResult = await method();
return methodResult;
},
})
class MyComponent {
@Stream()
test(stream) {
return stream({
text: 'Hello Streamer',
});
}
}
@Module({
models: [],
services: [],
components: [MyComponent],
})
class MyModule {}
class MyApp extends Application {}
const factory: AppFactory = new AppFactory(MyApp, {
disableNetwork: true,
modules: [MyModule],
});
const streamer: CallStreamer = new CallStreamer(factory, MyApp);
streamer.register(
{
uuid: '1',
rpc: 'MyApp.MyModule.MyComponent.test',
request: <IRequest>{
metadata: {stream: false},
payload: {},
},
},
result => {
t.is(result.text, 'Hello Streamer');
},
);
});

// Test CallStreamer invalid call
test('Core: CallStreamer invalid call.', async t => {
class MyComponent {}
@Module({
models: [],
services: [],
components: [MyComponent],
})
class MyModule {}
class MyApp extends Application {}
const factory: AppFactory = new AppFactory(MyApp, {
disableNetwork: true,
modules: [MyModule],
});
const streamer: CallStreamer = new CallStreamer(factory, MyApp);
streamer.register(
{
uuid: '1',
rpc: 'MyApp.MyModule.MyComponent.invalid.method.rpc.call',
request: <IRequest>{},
},
result => {
t.is(
result.message,
'OnixJS Error: RPC Call is invalid "MyApp.MyModule.MyComponent.invalid.method.rpc.call"',
);
},
);
});
// Test AppServer invalid operation
test('Core: AppServer invalid operation.', async t => {
class MyApp extends Application {}
Expand Down Expand Up @@ -201,3 +344,24 @@ test('Core: AppServer invalid operation.', async t => {
}
});
});
// Test Application start and stop
test('Core: Application start and stop.', async t => {
class MyComponent {
init() {}
destroy() {}
}
@Module({
models: [],
services: [],
components: [MyComponent],
})
class MyModule {}
class MyApp extends Application {}
const appInstance: MyApp = new MyApp();
appInstance.modules[MyModule.name] = new MyModule();
appInstance.modules[MyModule.name][MyComponent.name] = new MyComponent();
const startResult: boolean = await appInstance.start();
const stopResult: boolean = await appInstance.stop();
t.true(startResult);
t.true(stopResult);
});

0 comments on commit 62b6e75

Please sign in to comment.