@@ -233,3 +233,79 @@ macro_rules! event_cache_store_integration_tests {
233
233
}
234
234
} ;
235
235
}
236
+
237
+ /// Macro generating tests for the event cache store, related to time (mostly
238
+ /// for the cross-process lock).
239
+ #[ allow( unused_macros) ]
240
+ #[ macro_export]
241
+ macro_rules! event_cache_store_integration_tests_time {
242
+ ( ) => {
243
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
244
+ mod event_cache_store_integration_tests_time {
245
+ use std:: time:: Duration ;
246
+
247
+ use matrix_sdk_test:: async_test;
248
+ use $crate:: event_cache_store:: IntoEventCacheStore ;
249
+
250
+ use super :: get_event_cache_store;
251
+
252
+ #[ async_test]
253
+ async fn test_lease_locks( ) {
254
+ let store = get_event_cache_store( ) . await . unwrap( ) . into_event_cache_store( ) ;
255
+
256
+ let acquired0 = store. try_take_leased_lock( 0 , "key" , "alice" ) . await . unwrap( ) ;
257
+ assert!( acquired0) ;
258
+
259
+ // Should extend the lease automatically (same holder).
260
+ let acquired2 = store. try_take_leased_lock( 300 , "key" , "alice" ) . await . unwrap( ) ;
261
+ assert!( acquired2) ;
262
+
263
+ // Should extend the lease automatically (same holder + time is ok).
264
+ let acquired3 = store. try_take_leased_lock( 300 , "key" , "alice" ) . await . unwrap( ) ;
265
+ assert!( acquired3) ;
266
+
267
+ // Another attempt at taking the lock should fail, because it's taken.
268
+ let acquired4 = store. try_take_leased_lock( 300 , "key" , "bob" ) . await . unwrap( ) ;
269
+ assert!( !acquired4) ;
270
+
271
+ // Even if we insist.
272
+ let acquired5 = store. try_take_leased_lock( 300 , "key" , "bob" ) . await . unwrap( ) ;
273
+ assert!( !acquired5) ;
274
+
275
+ // That's a nice test we got here, go take a little nap.
276
+ tokio:: time:: sleep( Duration :: from_millis( 50 ) ) . await ;
277
+
278
+ // Still too early.
279
+ let acquired55 = store. try_take_leased_lock( 300 , "key" , "bob" ) . await . unwrap( ) ;
280
+ assert!( !acquired55) ;
281
+
282
+ // Ok you can take another nap then.
283
+ tokio:: time:: sleep( Duration :: from_millis( 250 ) ) . await ;
284
+
285
+ // At some point, we do get the lock.
286
+ let acquired6 = store. try_take_leased_lock( 0 , "key" , "bob" ) . await . unwrap( ) ;
287
+ assert!( acquired6) ;
288
+
289
+ tokio:: time:: sleep( Duration :: from_millis( 1 ) ) . await ;
290
+
291
+ // The other gets it almost immediately too.
292
+ let acquired7 = store. try_take_leased_lock( 0 , "key" , "alice" ) . await . unwrap( ) ;
293
+ assert!( acquired7) ;
294
+
295
+ tokio:: time:: sleep( Duration :: from_millis( 1 ) ) . await ;
296
+
297
+ // But when we take a longer lease...
298
+ let acquired8 = store. try_take_leased_lock( 300 , "key" , "bob" ) . await . unwrap( ) ;
299
+ assert!( acquired8) ;
300
+
301
+ // It blocks the other user.
302
+ let acquired9 = store. try_take_leased_lock( 300 , "key" , "alice" ) . await . unwrap( ) ;
303
+ assert!( !acquired9) ;
304
+
305
+ // We can hold onto our lease.
306
+ let acquired10 = store. try_take_leased_lock( 300 , "key" , "bob" ) . await . unwrap( ) ;
307
+ assert!( acquired10) ;
308
+ }
309
+ }
310
+ } ;
311
+ }
0 commit comments