nope/lib/dispatcher/RpcManager/NopeRpcManager.spec.ts
2022-01-04 12:42:44 +01:00

212 lines
5.0 KiB
TypeScript

import { expect } from "chai";
import { beforeEach, describe, it } from "mocha";
import "reflect-metadata";
import { getLayer } from "../communication/getLayer.nodejs";
import { sleep } from "../helpers/async";
import { NopeObservable } from "../observables/nopeObservable";
import { NopeRpcManager } from "./NopeRpcManager";
describe("NopeRpcManager", function () {
// Describe the required Test:
let manager = new NopeRpcManager(
{
communicator: getLayer("event", "", false),
logger: false,
},
() => new NopeObservable(),
async () => "test",
"test"
);
describe("serviceHandeling", function () {
beforeEach(() => {
// Create a new Observer
manager = new NopeRpcManager(
{
communicator: getLayer("event", "", false),
logger: false,
},
() => new NopeObservable(),
async () => "test",
"test"
);
});
const helloWorld = async (greetings: string) => {
return "Hello " + greetings + "!";
};
const delay = async (greetings: string) => {
await sleep(1000);
return "Hello " + greetings + "!";
};
it("registering service", async () => {
await manager.ready.waitFor();
const r = manager.registerService(helloWorld);
await manager.services.onChange.waitForUpdate();
});
it("call service", async () => {
await manager.ready.waitFor();
const r = manager.registerService(helloWorld, {
id: "helloworld",
});
await sleep(10);
const result = await manager.performCall("helloworld", ["Mocha"]);
expect(result).to.equal("Hello Mocha!", "result is not matching");
});
it("call service via methodInterface", async () => {
await manager.ready.waitFor();
const r = manager.registerService(helloWorld, {
id: "helloworld",
});
await sleep(10);
const result = await manager.methodInterface.helloworld("Mocha");
expect(result).to.equal("Hello Mocha!", "result is not matching");
});
it("call service with a timeout", async () => {
await manager.ready.waitFor();
const r = manager.registerService(delay, {
id: "helloworld",
});
await sleep(10);
const err = Error("Error not thrown");
try {
const result = await manager.methodInterfaceWithOptions.helloworld(
{
timeout: 50,
},
"Mocha"
);
throw err;
} catch (e) {
if (e == err) {
throw err;
}
}
});
});
describe("RpcManager Communication", function () {
let caller = new NopeRpcManager(
{
communicator: getLayer("event", "", false),
logger: false,
},
() => new NopeObservable(),
async () => "test",
"test"
);
beforeEach(() => {
const communicator = getLayer("event", "", false);
// Create a new Observer
manager = new NopeRpcManager(
{
communicator,
logger: false,
},
() => new NopeObservable(),
async () => "test",
"test"
);
caller = new NopeRpcManager(
{
communicator,
logger: false,
},
() => new NopeObservable(),
async () => "test",
"test"
);
});
const helloWorld = async (greetings: string) => {
return "Hello " + greetings + "!";
};
const delay = async (greetings: string) => {
await sleep(1000);
return "Hello " + greetings + "!";
};
it("registering service", async () => {
await manager.ready.waitFor();
await caller.ready.waitFor();
const r = manager.registerService(helloWorld);
await manager.services.onChange.waitForUpdate();
});
it("call service", async () => {
await manager.ready.waitFor();
await caller.ready.waitFor();
const r = manager.registerService(helloWorld, {
id: "helloworld",
});
await sleep(10);
const result = await caller.performCall("helloworld", ["Mocha"]);
expect(result).to.equal("Hello Mocha!", "result is not matching");
});
it("call service via methodInterface", async () => {
await manager.ready.waitFor();
await caller.ready.waitFor();
const r = manager.registerService(helloWorld, {
id: "helloworld",
});
await sleep(10);
const result = await caller.methodInterface.helloworld("Mocha");
expect(result).to.equal("Hello Mocha!", "result is not matching");
});
it("call service with a timeout", async () => {
await manager.ready.waitFor();
await caller.ready.waitFor();
const r = manager.registerService(delay, {
id: "helloworld",
});
await sleep(10);
const err = Error("Error not thrown");
try {
const result = await caller.methodInterfaceWithOptions.helloworld(
{
timeout: 50,
},
"Mocha"
);
throw err;
} catch (e) {
if (e == err) {
throw err;
}
}
});
});
});