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