/** * @author Martin Karkowski * @email m.karkowski@zema.de * @create date 2022-01-04 10:03:41 * @modify date 2022-01-04 12:38:45 * @desc [description] */ 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 { NopeConnectivityManager } from "./ConnectivityManager"; describe("NopeConnectivityManager", function () { // Describe the required Test: let first = new NopeConnectivityManager( { communicator: getLayer("event", "", false), logger: false, }, () => new NopeObservable(), "first" ); describe("Configuration", function () { let communicator = getLayer("event", "", false); beforeEach((done) => { first.dispose(true); communicator = getLayer("event", "", false); // Create a new Observer first = new NopeConnectivityManager( { communicator, logger: false, }, () => new NopeObservable(), "test" ); first.ready.waitFor().then(() => done()); }); it("adapting timings", async () => { // Remove the Old Timer first.setTimings({ checkInterval: 10, dead: 25, remove: 30, sendAliveInterval: 5, slow: 15, warn: 20, }); first.dispose(); }); it("master-flag", async () => { // Remove the Old Timer expect(first.isMaster, "Expecting to be the master"); first.isMaster = true; expect(first.isMaster, "Expecting to be the master"); first.isMaster = false; expect(!first.isMaster, "Expecting to be the master"); first.dispose(); }); }); describe("RpcManager Communication", function () { let communicator = getLayer("event", "", false); beforeEach((done) => { first.dispose(true); communicator = getLayer("event", "", false); // Create a new Observer first = new NopeConnectivityManager( { communicator, logger: false, }, () => new NopeObservable(), "test" ); first.ready.waitFor().then(() => done()); }); it("new detection", (done) => { let sub = null; sub = first.dispatchers.onChange.subscribe((changes) => { if (changes.added.length >= 1) { done(); first.dispose(true); second.dispose(true); sub.unsubscribe(); } else { done(new Error("Not found")); first.dispose(true); second.dispose(true); sub.unsubscribe(); } }); const second = new NopeConnectivityManager( { communicator, logger: false, timeouts: { checkInterval: 10, dead: 25, remove: 30, sendAliveInterval: 5, slow: 15, warn: 20, }, }, () => new NopeObservable(), "second" ); }); it("removed detection", (done) => { let sub = null; let firstCall = true; sub = first.dispatchers.onChange.subscribe((changes) => { if (firstCall) { firstCall = false; } else { if (changes.removed.length >= 1) { done(); first.dispose(true); second.dispose(true); sub.unsubscribe(); } else { done(new Error("removing has not been detected")); first.dispose(true); second.dispose(true); sub.unsubscribe(); } } }); const second = new NopeConnectivityManager( { communicator, logger: false, timeouts: { checkInterval: 10, dead: 25, remove: 30, sendAliveInterval: 5, slow: 15, warn: 20, }, }, () => new NopeObservable(), "second" ); }); it("synchronizing time", async () => { // Remove the Old Timer first.dispose(true); first = new NopeConnectivityManager( { communicator, logger: false, timeouts: { checkInterval: 10, dead: 25, remove: 30, sendAliveInterval: 5, slow: 15, warn: 20, }, }, () => new NopeObservable(), "second" ); const timestamp = Date.now(); let start = Date.now(); await sleep(30); let end = Date.now(); // We have waited something like 100 ms (+-) // thats our delay. Now if we use that delay, // we are able sync our time. first.syncTime(timestamp, end - start); // Get the Adapted Timestamp. const adapted = first.info.timestamp; end = Date.now(); // Dispose our Delay. first.dispose(true); expect(end - adapted).to.be.equal(0, "There should not be an delta."); }); }); });