nope/test/testPackageLoader.ts

136 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-11-07 00:45:20 +00:00
import { assert } from "console";
import "reflect-metadata";
import { promisify } from "util";
import { EventLayer } from "../lib/communication/eventLayer";
import { getDispatcher } from "../lib/dispatcher/getDispatcher";
import { nopeDispatcher } from "../lib/dispatcher/nopeDispatcher";
import { getPackageLoader } from "../lib/loader/getPackageLoader";
import { getNopeLogger } from "../lib/logger/getLogger";
import { NopeGenericModule } from "../lib/module/GenericModule";
import { NopeObservable } from "../lib/observables/nopeObservable";
import { INopePromise } from "../lib/types/nope/nopePromise.interface";
import { IHelloWorlModule } from "./modules/IHelloWorldModule";
import helloworld from './modules/TestPackage';
const sleep = promisify(setTimeout)
const communicator = new EventLayer('individual','individual');
2020-11-07 00:45:20 +00:00
const loader = getPackageLoader(
getDispatcher({
communicator,
logger: getNopeLogger('dispatcher-srv', 'debug')
})
);
const remote_01 = new nopeDispatcher({ communicator, logger: getNopeLogger('dispatcher-r-01', 'debug') }, () => new NopeObservable());
const remote_02 = new nopeDispatcher({ communicator, logger: getNopeLogger('dispatcher-r-02', 'debug') }, () => new NopeObservable());
const main = async () => {
// Add the Package
await loader.addPackage(helloworld);
console.log('HERE')
2020-11-07 00:45:20 +00:00
// Generate the Instances
await loader.generateInstances();
console.log('Generated Instances');
await sleep(100);
console.log('SLEEPED')
2020-11-07 00:45:20 +00:00
remote_01.registerInternalInstanceGenerator('helloworld', async (dispather, description) => {
const mod = new NopeGenericModule(dispather, () => new NopeObservable());
await mod.fromDescription(description, 'overwrite');
await mod.init();
console.log('Created HelloWorldModule-Wrapper with name', description.identifier);
return mod;
});
remote_02.registerInternalInstanceGenerator('helloworld', async (dispather, description) => {
const mod = new NopeGenericModule(dispather, () => new NopeObservable());
await mod.fromDescription(description, 'overwrite');
await mod.init();
console.log('Created HelloWorldModule-Wrapper with name', description.identifier);
return mod;
});
const instance_01 = await remote_01.generateInstance<IHelloWorlModule>({
identifier: 'instance_01',
params: ['hello', 'world'],
type: 'helloworld',
});
const copy_of_instance_01 = await remote_01.generateInstance<IHelloWorlModule>({
identifier: 'instance_01',
params: ['hello', 'world'],
type: 'helloworld',
});
// await instance_01.init();
console.log('Updating Prop')
instance_01.testProp.setContent('hello prop #1');
instance_01.testProp.setContent('hello prop #2');
await sleep(100);
console.log('Calling helloWorld')
const greetings = await instance_01.helloWorld('You!');
// Print the Greetings
console.log('Result of helloWorld:',greetings);
instance_01.testProp.subscribe((value, sender) => {
console.log('Local update of the property', value)
})
console.log('Got Update by subscribing');
console.log('Updating Variable by updateTestProp Function');
await instance_01.updateTestProp();
console.log('Creating new instance on other remote');
const instance_02 = await remote_02.generateInstance<NopeGenericModule & IHelloWorlModule>({
identifier: 'instance_01',
params: ['hello', 'world'],
type: 'helloworld',
});
console.log('Delte Copies etc');
remote_01.deleteInstance(instance_01);
remote_01.deleteInstance(copy_of_instance_01);
console.log('Delete Instance 2')
// Quick Test of manual Cancelation.
try {
const promise = instance_02.sleep(5000);
if ((promise as INopePromise<void>).cancel){
(promise as INopePromise<void>).cancel('Just for Fun');
}
await promise;
} catch(e){
// The Expected Error should be :'Just for Fun'
assert(e == 'Just for Fun')
}
// Quick Test of manual timeout.
try {
const promise = instance_02.dynamicInstanceMethodsWithOptions.sleep({
timeout: 1000
},5000);
await promise;
} catch(e){
console.log('Expect an Timeout Error',e);
}
remote_02.deleteInstance(instance_02);
await sleep(100);
}
main().catch(console.error);