nope/lib/dispatcher/ConnectivityManager/ConnectivityManager.spec.ts

203 lines
5.0 KiB
TypeScript
Raw Normal View History

2022-01-04 11:40:40 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2022-01-04 10:03:41
* @modify date 2022-01-04 12:38:45
2022-01-04 11:40:40 +00:00
* @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";
2022-01-05 17:14:54 +00:00
import { NopeConnectivityManager } from "./ConnectivityManager";
2022-01-04 11:40:40 +00:00
describe("NopeConnectivityManager", function () {
2022-01-04 11:40:40 +00:00
// Describe the required Test:
let first = new NopeConnectivityManager(
2022-01-04 11:40:40 +00:00
{
communicator: getLayer("event", "", false),
logger: false,
},
() => new NopeObservable(),
"first"
);
describe("Configuration", function () {
let communicator = getLayer("event", "", false);
2022-01-10 06:52:05 +00:00
beforeEach((done) => {
first.dispose(true);
communicator = getLayer("event", "", false);
// Create a new Observer
first = new NopeConnectivityManager(
{
communicator,
logger: false,
},
() => new NopeObservable(),
"test"
);
2022-01-10 06:52:05 +00:00
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("NopeConnectivityManager Communication", function () {
2022-01-04 11:40:40 +00:00
let communicator = getLayer("event", "", false);
2022-01-10 06:52:05 +00:00
beforeEach((done) => {
2022-01-04 11:40:40 +00:00
first.dispose(true);
communicator = getLayer("event", "", false);
// Create a new Observer
first = new NopeConnectivityManager(
2022-01-04 11:40:40 +00:00
{
communicator,
logger: false,
},
() => new NopeObservable(),
"test"
);
2022-01-10 06:52:05 +00:00
first.ready.waitFor().then(() => done());
2022-01-04 11:40:40 +00:00
});
it("new detection", (done) => {
let sub = null;
sub = first.dispatchers.onChange.subscribe((changes) => {
2022-01-04 11:40:40 +00:00
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(
2022-01-04 11:40:40 +00:00
{
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;
// We want our first dispatcher to detect the loss ==>
// We adapt the time. That our second dispatcher will be
// more less directly offline.
first.setTimings({
checkInterval: 10,
dead: 25,
remove: 30,
sendAliveInterval: 5,
slow: 15,
warn: 20,
});
sub = first.dispatchers.onChange.subscribe((changes) => {
2022-01-04 11:40:40 +00:00
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(
2022-01-04 11:40:40 +00:00
{
communicator,
logger: false,
},
() => new NopeObservable(),
"second"
);
});
it("synchronizing time", async () => {
// Remove the Old Timer
first.dispose(true);
first = new NopeConnectivityManager(
2022-01-04 11:40:40 +00:00
{
communicator,
},
() => new NopeObservable(),
"first"
2022-01-04 11:40:40 +00:00
);
const timestamp = first.now;
2022-01-04 11:40:40 +00:00
// Now we want to simulate an delay.
2022-01-04 11:40:40 +00:00
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.");
});
});
});