2021-04-08 16:24:21 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2024-03-18 21:48:00 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2021-04-08 16:24:21 +00:00
|
|
|
import * as sinon from 'sinon';
|
|
|
|
|
|
|
|
import { waitForOnline } from '../../util/waitForOnline';
|
|
|
|
|
|
|
|
describe('waitForOnline', () => {
|
2021-07-23 22:02:36 +00:00
|
|
|
let sandbox: sinon.SinonSandbox;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
sandbox = sinon.createSandbox();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
sandbox.restore();
|
|
|
|
});
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
function getFakeEmitter(): EventEmitter {
|
|
|
|
const result = new EventEmitter();
|
|
|
|
sinon.stub(result, 'on');
|
|
|
|
sinon.stub(result, 'off');
|
2021-04-08 16:24:21 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
it("resolves immediately if you're online", async () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
const fakeServer = { isOnline: () => true };
|
|
|
|
const fakeEvents = getFakeEmitter();
|
2021-04-08 16:24:21 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
await waitForOnline({ server: fakeServer, events: fakeEvents });
|
2021-04-08 16:24:21 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
sinon.assert.notCalled(fakeEvents.on as sinon.SinonStub);
|
|
|
|
sinon.assert.notCalled(fakeEvents.off as sinon.SinonStub);
|
2021-04-08 16:24:21 +00:00
|
|
|
});
|
|
|
|
|
2021-07-23 22:02:36 +00:00
|
|
|
it("if you're offline, resolves as soon as you're online (and cleans up listeners)", async () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
const fakeServer = { isOnline: () => false };
|
|
|
|
const fakeEvents = getFakeEmitter();
|
2021-04-08 16:24:21 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
(fakeEvents.on as sinon.SinonStub)
|
2021-04-08 16:24:21 +00:00
|
|
|
.withArgs('online')
|
|
|
|
.callsFake((_eventName: string, callback: () => void) => {
|
|
|
|
setTimeout(callback, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
let done = false;
|
|
|
|
const promise = (async () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
await waitForOnline({ server: fakeServer, events: fakeEvents });
|
2021-04-08 16:24:21 +00:00
|
|
|
done = true;
|
|
|
|
})();
|
|
|
|
|
|
|
|
assert.isFalse(done);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
|
|
|
|
assert.isTrue(done);
|
2024-03-18 21:48:00 +00:00
|
|
|
sinon.assert.calledOnce(fakeEvents.on as sinon.SinonStub);
|
|
|
|
sinon.assert.calledOnce(fakeEvents.off as sinon.SinonStub);
|
2021-04-08 16:24:21 +00:00
|
|
|
});
|
2021-07-23 22:02:36 +00:00
|
|
|
|
|
|
|
it("resolves immediately if you're online when passed a timeout", async () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
const fakeServer = { isOnline: () => true };
|
|
|
|
const fakeEvents = getFakeEmitter();
|
2021-07-23 22:02:36 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
await waitForOnline({
|
|
|
|
server: fakeServer,
|
|
|
|
events: fakeEvents,
|
|
|
|
timeout: 1234,
|
|
|
|
});
|
2021-07-23 22:02:36 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
sinon.assert.notCalled(fakeEvents.on as sinon.SinonStub);
|
|
|
|
sinon.assert.notCalled(fakeEvents.off as sinon.SinonStub);
|
2021-07-23 22:02:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("resolves immediately if you're online even if passed a timeout of 0", async () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
const fakeServer = { isOnline: () => true };
|
|
|
|
const fakeEvents = getFakeEmitter();
|
2021-07-23 22:02:36 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
await waitForOnline({ server: fakeServer, events: fakeEvents, timeout: 0 });
|
2021-07-23 22:02:36 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
sinon.assert.notCalled(fakeEvents.on as sinon.SinonStub);
|
|
|
|
sinon.assert.notCalled(fakeEvents.off as sinon.SinonStub);
|
2021-07-23 22:02:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("if you're offline, resolves as soon as you're online if it happens before the timeout", async () => {
|
|
|
|
const clock = sandbox.useFakeTimers();
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
const fakeServer = { isOnline: () => false };
|
|
|
|
const fakeEvents = getFakeEmitter();
|
2021-07-23 22:02:36 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
(fakeEvents.on as sinon.SinonStub)
|
2021-07-23 22:02:36 +00:00
|
|
|
.withArgs('online')
|
|
|
|
.callsFake((_eventName: string, callback: () => void) => {
|
|
|
|
setTimeout(callback, 1000);
|
|
|
|
});
|
|
|
|
|
|
|
|
let done = false;
|
2022-12-21 18:41:48 +00:00
|
|
|
void (async () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
await waitForOnline({
|
|
|
|
server: fakeServer,
|
|
|
|
events: fakeEvents,
|
|
|
|
timeout: 9999,
|
|
|
|
});
|
2021-07-23 22:02:36 +00:00
|
|
|
done = true;
|
|
|
|
})();
|
|
|
|
|
|
|
|
await clock.tickAsync(600);
|
|
|
|
assert.isFalse(done);
|
|
|
|
|
|
|
|
await clock.tickAsync(500);
|
|
|
|
|
|
|
|
assert.isTrue(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects if too much time has passed, and cleans up listeners', async () => {
|
|
|
|
const clock = sandbox.useFakeTimers();
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
const fakeServer = { isOnline: () => false };
|
|
|
|
const fakeEvents = getFakeEmitter();
|
2021-07-23 22:02:36 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
(fakeEvents.on as sinon.SinonStub)
|
2021-07-23 22:02:36 +00:00
|
|
|
.withArgs('online')
|
|
|
|
.callsFake((_eventName: string, callback: () => void) => {
|
|
|
|
setTimeout(callback, 9999);
|
|
|
|
});
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
const promise = waitForOnline({
|
|
|
|
server: fakeServer,
|
|
|
|
events: fakeEvents,
|
2021-07-23 22:02:36 +00:00
|
|
|
timeout: 100,
|
|
|
|
});
|
|
|
|
|
|
|
|
await clock.tickAsync(500);
|
|
|
|
|
|
|
|
await assert.isRejected(promise);
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
sinon.assert.calledOnce(fakeEvents.off as sinon.SinonStub);
|
2021-07-23 22:02:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects if offline and passed a timeout of 0', async () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
const fakeServer = { isOnline: () => false };
|
|
|
|
const fakeEvents = getFakeEmitter();
|
2021-07-23 22:02:36 +00:00
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
(fakeEvents.on as sinon.SinonStub)
|
2021-07-23 22:02:36 +00:00
|
|
|
.withArgs('online')
|
|
|
|
.callsFake((_eventName: string, callback: () => void) => {
|
|
|
|
setTimeout(callback, 9999);
|
|
|
|
});
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
const promise = waitForOnline({
|
|
|
|
server: fakeServer,
|
|
|
|
events: fakeEvents,
|
|
|
|
timeout: 100,
|
|
|
|
});
|
2021-07-23 22:02:36 +00:00
|
|
|
|
|
|
|
await assert.isRejected(promise);
|
|
|
|
});
|
2021-04-08 16:24:21 +00:00
|
|
|
});
|