Update SnapshottableCollectionLockTest.java

This commit is contained in:
Andrea Cavalli 2021-01-24 03:15:14 +01:00
parent 27ea0deaed
commit 9950a4ceb1

View File

@ -1,6 +1,7 @@
package org.warp.commonutils.locks;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.jupiter.api.Assertions;
@ -58,20 +59,22 @@ public class SnapshottableCollectionLockTest {
private void checkSituation(SnapshottableCollectionLock<Long> lock, boolean fullReadAllow, boolean fullWriteAllow, boolean dataReadAllow, boolean dataWriteAllow) {
var pool = Executors.newWorkStealingPool();
for (FullLockType readFullType : FullLockType.READ_FULL_TYPES) {
if (fullReadAllow) {
Assertions.assertDoesNotThrow(() -> {
CompletableFuture.runAsync(() -> {
lock.fullLock(null, readFullType);
lock.fullUnlock(null, readFullType);
}).get(1, TimeUnit.SECONDS);
}, pool).get(2, TimeUnit.SECONDS);
});
} else {
Assertions.assertThrows(TimeoutException.class, () -> {
CompletableFuture.runAsync(() -> {
lock.fullLock(null, readFullType);
lock.fullUnlock(null, readFullType);
}).get(50, TimeUnit.MILLISECONDS);
}, pool).get(50, TimeUnit.MILLISECONDS);
});
}
}
@ -82,14 +85,14 @@ public class SnapshottableCollectionLockTest {
CompletableFuture.runAsync(() -> {
lock.fullLock(null, writeFullType);
lock.fullUnlock(null, writeFullType);
}).get(1, TimeUnit.SECONDS);
}, pool).get(2, TimeUnit.SECONDS);
});
} else {
Assertions.assertThrows(TimeoutException.class, () -> {
CompletableFuture.runAsync(() -> {
lock.fullLock(null, writeFullType);
lock.fullUnlock(null, writeFullType);
}).get(50, TimeUnit.MILLISECONDS);
}, pool).get(50, TimeUnit.MILLISECONDS);
});
}
}
@ -99,14 +102,14 @@ public class SnapshottableCollectionLockTest {
CompletableFuture.runAsync(() -> {
lock.dataLock(null, DataLockType.READ, 1L);
lock.dataUnlock(null, DataLockType.READ, 1L);
}).get(1, TimeUnit.SECONDS);
}, pool).get(2, TimeUnit.SECONDS);
});
} else {
Assertions.assertThrows(TimeoutException.class, () -> {
CompletableFuture.runAsync(() -> {
lock.dataLock(null, DataLockType.READ, 1L);
lock.dataUnlock(null, DataLockType.READ, 1L);
}).get(50, TimeUnit.MILLISECONDS);
}, pool).get(50, TimeUnit.MILLISECONDS);
});
}
@ -115,14 +118,14 @@ public class SnapshottableCollectionLockTest {
CompletableFuture.runAsync(() -> {
lock.dataLock(null, DataLockType.WRITE, 1L);
lock.dataUnlock(null, DataLockType.WRITE, 1L);
}).get(1, TimeUnit.SECONDS);
}, pool).get(2, TimeUnit.SECONDS);
});
} else {
Assertions.assertThrows(TimeoutException.class, () -> {
CompletableFuture.runAsync(() -> {
lock.dataLock(null, DataLockType.WRITE, 1L);
lock.dataUnlock(null, DataLockType.WRITE, 1L);
}).get(50, TimeUnit.MILLISECONDS);
}, pool).get(50, TimeUnit.MILLISECONDS);
});
}
}