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