public void TestAcquireLock_SameId() { using (MongoDbPessimisticLocker locker = new MongoDbPessimisticLocker(connectionString)) { string id = "id-to-lock"; string keeperId1 = "job1"; string keeperId2 = "job2"; Assert.True(locker.AcquireLock(id, keeperId1)); Assert.False(locker.AcquireLock(id, keeperId2)); } }
public void TestAcquireLock_SameId_Timeout_LockReleased_By_MongoDB() { int ttl = 1; using (MongoDbPessimisticLocker locker = new MongoDbPessimisticLocker(connectionString, ttl, true)) { string id = "id-to-lock"; string keeperId1 = "job1"; string keeperId2 = "job2"; Assert.True(locker.AcquireLock(id, keeperId1)); Thread.Sleep(TimeSpan.FromSeconds(ttl + 60)); // give mongodb a little bit time to remove lock Assert.True(locker.AcquireLock(id, keeperId2)); Assert.False(locker.AcquireLock(id, keeperId1)); } }
public void TestAcquireLock_SameId_Timeout_KeepAllive() { int ttl = 1; using (MongoDbPessimisticLocker locker = new MongoDbPessimisticLocker(connectionString, ttl, true)) { string id = "id-to-lock"; string keeperId1 = "job1"; string keeperId2 = "job2"; Assert.True(locker.AcquireLock(id, keeperId1)); for (int i = 0; i < 59; i++) { Thread.Sleep(1000); Assert.True(locker.AcquireLock(id, keeperId1)); } Assert.False(locker.AcquireLock(id, keeperId2)); Assert.True(locker.AcquireLock(id, keeperId1)); } }