improvement to map (#266) and (#265)

* added AbstractClosable

* added Jvm.init()

* added disable.thread.safety

* ignore thread safety check

* disabled threadSafetyCheck for chronicle map

* removed init()

* changed isOpen() to be !isClosed()

* throwExceptionIfClosed() now creates a new ChronicleHashClosedException()

* added  test testRemainingAutoResizesAfterClose

Co-authored-by: Rob Austin <rob.austin@boundedbuffer.co.uk>
This commit is contained in:
Rob Austin
2021-01-27 18:23:07 +00:00
committed by GitHub
co-authored by Rob Austin
parent f921c40819
commit addfc1481f
4 changed files with 49 additions and 14 deletions
@@ -16,8 +16,10 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.hash.Data;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Test;
import java.util.Map;
@@ -27,6 +29,7 @@ import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.BiFunction;
import static net.openhft.chronicle.core.Jvm.*;
import static net.openhft.chronicle.hash.Data.bytesEquivalent;
import static net.openhft.chronicle.map.BiMapTest.DualLockSuccess.FAIL;
import static net.openhft.chronicle.map.BiMapTest.DualLockSuccess.SUCCESS;
@@ -25,13 +25,14 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import static net.openhft.chronicle.hash.impl.BigSegmentHeader.LOCK_TIMEOUT_SECONDS;
import static net.openhft.chronicle.map.ChronicleMap.of;
public class MapCloseTest {
@Test(expected = IllegalStateException.class)
@Test
public void closeInContextTest() {
ChronicleMap<Integer, Integer> map =
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
of(Integer.class, Integer.class).entries(1).create();
ExternalMapQueryContext<Integer, Integer, ?> cxt = map.queryContext(1);
map.close();
}
@@ -39,7 +40,7 @@ public class MapCloseTest {
@Test(expected = ChronicleHashClosedException.class)
public void testGetAfterCloseThrowsChronicleHashClosedException() throws InterruptedException {
ChronicleMap<Integer, Integer> map =
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
of(Integer.class, Integer.class).entries(1).create();
Thread t = new Thread(() -> map.close());
t.start();
t.join();
@@ -50,7 +51,7 @@ public class MapCloseTest {
public void testIterationAfterCloseThrowsChronicleHashClosedException()
throws InterruptedException {
ChronicleMap<Integer, Integer> map =
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
of(Integer.class, Integer.class).entries(1).create();
Thread t = new Thread(() -> map.close());
t.start();
t.join();
@@ -62,18 +63,18 @@ public class MapCloseTest {
public void testSizeAfterCloseThrowsChronicleHashClosedException()
throws InterruptedException {
ChronicleMap<Integer, Integer> map =
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
of(Integer.class, Integer.class).entries(1).create();
Thread t = new Thread(() -> map.close());
t.start();
t.join();
map.size();
}
@Test(expected = RuntimeException.class)
@Test
public void closeWithContextInAnotherThreadTest() throws InterruptedException {
LOCK_TIMEOUT_SECONDS = 2;
ChronicleMap<Integer, Integer> map =
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
of(Integer.class, Integer.class).entries(1).create();
Object lock = new Object();
CountDownLatch latch = new CountDownLatch(1);
synchronized (lock) {
@@ -93,11 +94,19 @@ public class MapCloseTest {
LOCK_TIMEOUT_SECONDS = 60;
}
@Test(expected = ChronicleHashClosedException.class)
public void testRemainingAutoResizesAfterClose() {
ChronicleMap<Integer, Integer> map = of(Integer.class, Integer.class).entries(1).create();
map.close();
map.remainingAutoResizes();
}
@Test
public void vanillaChronicleHashAllContextsExpungeTest() throws InterruptedException {
VanillaChronicleMap<Integer, Integer, Void> map =
(VanillaChronicleMap<Integer, Integer, Void>)
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
of(Integer.class, Integer.class).entries(1).create();
Semaphore semaphore = new Semaphore(0);
CountDownLatch latch = new CountDownLatch(2);
class MapAccessThread extends Thread {