@@ -12,6 +12,7 @@ import TestClient from '../TestClient';
12
12
import { MatrixEvent } from '../../lib/models/event' ;
13
13
import Room from '../../lib/models/room' ;
14
14
import olmlib from '../../lib/crypto/olmlib' ;
15
+ import lolex from 'lolex' ;
15
16
16
17
const EventEmitter = require ( "events" ) . EventEmitter ;
17
18
@@ -127,7 +128,6 @@ describe("Crypto", function() {
127
128
describe ( 'Key requests' , function ( ) {
128
129
let aliceClient ;
129
130
let bobClient ;
130
- let realSetTimeout ;
131
131
132
132
beforeEach ( async function ( ) {
133
133
aliceClient = ( new TestClient (
@@ -138,15 +138,9 @@ describe("Crypto", function() {
138
138
) ) . client ;
139
139
await aliceClient . initCrypto ( ) ;
140
140
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
- } ;
146
141
} ) ;
147
142
148
143
afterEach ( async function ( ) {
149
- global . setTimeout = realSetTimeout ;
150
144
aliceClient . stopClient ( ) ;
151
145
bobClient . stopClient ( ) ;
152
146
} ) ;
@@ -322,32 +316,51 @@ describe("Crypto", function() {
322
316
let func ;
323
317
const promise = new Promise ( ( resolve , reject ) => {
324
318
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
+ } ) ;
327
325
} ;
328
326
} ) ;
329
327
return { func, promise} ;
330
328
}
331
329
332
330
aliceClient . startClient ( ) ;
333
331
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
+ }
351
364
} ) ;
352
365
} ) ;
353
366
} ) ;
0 commit comments