Skip to content

Commit 8194629

Browse files
committed
use lolex to fake the timer
1 parent 9e29289 commit 8194629

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

spec/unit/crypto.spec.js

+39-26
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import TestClient from '../TestClient';
1212
import {MatrixEvent} from '../../lib/models/event';
1313
import Room from '../../lib/models/room';
1414
import olmlib from '../../lib/crypto/olmlib';
15+
import lolex from 'lolex';
1516

1617
const EventEmitter = require("events").EventEmitter;
1718

@@ -127,7 +128,6 @@ describe("Crypto", function() {
127128
describe('Key requests', function() {
128129
let aliceClient;
129130
let bobClient;
130-
let realSetTimeout;
131131

132132
beforeEach(async function() {
133133
aliceClient = (new TestClient(
@@ -138,15 +138,9 @@ describe("Crypto", function() {
138138
)).client;
139139
await aliceClient.initCrypto();
140140
await bobClient.initCrypto();
141-
// clobber the setTimeout function to run 10x faster.
142-
realSetTimeout = global.setTimeout;
143-
global.setTimeout = function(f, n) {
144-
return realSetTimeout(f, n/10);
145-
};
146141
});
147142

148143
afterEach(async function() {
149-
global.setTimeout = realSetTimeout;
150144
aliceClient.stopClient();
151145
bobClient.stopClient();
152146
});
@@ -322,32 +316,51 @@ describe("Crypto", function() {
322316
let func;
323317
const promise = new Promise((resolve, reject) => {
324318
func = function(...args) {
325-
global.setTimeout(() => resolve(args), 10);
326-
return Promise.resolve();
319+
resolve(args);
320+
return new Promise((resolve, reject) => {
321+
// give us some time to process the result before
322+
// continuing
323+
global.setTimeout(resolve, 1);
324+
});
327325
};
328326
});
329327
return {func, promise};
330328
}
331329

332330
aliceClient.startClient();
333331

334-
let promise;
335-
// make a room key request, and record the transaction ID for the
336-
// sendToDevice call
337-
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
338-
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
339-
let args = await promise;
340-
const txnId = args[2];
341-
342-
// cancel and resend the room key request
343-
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
344-
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
345-
// the first call to sendToDevice will be the cancellation
346-
args = await promise;
347-
// the second call to sendToDevice will be the key request
348-
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
349-
args = await promise;
350-
expect(args[2]).toNotBe(txnId);
332+
const clock = lolex.install();
333+
334+
try {
335+
let promise;
336+
// make a room key request, and record the transaction ID for the
337+
// sendToDevice call
338+
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
339+
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
340+
clock.runToLast();
341+
let args = await promise;
342+
const txnId = args[2];
343+
clock.runToLast();
344+
345+
// give the room key request manager time to update the state
346+
// of the request
347+
await Promise.resolve();
348+
349+
// cancel and resend the room key request
350+
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
351+
await aliceClient.cancelAndResendEventRoomKeyRequest(event);
352+
clock.runToLast();
353+
// the first call to sendToDevice will be the cancellation
354+
args = await promise;
355+
// the second call to sendToDevice will be the key request
356+
({promise, func: aliceClient.sendToDevice} = awaitFunctionCall());
357+
clock.runToLast();
358+
args = await promise;
359+
clock.runToLast();
360+
expect(args[2]).toNotBe(txnId);
361+
} finally {
362+
clock.uninstall();
363+
}
351364
});
352365
});
353366
});

0 commit comments

Comments
 (0)