@@ -29,8 +29,9 @@ import {
29
29
} from "@matrix-org/matrix-sdk-crypto-wasm" ;
30
30
31
31
import { TypedEventEmitter } from "../../../src/models/typed-event-emitter" ;
32
- import { HttpApiEvent , HttpApiEventHandlerMap , MatrixHttpApi , UIAuthCallback } from "../../../src" ;
32
+ import { HttpApiEvent , HttpApiEventHandlerMap , IHttpOpts , MatrixHttpApi , UIAuthCallback } from "../../../src" ;
33
33
import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor" ;
34
+ import { defer } from "../../../src/utils" ;
34
35
35
36
describe ( "OutgoingRequestProcessor" , ( ) => {
36
37
/** the OutgoingRequestProcessor implementation under test */
@@ -218,4 +219,40 @@ describe("OutgoingRequestProcessor", () => {
218
219
await Promise . all ( [ processor . makeOutgoingRequest ( outgoingRequest ) , markSentCallPromise ] ) ;
219
220
expect ( olmMachine . markRequestAsSent ) . toHaveBeenCalledWith ( "5678" , 987 , "" ) ;
220
221
} ) ;
222
+
223
+ it ( "does not explode if the OlmMachine is stopped while the request is in flight" , async ( ) => {
224
+ // we use a real olm machine for this test
225
+ const olmMachine = await RustSdkCryptoJs . OlmMachine . initialize (
226
+ new RustSdkCryptoJs . UserId ( "@alice:example.com" ) ,
227
+ new RustSdkCryptoJs . DeviceId ( "TEST_DEVICE" ) ,
228
+ ) ;
229
+
230
+ const authRequestResultDefer = defer < string > ( ) ;
231
+
232
+ const authRequestCalledPromise = new Promise < void > ( ( resolve ) => {
233
+ const mockHttpApi = {
234
+ authedRequest : async ( ) => {
235
+ resolve ( ) ;
236
+ return await authRequestResultDefer . promise ;
237
+ } ,
238
+ } as unknown as Mocked < MatrixHttpApi < IHttpOpts & { onlyData : true } > > ;
239
+ processor = new OutgoingRequestProcessor ( olmMachine , mockHttpApi ) ;
240
+ } ) ;
241
+
242
+ // build a request
243
+ const request = olmMachine . queryKeysForUsers ( [ new RustSdkCryptoJs . UserId ( "@bob:example.com" ) ] ) ;
244
+ const result = processor . makeOutgoingRequest ( request ) ;
245
+
246
+ // wait for the HTTP request to be made
247
+ await authRequestCalledPromise ;
248
+
249
+ // while the HTTP request is in flight, the OlmMachine gets stopped.
250
+ olmMachine . close ( ) ;
251
+
252
+ // the HTTP request completes...
253
+ authRequestResultDefer . resolve ( "{}" ) ;
254
+
255
+ // ... and `makeOutgoingRequest` resolves satisfactorily
256
+ await result ;
257
+ } ) ;
221
258
} ) ;
0 commit comments