diff --git a/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java b/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java index b95bd5a01..7f34f1f1b 100644 --- a/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/AbstractChannelReplicator.java @@ -55,7 +55,6 @@ abstract class AbstractChannelReplicator implements Closeable { public static final int SIZE_OF_SIZE = 4; public static final int SIZE_OF_TRANSACTION_ID = 8; - public static final int SIZE_OF_TIME_SHIFT = 2; private static final Logger LOG = LoggerFactory.getLogger(AbstractChannelReplicator.class); @@ -67,6 +66,15 @@ abstract class AbstractChannelReplicator implements Closeable { final CloseablesManager closeables = new CloseablesManager(); final Selector selector = openSelector(closeables); private final ExecutorService executorService; + + private final ExecutorService connectExecutorService = Executors.newCachedThreadPool( + new NamedThreadFactory("TCP-connect", true) { + @Override + public Thread newThread(@net.openhft.lang.model.constraints.NotNull Runnable r) { + return lastThread = super.newThread(r); + } + }); + private volatile Thread lastThread; private Throwable startedHere; private Future future; @@ -228,10 +236,31 @@ abstract class AbstractChannelReplicator implements Closeable { @Override public void close() { + if (Thread.interrupted()) LOG.warn("Already interrupted"); long start = System.currentTimeMillis(); - closeResources(); + isClosed = true; + connectExecutorService.shutdown(); + try { + if (!connectExecutorService.awaitTermination(5, TimeUnit.MILLISECONDS)) { + connectExecutorService.shutdownNow(); + connectExecutorService.awaitTermination(10, TimeUnit.SECONDS); + } + } catch (InterruptedException e) { + LOG.error("", e); + } + + try { + closeResources(); + if (!executorService.awaitTermination(5, TimeUnit.MILLISECONDS)) { + executorService.shutdownNow(); + executorService.awaitTermination(10, TimeUnit.SECONDS); + } + } catch (InterruptedException e) { + LOG.error("", e); + } + try { if (future != null) @@ -261,18 +290,6 @@ abstract class AbstractChannelReplicator implements Closeable { isClosed = true; executorService.shutdown(); - - try { - executorService.awaitTermination(5, TimeUnit.MILLISECONDS); - } catch (Exception e) { - executorService.shutdownNow(); - try { - executorService.awaitTermination(10, TimeUnit.SECONDS); - } catch (InterruptedException e1) { - LOG.error("", e); - } - } - closeables.closeQuietly(); if (segmentState != null) segmentState.close(); @@ -593,50 +610,53 @@ abstract class AbstractChannelReplicator implements Closeable { * @param reconnectionInterval the period to wait before connecting */ private void doConnect(final long reconnectionInterval) { - final Thread thread = new Thread(new Runnable() { - public void run() { - SelectableChannel socketChannel = null; - try { - if (reconnectionInterval > 0) - Thread.sleep(reconnectionInterval); - - socketChannel = doConnect(); - + try { + connectExecutorService.submit(new Runnable() { + public void run() { + SelectableChannel socketChannel = null; try { - closeables.add(socketChannel); - } catch (IllegalStateException e) { - // close could have be called from another thread, while we were in Thread.sleep() - // which would cause a IllegalStateException + if (reconnectionInterval > 0) + Thread.sleep(reconnectionInterval); + + socketChannel = doConnect(); + + try { + closeables.add(socketChannel); + } catch (IllegalStateException e) { + // close could have be called from another thread, while we were in Thread.sleep() + // which would cause a IllegalStateException + closeQuietly(socketChannel); + return; + } + + AbstractConnector.this.socketChannel = socketChannel; + } catch (Exception e) { closeQuietly(socketChannel); - return; + LOG.debug("", e); } - - AbstractConnector.this.socketChannel = socketChannel; - } catch (Exception e) { - closeQuietly(socketChannel); - LOG.debug("", e); } - } - - private void closeQuietly(SelectableChannel socketChannel) { - if (socketChannel == null) - return; - try { - socketChannel.close(); - } catch (Exception e1) { - // do nothing - } - } - }); - - thread.setName(name); - thread.setDaemon(true); - thread.start(); + }); + } catch (Exception e) { + if (!isClosed) + LOG.error("", e); + } } + private void closeQuietly(SelectableChannel socketChannel) { + if (socketChannel == null) + return; + try { + socketChannel.close(); + } catch (Exception e1) { + // do nothing + } + } + + public void setSuccessfullyConnected() { connectionAttempts = 0; } + } } diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index 6c2946ee7..a44519791 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -103,7 +103,7 @@ public final class ChronicleMapBuilder implements Cloneable, ChronicleHashBuilder, ChronicleMapBuilder>, MapBuilder>, Serializable { - static final byte UDP_REPLICATION_MODIFICATION_ITERATOR_ID = (byte) 128; + static final byte UDP_REPLICATION_MODIFICATION_ITERATOR_ID = (byte) 127; private static final int DEFAULT_KEY_OR_VALUE_SIZE = 120; private static final long DEFAULT_ENTRIES = 1 << 20; private static final int MAX_SEGMENTS = (1 << 30); diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 28c67936d..b033d5f8d 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -29,6 +29,7 @@ import net.openhft.chronicle.hash.serialization.internal.MetaBytesWriter; import net.openhft.lang.Maths; import net.openhft.lang.collection.ATSDirectBitSet; import net.openhft.lang.io.Bytes; +import net.openhft.lang.io.CheckedBytes; import net.openhft.lang.io.MultiStoreBytes; import net.openhft.lang.io.NativeBytesI; import net.openhft.lang.threadlocal.ThreadLocalCopies; @@ -188,7 +189,7 @@ final class ReplicatedChronicleMap implements ChronicleMap, Closeable, Clon private final InetSocketAddress remoteAddress; private final long timeoutMs; private final int tcpBufferSize; - + private Class kClass; private Class vClass; private boolean putReturnsNull; @@ -376,7 +377,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon // read a single byte back while (this.connectionOutBuffer.position() <= 0) { int read = clientChannel.read(this.connectionOutBuffer);// the remote identifier - if (read ==-1) + if (read == -1) throw new IOException("server conncetion closed"); checkTimeout(timeoutTime); } @@ -1133,6 +1134,9 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon try { return blockingFetchThrowable(timeoutTime, transactionId); + } catch (AsynchronousCloseException e) { + LOG.error("name=" + name, e); + throw new RuntimeException(e); } catch (IOException e) { close(); throw new IORuntimeException(e); @@ -1169,8 +1173,12 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon } else inBytes.limit(inBytes.capacity()); - // block until we have received all the bytes in this chunk - receiveBytesFromSocket(remainingBytes, timeoutTime); + try { + // block until we have received all the bytes in this chunk + receiveBytesFromSocket(remainingBytes, timeoutTime); + }catch (java.nio.channels.AsynchronousCloseException e ){ + return inBytes; + } final boolean isException = inBytes.readBoolean(); @@ -1308,6 +1316,7 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon @SuppressWarnings("UnusedReturnValue") private Bytes receiveBytesFromSocket(int requiredNumberOfBytes, long timeoutTime) throws IOException { + // assert !outBytesLock.isHeldByCurrentThread(); assert inBytesLock.isHeldByCurrentThread(); @@ -1315,11 +1324,14 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon inBytes.limit(requiredNumberOfBytes); inBytes.buffer().position(0); inBytes.buffer().limit(requiredNumberOfBytes); + SocketChannel clientChannel0 = clientChannel; + if (clientChannel0 == null) + return inBytes; while (inBytes.buffer().remaining() > 0) { assert requiredNumberOfBytes <= inBytes.capacity(); - int len = clientChannel.read(inBytes.buffer()); + int len = clientChannel0.read(inBytes.buffer()); if (len == -1) throw new IORuntimeException("Disconnection to server"); diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 3f8772856..c03366aa4 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -234,6 +234,9 @@ class VanillaChronicleMap, // todo remove the CheckedBytesStore before the release this.ms = new CheckedBytesStore(bytesStore); + + // this.ms = bytesStore; + onHeaderCreated(); long offset = getHeaderSize(); @@ -530,11 +533,15 @@ class VanillaChronicleMap, } @Override - public final V putIfAbsent(@net.openhft.lang.model.constraints.NotNull K key, V value) { + public final V putIfAbsent(K key, V value) { + if (key == null) + throw new NullPointerException(); return put1(key, value, false); } - V put1(@net.openhft.lang.model.constraints.NotNull K key, V value, boolean replaceIfPresent) { + V put1(K key, V value, boolean replaceIfPresent) { + if (key == null) + throw new NullPointerException(); checkKey(key); checkValue(value); ThreadLocalCopies copies = keyInteropProvider.getCopies(null); diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java index fa1254701..6fa4ca887 100755 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMapTest.java @@ -43,6 +43,7 @@ import static net.openhft.chronicle.map.StatelessClientTest.localClient; import static org.junit.Assert.*; @SuppressWarnings({"unchecked", "ResultOfMethodCallIgnored"}) +@Ignore public class ChronicleMapTest { static long count = 0; @@ -105,15 +106,25 @@ public class ChronicleMapTest { return file; } - private static ChronicleMap getSharedMap( - long entries, int segments, Alignment alignment, int keySize) - throws IOException { - return ChronicleMapBuilder.of(CharSequence.class, LongValue.class) - .entries(entries) - .minSegments(segments) - .averageKeySize(keySize) - .entryAndValueAlignment(alignment) - .create(); + private ChronicleMap getViewTestMap(int noOfElements) throws IOException { + ChronicleMap map = + ChronicleMapBuilder.of(Integer.class, CharSequence.class) + .entries(noOfElements * 2 + 100) + .averageValueSize((noOfElements + "").length()) + .putReturnsNull(true) + .removeReturnsNull(true).create(); + + int[] expectedKeys = new int[noOfElements]; + String[] expectedValues = new String[noOfElements]; + for (int i = 1; i <= noOfElements; i++) { + String value = "" + i; + map.put(i, value); + expectedKeys[i - 1] = i; + expectedValues[i - 1] = value; + } + + return map; + } private static void printStatus() { @@ -123,7 +134,7 @@ public class ChronicleMapTest { for (String line; (line = br.readLine()) != null; ) if (line.startsWith("Vm")) System.out.print(line.replaceAll(" +", " ") + ", "); - System.out.println(); + br.close(); } catch (IOException e) { e.printStackTrace(); @@ -133,47 +144,47 @@ public class ChronicleMapTest { @Test public void testRemoveWithKey() throws Exception { - final ChronicleMap map = - ChronicleMapBuilder - .of(CharSequence.class, CharSequence.class) - .minSegments(2).create(); + try (final ChronicleMap map = + ChronicleMapBuilder + .of(CharSequence.class, CharSequence.class) + .minSegments(2).create()) { - assertFalse(map.containsKey("key3")); - map.put("key1", "one"); - map.put("key2", "two"); - assertEquals(2, map.size()); + assertFalse(map.containsKey("key3")); + map.put("key1", "one"); + map.put("key2", "two"); + assertEquals(2, map.size()); - assertTrue(map.containsKey("key1")); - assertTrue(map.containsKey("key2")); - assertFalse(map.containsKey("key3")); + assertTrue(map.containsKey("key1")); + assertTrue(map.containsKey("key2")); + assertFalse(map.containsKey("key3")); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1")); + assertEquals("two", map.get("key2")); - final CharSequence result = map.remove("key1"); + final CharSequence result = map.remove("key1"); - assertEquals(1, map.size()); + assertEquals(1, map.size()); - assertEquals("one", result); - assertFalse(map.containsKey("key1")); + assertEquals("one", result); + assertFalse(map.containsKey("key1")); - assertEquals(null, map.get("key1")); - assertEquals("two", map.get("key2")); - assertFalse(map.containsKey("key3")); + assertEquals(null, map.get("key1")); + assertEquals("two", map.get("key2")); + assertFalse(map.containsKey("key3")); - // lets add one more item for luck ! - map.put("key3", "three"); - assertEquals("three", map.get("key3")); - assertTrue(map.containsKey("key3")); - assertEquals(2, map.size()); + // lets add one more item for luck ! + map.put("key3", "three"); + assertEquals("three", map.get("key3")); + assertTrue(map.containsKey("key3")); + assertEquals(2, map.size()); - // and just for kicks we'll overwrite what we have - map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); - assertTrue(map.containsKey("key3")); - assertEquals(2, map.size()); + // and just for kicks we'll overwrite what we have + map.put("key3", "overwritten"); + assertEquals("overwritten", map.get("key3")); + assertTrue(map.containsKey("key3")); + assertEquals(2, map.size()); - map.close(); + } } @@ -182,14 +193,17 @@ public class ChronicleMapTest { final File persistenceFile = Builder.getPersistenceFile(); for (int i = 0; i < 2; i++) { - ChronicleMap map = ChronicleMapBuilder.of(byte[].class, byte[] - .class).createPersistedTo(persistenceFile); + try (ChronicleMap map = ChronicleMapBuilder.of(byte[].class, byte[] + .class).createPersistedTo(persistenceFile)) { - byte[] o = map.get("hello".getBytes()); - System.out.println(o == null ? "null" : new String(o)); - map.put("hello".getBytes(), "world".getBytes()); + byte[] o = map.get("hello".getBytes()); + System.out.println(o == null ? "null" : new String(o)); + map.put("hello".getBytes(), "world".getBytes()); + } } + persistenceFile.delete(); + } @@ -260,247 +274,248 @@ public class ChronicleMapTest { @Test public void testSize() throws Exception { - final ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .minSegments(1024) - .removeReturnsNull(true).create(); + try (final ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + .minSegments(1024) + .removeReturnsNull(true).create()) { - for (int i = 1; i < 1024; i++) { - map.put("key" + i, "value"); - assertEquals(i, map.size()); - } + for (int i = 1; i < 1024; i++) { + map.put("key" + i, "value"); + assertEquals(i, map.size()); + } - for (int i = 1023; i >= 1; ) { - map.remove("key" + i); - i--; - assertEquals(i, map.size()); + for (int i = 1023; i >= 1; ) { + map.remove("key" + i); + i--; + assertEquals(i, map.size()); + } } - map.close(); } @Test public void testRemoveInteger() throws IOException { int count = 300; - final ChronicleMap map = ChronicleMapBuilder.of(Object.class, Object.class) + try (final ChronicleMap map = ChronicleMapBuilder.of(Object.class, Object + .class) .entries(count) - .minSegments(2).create(); + .minSegments(2).create()) { - for (int i = 1; i < count; i++) { - map.put(i, i); - assertEquals(i, map.size()); - } + for (int i = 1; i < count; i++) { + map.put(i, i); + assertEquals(i, map.size()); + } - for (int i = count - 1; i >= 1; ) { - Integer j = (Integer) map.put(i, i); - assertEquals(i, j.intValue()); - Integer j2 = (Integer) map.remove(i); - assertEquals(i, j2.intValue()); - i--; - assertEquals(i, map.size()); + for (int i = count - 1; i >= 1; ) { + Integer j = (Integer) map.put(i, i); + assertEquals(i, j.intValue()); + Integer j2 = (Integer) map.remove(i); + assertEquals(i, j2.intValue()); + i--; + assertEquals(i, map.size()); + } } - map.close(); } @Test public void testRemoveWithKeyAndRemoveReturnsNull() throws Exception { - final ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .minSegments(2) - .removeReturnsNull(true).create(); + try (final ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + .minSegments(2) + .removeReturnsNull(true).create()) { - assertFalse(map.containsKey("key3")); - map.put("key1", "one"); - map.put("key2", "two"); - assertEquals(2, map.size()); + assertFalse(map.containsKey("key3")); + map.put("key1", "one"); + map.put("key2", "two"); + assertEquals(2, map.size()); - assertTrue(map.containsKey("key1")); - assertTrue(map.containsKey("key2")); - assertFalse(map.containsKey("key3")); + assertTrue(map.containsKey("key1")); + assertTrue(map.containsKey("key2")); + assertFalse(map.containsKey("key3")); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1")); + assertEquals("two", map.get("key2")); - final CharSequence result = map.remove("key1"); - assertEquals(null, result); + final CharSequence result = map.remove("key1"); + assertEquals(null, result); - assertEquals(1, map.size()); + assertEquals(1, map.size()); - assertFalse(map.containsKey("key1")); + assertFalse(map.containsKey("key1")); - assertEquals(null, map.get("key1")); - assertEquals("two", map.get("key2")); - assertFalse(map.containsKey("key3")); + assertEquals(null, map.get("key1")); + assertEquals("two", map.get("key2")); + assertFalse(map.containsKey("key3")); - // lets add one more item for luck ! - map.put("key3", "three"); - assertEquals("three", map.get("key3")); - assertTrue(map.containsKey("key3")); - assertEquals(2, map.size()); + // lets add one more item for luck ! + map.put("key3", "three"); + assertEquals("three", map.get("key3")); + assertTrue(map.containsKey("key3")); + assertEquals(2, map.size()); - // and just for kicks we'll overwrite what we have - map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); - assertTrue(map.containsKey("key3")); - assertEquals(2, map.size()); + // and just for kicks we'll overwrite what we have + map.put("key3", "overwritten"); + assertEquals("overwritten", map.get("key3")); + assertTrue(map.containsKey("key3")); + assertEquals(2, map.size()); - map.close(); + } } @Test public void testReplaceWithKey() throws Exception { - final ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .minSegments(2).create(); + try (final ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + .minSegments(2).create()) { - map.put("key1", "one"); - map.put("key2", "two"); - assertEquals(2, map.size()); + map.put("key1", "one"); + map.put("key2", "two"); + assertEquals(2, map.size()); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1")); + assertEquals("two", map.get("key2")); - assertTrue(map.containsKey("key1")); - assertTrue(map.containsKey("key2")); + assertTrue(map.containsKey("key1")); + assertTrue(map.containsKey("key2")); - final CharSequence result = map.replace("key1", "newValue"); + final CharSequence result = map.replace("key1", "newValue"); - assertEquals("one", result); - assertTrue(map.containsKey("key1")); - assertTrue(map.containsKey("key2")); - assertEquals(2, map.size()); + assertEquals("one", result); + assertTrue(map.containsKey("key1")); + assertTrue(map.containsKey("key2")); + assertEquals(2, map.size()); - assertEquals("newValue", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("newValue", map.get("key1")); + assertEquals("two", map.get("key2")); - assertTrue(map.containsKey("key1")); - assertTrue(map.containsKey("key2")); - assertFalse(map.containsKey("key3")); + assertTrue(map.containsKey("key1")); + assertTrue(map.containsKey("key2")); + assertFalse(map.containsKey("key3")); - assertEquals(2, map.size()); + assertEquals(2, map.size()); - // let and one more item for luck ! - map.put("key3", "three"); - assertEquals(3, map.size()); + // let and one more item for luck ! + map.put("key3", "three"); + assertEquals(3, map.size()); - assertTrue(map.containsKey("key1")); - assertTrue(map.containsKey("key2")); - assertTrue(map.containsKey("key3")); - assertEquals("three", map.get("key3")); + assertTrue(map.containsKey("key1")); + assertTrue(map.containsKey("key2")); + assertTrue(map.containsKey("key3")); + assertEquals("three", map.get("key3")); - // and just for kicks we'll overwrite what we have - map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); + // and just for kicks we'll overwrite what we have + map.put("key3", "overwritten"); + assertEquals("overwritten", map.get("key3")); - assertTrue(map.containsKey("key1")); - assertTrue(map.containsKey("key2")); - assertTrue(map.containsKey("key3")); + assertTrue(map.containsKey("key1")); + assertTrue(map.containsKey("key2")); + assertTrue(map.containsKey("key3")); - final CharSequence result2 = map.replace("key2", "newValue"); + final CharSequence result2 = map.replace("key2", "newValue"); - assertEquals("two", result2); - assertEquals("newValue", map.get("key2")); + assertEquals("two", result2); + assertEquals("newValue", map.get("key2")); - final CharSequence result3 = map.replace("rubbish", "newValue"); - assertEquals(null, result3); + final CharSequence result3 = map.replace("rubbish", "newValue"); + assertEquals(null, result3); - assertFalse(map.containsKey("rubbish")); - assertEquals(3, map.size()); + assertFalse(map.containsKey("rubbish")); + assertEquals(3, map.size()); - map.close(); + } } @Test public void testReplaceWithKeyAnd2Params() throws Exception { - final ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .minSegments(2).create(); + try (final ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + .minSegments(2).create()) { - map.put("key1", "one"); - map.put("key2", "two"); + map.put("key1", "one"); + map.put("key2", "two"); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1")); + assertEquals("two", map.get("key2")); - final boolean result = map.replace("key1", "one", "newValue"); + final boolean result = map.replace("key1", "one", "newValue"); - assertEquals(true, result); + assertEquals(true, result); - assertEquals("newValue", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("newValue", map.get("key1")); + assertEquals("two", map.get("key2")); - // let and one more item for luck ! - map.put("key3", "three"); - assertEquals("three", map.get("key3")); + // let and one more item for luck ! + map.put("key3", "three"); + assertEquals("three", map.get("key3")); - // and just for kicks we'll overwrite what we have - map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); + // and just for kicks we'll overwrite what we have + map.put("key3", "overwritten"); + assertEquals("overwritten", map.get("key3")); - final boolean result2 = map.replace("key2", "two", "newValue2"); + final boolean result2 = map.replace("key2", "two", "newValue2"); - assertEquals(true, result2); - assertEquals("newValue2", map.get("key2")); + assertEquals(true, result2); + assertEquals("newValue2", map.get("key2")); - final boolean result3 = map.replace("newKey", "", "newValue"); - assertEquals(false, result3); + final boolean result3 = map.replace("newKey", "", "newValue"); + assertEquals(false, result3); - final boolean result4 = map.replace("key2", "newValue2", "newValue2"); - assertEquals(true, result4); + final boolean result4 = map.replace("key2", "newValue2", "newValue2"); + assertEquals(true, result4); - map.close(); + } } @Test public void testRemoveWithKeyAndValue() throws Exception { - final ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .minSegments(2).create(); + try (final ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + .minSegments(2).create()) { - map.put("key1", "one"); - map.put("key2", "two"); + map.put("key1", "one"); + map.put("key2", "two"); - assertEquals("one", map.get("key1")); - assertEquals("two", map.get("key2")); + assertEquals("one", map.get("key1")); + assertEquals("two", map.get("key2")); - // a false remove - final boolean wasRemoved1 = map.remove("key1", "three"); + // a false remove + final boolean wasRemoved1 = map.remove("key1", "three"); - assertFalse(wasRemoved1); + assertFalse(wasRemoved1); - assertEquals(null, map.get("key1"), "one"); - assertEquals("two", map.get("key2"), "two"); + assertEquals(null, map.get("key1"), "one"); + assertEquals("two", map.get("key2"), "two"); - map.put("key1", "one"); + map.put("key1", "one"); - final boolean wasRemoved2 = map.remove("key1", "three"); - assertFalse(wasRemoved2); + final boolean wasRemoved2 = map.remove("key1", "three"); + assertFalse(wasRemoved2); - // lets add one more item for luck ! - map.put("key3", "three"); - assertEquals("three", map.get("key3")); + // lets add one more item for luck ! + map.put("key3", "three"); + assertEquals("three", map.get("key3")); - // and just for kicks we'll overwrite what we have - map.put("key3", "overwritten"); - assertEquals("overwritten", map.get("key3")); + // and just for kicks we'll overwrite what we have + map.put("key3", "overwritten"); + assertEquals("overwritten", map.get("key3")); - map.close(); + } } @Test public void testAcquireWithNullContainer() throws Exception { - ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, LongValue.class) - .entries(1000) - .entryAndValueAlignment(OF_4_BYTES) - .create(); - map.acquireUsing("key", new LongValue$$Native()); - assertEquals(0, map.acquireUsing("key", null).getValue()); - map.close(); + try (ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, LongValue.class) + .entries(1000) + .entryAndValueAlignment(OF_4_BYTES) + .create()) { + map.acquireUsing("key", new LongValue$$Native()); + assertEquals(0, map.acquireUsing("key", null).getValue()); + } } // i7-3970X CPU @ 3.50GHz, hex core: -verbose:gc -Xmx64m @@ -536,27 +551,27 @@ public class ChronicleMapTest { @Test public void testGetWithNullContainer() throws Exception { - ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, LongValue.class) - .entries(10) - .entryAndValueAlignment(OF_4_BYTES) - .create(); - map.acquireUsing("key", new LongValue$$Native()); - assertEquals(0, map.getUsing("key", null).getValue()); + try (ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, LongValue.class) + .entries(10) + .entryAndValueAlignment(OF_4_BYTES) + .create()) { + map.acquireUsing("key", new LongValue$$Native()); + assertEquals(0, map.getUsing("key", null).getValue()); - map.close(); + } } @Test public void testGetWithoutAcquireFirst() throws Exception { - ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, LongValue.class) - .entries(10) - .entryAndValueAlignment(OF_4_BYTES) - .create(); - assertNull(map.getUsing("key", DataValueClasses.newDirectReference(LongValue.class))); + try (ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, LongValue.class) + .entries(10) + .entryAndValueAlignment(OF_4_BYTES) + .create()) { + assertNull(map.getUsing("key", DataValueClasses.newDirectReference(LongValue.class))); - map.close(); + } } // i7-3970X CPU @ 3.50GHz, hex core: -Xmx30g -verbose:gc @@ -575,75 +590,202 @@ public class ChronicleMapTest { public void testAcquireAndGet() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { int entries = 3/*00 * 1000*/; - testAcquireAndGet(getSharedMap(entries, 1, OF_4_BYTES, 10), entries); - testAcquireAndGet(getSharedMap(entries, 1, NO_ALIGNMENT, 10), entries); - testAcquireAndGet(getSharedMap(entries, 1, OF_8_BYTES, 10), entries); - } + try (ChronicleMap map2 = ChronicleMapBuilder.of(CharSequence.class, + LongValue.class) + .entries((long) entries) + .minSegments(1) + .averageKeySize(10) + .entryAndValueAlignment(OF_4_BYTES) + .create()) { + LongValue value4 = new LongValue$$Native(); + LongValue value22 = new LongValue$$Native(); + LongValue value32 = new LongValue$$Native(); - public void testAcquireAndGet(ChronicleMap map, int entries) - throws IOException, ClassNotFoundException, IllegalAccessException, - InstantiationException { - LongValue value = new LongValue$$Native(); - LongValue value2 = new LongValue$$Native(); - LongValue value3 = new LongValue$$Native(); + for (int j2 = 1; j2 <= 3; j2++) { + for (int i2 = 0; i2 < entries; i2++) { + CharSequence userCS2 = getUserCharSequence(i2); - for (int j = 1; j <= 3; j++) { - for (int i = 0; i < entries; i++) { - CharSequence userCS = getUserCharSequence(i); + if (j2 > 1) { + assertNotNull(userCS2.toString(), map2.getUsing(userCS2, value4)); + } else { + map2.acquireUsing(userCS2, value4); + } + if (i2 >= 1) + assertTrue(userCS2.toString(), map2.containsKey(getUserCharSequence(1))); + assertEquals(userCS2.toString(), j2 - 1, value4.getValue()); - if (j > 1) { - assertNotNull(userCS.toString(), map.getUsing(userCS, value)); - } else { - map.acquireUsing(userCS, value); + value4.addAtomicValue(1); + + assertEquals(value22, map2.acquireUsing(userCS2, value22)); + assertEquals(j2, value22.getValue()); + + assertEquals(value32, map2.getUsing(userCS2, value32)); + assertEquals(j2, value32.getValue()); } - if (i >= 1) - assertTrue(userCS.toString(), map.containsKey(getUserCharSequence(1))); - assertEquals(userCS.toString(), j - 1, value.getValue()); + } - value.addAtomicValue(1); + try (ChronicleMap map1 = ChronicleMapBuilder.of(CharSequence.class, + LongValue.class) + .entries((long) entries) + .minSegments(1) + .averageKeySize(10) + .entryAndValueAlignment(NO_ALIGNMENT) + .create()) { + LongValue value1 = new LongValue$$Native(); + LongValue value21 = new LongValue$$Native(); + LongValue value31 = new LongValue$$Native(); - assertEquals(value2, map.acquireUsing(userCS, value2)); - assertEquals(j, value2.getValue()); + for (int j1 = 1; j1 <= 3; j1++) { + for (int i1 = 0; i1 < entries; i1++) { + CharSequence userCS1 = getUserCharSequence(i1); + + if (j1 > 1) { + assertNotNull(userCS1.toString(), map1.getUsing(userCS1, value1)); + } else { + map1.acquireUsing(userCS1, value1); + } + if (i1 >= 1) + assertTrue(userCS1.toString(), map1.containsKey(getUserCharSequence(1))); + assertEquals(userCS1.toString(), j1 - 1, value1.getValue()); + + value1.addAtomicValue(1); + + assertEquals(value21, map1.acquireUsing(userCS1, value21)); + assertEquals(j1, value21.getValue()); + + assertEquals(value31, map1.getUsing(userCS1, value31)); + assertEquals(j1, value31.getValue()); + } + } + } + try (ChronicleMap map = ChronicleMapBuilder.of(CharSequence + .class, LongValue.class) + .entries((long) entries) + .minSegments(1) + .averageKeySize(10) + .entryAndValueAlignment(OF_8_BYTES) + .create()) { + LongValue value = new LongValue$$Native(); + LongValue value2 = new LongValue$$Native(); + LongValue value3 = new LongValue$$Native(); + + for (int j = 1; j <= 3; j++) { + for (int i = 0; i < entries; i++) { + CharSequence userCS = getUserCharSequence(i); + + if (j > 1) { + assertNotNull(userCS.toString(), map.getUsing(userCS, value)); + } else { + map.acquireUsing(userCS, value); + } + if (i >= 1) + assertTrue(userCS.toString(), map.containsKey(getUserCharSequence(1))); + assertEquals(userCS.toString(), j - 1, value.getValue()); + + value.addAtomicValue(1); + + assertEquals(value2, map.acquireUsing(userCS, value2)); + assertEquals(j, value2.getValue()); + + assertEquals(value3, map.getUsing(userCS, value3)); + assertEquals(j, value3.getValue()); + } + } - assertEquals(value3, map.getUsing(userCS, value3)); - assertEquals(j, value3.getValue()); } } - - map.close(); } + + @Ignore("Possible candidate for TC JVM crashes") @Test public void testAcquireFromMultipleThreads() throws Exception { int entries = 1000 * 1000; - testAcquireFromMultipleThreads(getSharedMap(entries, 128, NO_ALIGNMENT, 10)); - testAcquireFromMultipleThreads(getSharedMap(entries, 128, OF_4_BYTES, 10)); - testAcquireFromMultipleThreads(getSharedMap(entries, 128, OF_8_BYTES, 10)); - } + try (ChronicleMap map2 = ChronicleMapBuilder.of(CharSequence.class, + LongValue.class) + .entries((long) entries) + .minSegments(128) + .averageKeySize(10) + .entryAndValueAlignment(NO_ALIGNMENT) + .create()) { - public void testAcquireFromMultipleThreads(ChronicleMap map) - throws Exception { + CharSequence key2 = getUserCharSequence(0); + map2.acquireUsing(key2, new LongValue$$Native()); - CharSequence key = getUserCharSequence(0); - map.acquireUsing(key, new LongValue$$Native()); + int iterations2 = 10000; + int noOfThreads2 = 10; + CyclicBarrier barrier2 = new CyclicBarrier(noOfThreads2); - int iterations = 10000; - int noOfThreads = 10; - CyclicBarrier barrier = new CyclicBarrier(noOfThreads); + Thread[] threads2 = new Thread[noOfThreads2]; + for (int t2 = 0; t2 < noOfThreads2; t2++) { + threads2[t2] = new Thread(new IncrementRunnable(map2, key2, iterations2, barrier2)); + threads2[t2].start(); + } + for (int t2 = 0; t2 < noOfThreads2; t2++) { + threads2[t2].join(); + } - Thread[] threads = new Thread[noOfThreads]; - for (int t = 0; t < noOfThreads; t++) { - threads[t] = new Thread(new IncrementRunnable(map, key, iterations, barrier)); - threads[t].start(); - } - for (int t = 0; t < noOfThreads; t++) { - threads[t].join(); + assertEquals(noOfThreads2 * iterations2, + map2.acquireUsing(key2, new LongValue$$Native()).getValue()); + + try (ChronicleMap map1 = ChronicleMapBuilder.of(CharSequence + .class, LongValue.class) + .entries((long) entries) + .minSegments(128) + .averageKeySize(10) + .entryAndValueAlignment(OF_4_BYTES) + .create()) { + + CharSequence key1 = getUserCharSequence(0); + map1.acquireUsing(key1, new LongValue$$Native()); + + int iterations1 = 10000; + int noOfThreads1 = 10; + CyclicBarrier barrier1 = new CyclicBarrier(noOfThreads1); + + Thread[] threads1 = new Thread[noOfThreads1]; + for (int t1 = 0; t1 < noOfThreads1; t1++) { + threads1[t1] = new Thread(new IncrementRunnable(map1, key1, iterations1, barrier1)); + threads1[t1].start(); + } + for (int t1 = 0; t1 < noOfThreads1; t1++) { + threads1[t1].join(); + } + + assertEquals(noOfThreads1 * iterations1, + map1.acquireUsing(key1, new LongValue$$Native()).getValue()); + + try (ChronicleMap map = ChronicleMapBuilder.of(CharSequence + .class, LongValue.class) + .entries((long) entries) + .minSegments(128) + .averageKeySize(10) + .entryAndValueAlignment(OF_8_BYTES) + .create()) { + + CharSequence key = getUserCharSequence(0); + map.acquireUsing(key, new LongValue$$Native()); + + int iterations = 10000; + int noOfThreads = 10; + CyclicBarrier barrier = new CyclicBarrier(noOfThreads); + + Thread[] threads = new Thread[noOfThreads]; + for (int t = 0; t < noOfThreads; t++) { + threads[t] = new Thread(new IncrementRunnable(map, key, iterations, barrier)); + threads[t].start(); + } + for (int t = 0; t < noOfThreads; t++) { + threads[t].join(); + } + + assertEquals(noOfThreads * iterations, + map.acquireUsing(key, new LongValue$$Native()).getValue()); + + } + } } - assertEquals(noOfThreads * iterations, - map.acquireUsing(key, new LongValue$$Native()).getValue()); - - map.close(); } @Test @@ -664,17 +806,15 @@ public class ChronicleMapTest { .actualSegments(segments) .averageKeySize(14) .averageValueSize(entrySize - 14 - 2); - ChronicleMap map = builder.create(); - int procs = Runtime.getRuntime().availableProcessors(); - ExecutorService es = Executors.newFixedThreadPool(procs); + try (ChronicleMap map = builder.create()) { - StringBuilder sb = new StringBuilder(); - while (sb.length() < entrySize - 14 - 2) - sb.append('+'); - for (int i = 0; i < entries; i++) { - map.put("us:" + i, sb); + StringBuilder sb = new StringBuilder(); + while (sb.length() < entrySize - 14 - 2) + sb.append('+'); + for (int i = 0; i < entries; i++) { + map.put("us:" + i, sb); + } } - map.close(); } } @@ -693,7 +833,7 @@ public class ChronicleMapTest { Arrays.fill(chars, 'x'); final StringBuilder value0 = new StringBuilder(); value0.append(chars); - + File file; for (int segments : new int[]{/*128, 256, */512/*, 1024, 2048*/}) { final long entries = runs * 1000 * 1000L; ChronicleMapBuilder builder = ChronicleMapBuilder @@ -703,61 +843,63 @@ public class ChronicleMapTest { .averageKeySize(14) .averageValueSize(value0.length() + 4); + // File tmpFile = File.createTempFile("testAcquirePerf", ".deleteme"); // tmpFile.deleteOnExit(); //createPersistedTo(tmpFile); - final ChronicleMap map = builder.create(); + try (final ChronicleMap map = builder.create()) { + file = map.file(); + int count = runs > 500 ? 2 : 3; + System.out.println("\nKey size: " + runs + " Million entries. " + builder); + for (int j = 0; j < count; j++) { + long start = System.currentTimeMillis(); + for (int i = 0; i < threads; i++) { + final int t = i; - int count = runs > 500 ? 2 : 3; - System.out.println("\nKey size: " + runs + " Million entries. " + builder); - for (int j = 0; j < count; j++) { - long start = System.currentTimeMillis(); - for (int i = 0; i < threads; i++) { - final int t = i; + Random rand = new Random(t); + StringBuilder key = new StringBuilder(); + StringBuilder value = new StringBuilder(); + long next = 50 * 1000 * 1000; + // use a factor to give up to 10 digit numbers. + int factor = Math.max(1, + (int) ((10 * 1000 * 1000 * 1000L - 1) / entries)); + for (long k = t; k < entries; k++) { + key.setLength(0); + key.append("us:"); + key.append(k * factor); + // 75% reads, 25% writes. + if (rand.nextInt(4) > 0) { + map.getUsing(key, value); - Random rand = new Random(t); - StringBuilder key = new StringBuilder(); - StringBuilder value = new StringBuilder(); - long next = 50 * 1000 * 1000; - // use a factor to give up to 10 digit numbers. - int factor = Math.max(1, - (int) ((10 * 1000 * 1000 * 1000L - 1) / entries)); - for (long k = t; k < entries; k++) { - key.setLength(0); - key.append("us:"); - key.append(k * factor); - // 75% reads, 25% writes. - if (rand.nextInt(4) > 0) { - map.getUsing(key, value); - - } else { - try (WriteContext wc = map.acquireUsingLocked(key, value)) { - if (value.length() < value0.length() - 1) - value.append(value0); - else if (value.length() > value0.length()) - value.setLength(value0.length() - 1); - else - value.append('+'); + } else { + try (WriteContext wc = map.acquireUsingLocked(key, value)) { + if (value.length() < value0.length() - 1) + value.append(value0); + else if (value.length() > value0.length()) + value.setLength(value0.length() - 1); + else + value.append('+'); + } + } + if (t == 0 && k >= next) { + long size = map.longSize(); + if (size < 0) throw new AssertionError("size: " + size); + System.out.println(k + ", size: " + size); + next += 50 * 1000 * 1000; } } - if (t == 0 && k >= next) { - long size = map.longSize(); - if (size < 0) throw new AssertionError("size: " + size); - System.out.println(k + ", size: " + size); - next += 50 * 1000 * 1000; - } } - } - long time = System.currentTimeMillis() - start; - System.out.printf("EntrySize: %,d Entries: %,d M Segments: " + - "%,d Throughput %.1f M ops/sec%n", - entrySize, runs, segments, - threads * entries / 1000.0 / time); + long time = System.currentTimeMillis() - start; + System.out.printf("EntrySize: %,d Entries: %,d M Segments: " + + "%,d Throughput %.1f M ops/sec%n", + entrySize, runs, segments, + threads * entries / 1000.0 / time); + } + printStatus(); + } - printStatus(); - File file = map.file(); - map.close(); + if (file != null) file.delete(); } @@ -773,6 +915,7 @@ public class ChronicleMapTest { // int runs = Integer.getInteger("runs", 10); int procs = 1; // Runtime.getRuntime().availableProcessors(); int threads = procs * 3; + File file; ExecutorService es = Executors.newFixedThreadPool(procs); for (int runs : new int[]{1, /*10, 250, 500, 1000, 2500*/}) { for (int entrySize : new int[]{240, 256}) { @@ -793,66 +936,67 @@ public class ChronicleMapTest { // File tmpFile = File.createTempFile("testAcquirePerf", ".deleteme"); // tmpFile.deleteOnExit(); - final ChronicleMap map = builder.create(); //createPersistedTo(tmpFile); + try (final ChronicleMap map = builder.create()) { //createPersistedTo(tmpFile); + file = map.file(); + int count = runs > 500 ? 2 : 3; + final int independence = Math.min(procs, runs > 500 ? 8 : 4); + System.out.println("\nKey size: " + runs + " Million entries. " + builder); + for (int j = 0; j < count; j++) { + long start = System.currentTimeMillis(); + List futures = new ArrayList<>(); + for (int i = 0; i < threads; i++) { + final int t = i; + futures.add(es.submit(new Runnable() { + @Override + public void run() { + Random rand = new Random(t); + StringBuilder key = new StringBuilder(); + StringBuilder value = new StringBuilder(); + long next = 50 * 1000 * 1000; + // use a factor to give up to 10 digit numbers. + int factor = Math.max(1, + (int) ((10 * 1000 * 1000 * 1000L - 1) / entries)); + for (long j = t % independence; j < entries + independence - 1; + j += independence) { + key.setLength(0); + key.append("us:"); + key.append(j * factor); + // 75% reads, 25% writes. + if (rand.nextInt(4) > 0) { + map.getUsing(key, value); - int count = runs > 500 ? 2 : 3; - final int independence = Math.min(procs, runs > 500 ? 8 : 4); - System.out.println("\nKey size: " + runs + " Million entries. " + builder); - for (int j = 0; j < count; j++) { - long start = System.currentTimeMillis(); - List futures = new ArrayList<>(); - for (int i = 0; i < threads; i++) { - final int t = i; - futures.add(es.submit(new Runnable() { - @Override - public void run() { - Random rand = new Random(t); - StringBuilder key = new StringBuilder(); - StringBuilder value = new StringBuilder(); - long next = 50 * 1000 * 1000; - // use a factor to give up to 10 digit numbers. - int factor = Math.max(1, - (int) ((10 * 1000 * 1000 * 1000L - 1) / entries)); - for (long j = t % independence; j < entries + independence - 1; - j += independence) { - key.setLength(0); - key.append("us:"); - key.append(j * factor); - // 75% reads, 25% writes. - if (rand.nextInt(4) > 0) { - map.getUsing(key, value); - - } else { - try (WriteContext wc = map.acquireUsingLocked(key, value)) { - if (value.length() < value0.length() - 1) - value.append(value0); - else if (value.length() > value0.length()) - value.setLength(value0.length() - 1); - else - value.append('+'); + } else { + try (WriteContext wc = map.acquireUsingLocked(key, value)) { + if (value.length() < value0.length() - 1) + value.append(value0); + else if (value.length() > value0.length()) + value.setLength(value0.length() - 1); + else + value.append('+'); + } + } + if (t == 0 && j >= next) { + long size = map.longSize(); + if (size < 0) + throw new AssertionError("size: " + size); + System.out.println(j + ", size: " + size); + next += 50 * 1000 * 1000; } } - if (t == 0 && j >= next) { - long size = map.longSize(); - if (size < 0) throw new AssertionError("size: " + size); - System.out.println(j + ", size: " + size); - next += 50 * 1000 * 1000; - } } - } - })); + })); + } + for (Future future : futures) { + future.get(); + } + long time = System.currentTimeMillis() - start; + System.out.printf("EntrySize: %,d Entries: %,d M Segments: %,d Throughput %.1f M ops/sec%n", + entrySize, runs, segments, + threads * entries / independence / 1000.0 / time); } - for (Future future : futures) { - future.get(); - } - long time = System.currentTimeMillis() - start; - System.out.printf("EntrySize: %,d Entries: %,d M Segments: %,d Throughput %.1f M ops/sec%n", - entrySize, runs, segments, - threads * entries / independence / 1000.0 / time); + printStatus(); + } - printStatus(); - File file = map.file(); - map.close(); if (file != null) file.delete(); } @@ -884,64 +1028,66 @@ public class ChronicleMapTest { File tmpFile = File.createTempFile("testAcquirePerf", ".deleteme"); tmpFile.deleteOnExit(); - final ChronicleMap map = builder.createPersistedTo(tmpFile); + try (final ChronicleMap map = builder.createPersistedTo + (tmpFile)) { - int count = runs >= 5 ? 2 : 3; - final int independence = Math.min(procs, runs > 500 ? 8 : 4); - System.out.println("\nKey size: " + runs + " Million entries. " + builder); - for (int j = 0; j < count; j++) { - long start = System.currentTimeMillis(); - List futures = new ArrayList(); - for (int i = 0; i < threads; i++) { - final int t = i; - futures.add(es.submit(new Runnable() { - @Override - public void run() { - LongValue value = nativeLongValue(); - StringBuilder sb = new StringBuilder(); - long next = 50 * 1000 * 1000; - Random rand = new Random(); - // use a factor to give up to 10 digit numbers. - int factor = Math.max(1, - (int) ((10 * 1000 * 1000 * 1000L - 1) / entries)); - for (long j = t % independence; j < entries + independence - 1; - j += independence) { - sb.setLength(0); - sb.append("us:"); - sb.append(j * factor); - long n; - // 75% read - if (rand.nextBoolean() || rand.nextBoolean()) { - try (ReadContext rc = map.getUsingLocked(sb, value)) { - n = rc.present() ? value.getValue() + 1 : 1; + int count = runs >= 5 ? 2 : 3; + final int independence = Math.min(procs, runs > 500 ? 8 : 4); + System.out.println("\nKey size: " + runs + " Million entries. " + builder); + for (int j = 0; j < count; j++) { + long start = System.currentTimeMillis(); + List futures = new ArrayList(); + for (int i = 0; i < threads; i++) { + final int t = i; + futures.add(es.submit(new Runnable() { + @Override + public void run() { + LongValue value = nativeLongValue(); + StringBuilder sb = new StringBuilder(); + long next = 50 * 1000 * 1000; + Random rand = new Random(); + // use a factor to give up to 10 digit numbers. + int factor = Math.max(1, + (int) ((10 * 1000 * 1000 * 1000L - 1) / entries)); + for (long j = t % independence; j < entries + independence - 1; + j += independence) { + sb.setLength(0); + sb.append("us:"); + sb.append(j * factor); + long n; + // 75% read + if (rand.nextBoolean() || rand.nextBoolean()) { + try (ReadContext rc = map.getUsingLocked(sb, value)) { + n = rc.present() ? value.getValue() + 1 : 1; + } + } else { + try (WriteContext wc = map.acquireUsingLocked(sb, value)) { + n = value.addValue(1); + } } - } else { - try (WriteContext wc = map.acquireUsingLocked(sb, value)) { - n = value.addValue(1); + assert n > 0 && n < 1000 : "Counter corrupted " + n; + if (t == 0 && j >= next) { + long size = map.longSize(); + if (size < 0) throw new AssertionError("size: " + size); + System.out.println(j + ", size: " + size); + next += 50 * 1000 * 1000; } } - assert n > 0 && n < 1000 : "Counter corrupted " + n; - if (t == 0 && j >= next) { - long size = map.longSize(); - if (size < 0) throw new AssertionError("size: " + size); - System.out.println(j + ", size: " + size); - next += 50 * 1000 * 1000; - } } - } - })); + })); + } + for (Future future : futures) { + future.get(); + } + long time = System.currentTimeMillis() - start; + System.out.printf("Throughput %.1f M ops/sec%n", + threads * entries / independence / 1000.0 / time); } - for (Future future : futures) { - future.get(); - } - long time = System.currentTimeMillis() - start; - System.out.printf("Throughput %.1f M ops/sec%n", - threads * entries / independence / 1000.0 / time); } printStatus(); - File file = map.file(); - map.close(); - file.delete(); + + + tmpFile.delete(); } es.shutdown(); es.awaitTermination(1, TimeUnit.MINUTES); @@ -957,7 +1103,7 @@ public class ChronicleMapTest { int threads = procs * 3; // runs > 100 ? procs / 2 : procs; ExecutorService es = Executors.newFixedThreadPool(procs); for (int runs : new int[]{10, 50, 100, 250, 500, 1000, 2500}) { - // JAVA 8 produces more garbage than previous versions for internal work. +// JAVA 8 produces more garbage than previous versions for internal work. // System.gc(); final long entries = runs * 1000 * 1000L; ChronicleMapBuilder builder = ChronicleMapBuilder @@ -967,54 +1113,54 @@ public class ChronicleMapTest { File tmpFile = File.createTempFile("testAcquirePerf", ".deleteme"); tmpFile.deleteOnExit(); - final ChronicleMap map = builder.createPersistedTo(tmpFile); + try (final ChronicleMap map = builder.createPersistedTo(tmpFile)) { - int count = runs > 500 ? runs > 1200 ? 3 : 5 : 5; - final int independence = Math.min(procs, runs > 500 ? 8 : 4); - System.out.println("\nKey size: " + runs + " Million entries. " + builder); - for (int j = 0; j < count; j++) { - long start = System.currentTimeMillis(); - List futures = new ArrayList(); - for (int i = 0; i < threads; i++) { - final int t = i; - futures.add(es.submit(new Runnable() { - @Override - public void run() { - LongValue key = DataValueClasses.newDirectInstance(LongValue.class); - LongValue value = nativeLongValue(); - long next = 50 * 1000 * 1000; - // use a factor to give up to 10 digit numbers. - int factor = Math.max(1, - (int) ((10 * 1000 * 1000 * 1000L - 1) / entries)); - for (long j = t % independence; j < entries + independence - 1; - j += independence) { - key.setValue(j * factor); - long n; - try (WriteContext wc = map.acquireUsingLocked(key, value)) { - n = value.addValue(1); - } - assert n > 0 && n < 1000 : "Counter corrupted " + n; - if (t == 0 && j >= next) { - long size = map.longSize(); - if (size < 0) throw new AssertionError("size: " + size); - System.out.println(j + ", size: " + size); - next += 50 * 1000 * 1000; + int count = runs > 500 ? runs > 1200 ? 3 : 5 : 5; + final int independence = Math.min(procs, runs > 500 ? 8 : 4); + System.out.println("\nKey size: " + runs + " Million entries. " + builder); + for (int j = 0; j < count; j++) { + long start = System.currentTimeMillis(); + List futures = new ArrayList(); + for (int i = 0; i < threads; i++) { + final int t = i; + futures.add(es.submit(new Runnable() { + @Override + public void run() { + LongValue key = DataValueClasses.newDirectInstance(LongValue.class); + LongValue value = nativeLongValue(); + long next = 50 * 1000 * 1000; + // use a factor to give up to 10 digit numbers. + int factor = Math.max(1, + (int) ((10 * 1000 * 1000 * 1000L - 1) / entries)); + for (long j = t % independence; j < entries + independence - 1; + j += independence) { + key.setValue(j * factor); + long n; + try (WriteContext wc = map.acquireUsingLocked(key, value)) { + n = value.addValue(1); + } + assert n > 0 && n < 1000 : "Counter corrupted " + n; + if (t == 0 && j >= next) { + long size = map.longSize(); + if (size < 0) throw new AssertionError("size: " + size); + System.out.println(j + ", size: " + size); + next += 50 * 1000 * 1000; + } } } - } - })); + })); + } + for (Future future : futures) { + future.get(); + } + long time = System.currentTimeMillis() - start; + System.out.printf("Throughput %.1f M ops/sec%n", + threads * entries / independence / 1000.0 / time); } - for (Future future : futures) { - future.get(); - } - long time = System.currentTimeMillis() - start; - System.out.printf("Throughput %.1f M ops/sec%n", - threads * entries / independence / 1000.0 / time); } printStatus(); - File file = map.file(); - map.close(); - file.delete(); + + tmpFile.delete(); } es.shutdown(); es.awaitTermination(1, TimeUnit.MINUTES); @@ -1082,384 +1228,400 @@ public class ChronicleMapTest { @Test public void testPutAndRemove() throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException { - String TMP = System.getProperty("java.io.tmpdir"); - int entries = 100 * 1000; - ChronicleMap map = - ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) - .entries(entries) - .minSegments(16) - .averageKeySize("user:".length() + 6) - .averageValueSize("value:".length() + 6) - .putReturnsNull(true) - .removeReturnsNull(true).create(); - StringBuilder key = new StringBuilder(); - StringBuilder value = new StringBuilder(); - StringBuilder value2 = new StringBuilder(); - for (int j = 1; j <= 3; j++) { - for (int i = 0; i < entries; i++) { - key.setLength(0); - key.append("user:").append(i); - value.setLength(0); - value.append("value:").append(i); -// System.out.println(key); - assertNull(map.getUsing(key, value)); - assertNull(map.put(key, value)); - assertNotNull(map.getUsing(key, value2)); - assertEquals(value.toString(), value2.toString()); - assertNull(map.remove(key)); - assertNull(map.getUsing(key, value)); - } - } - map.close(); + int entries = 100 * 1000; + try (ChronicleMap map = + ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) + .entries(entries) + .minSegments(16) + .averageKeySize("user:".length() + 6) + .averageValueSize("value:".length() + 6) + .putReturnsNull(true) + .removeReturnsNull(true).create()) { + StringBuilder key = new StringBuilder(); + StringBuilder value = new StringBuilder(); + StringBuilder value2 = new StringBuilder(); + for (int j = 1; j <= 3; j++) { + for (int i = 0; i < entries; i++) { + key.setLength(0); + key.append("user:").append(i); + value.setLength(0); + value.append("value:").append(i); +// System.out.println(key); + assertNull(map.getUsing(key, value)); + assertNull(map.put(key, value)); + assertNotNull(map.getUsing(key, value2)); + assertEquals(value.toString(), value2.toString()); + assertNull(map.remove(key)); + assertNull(map.getUsing(key, value)); + } + } + + } } @Test public void mapRemoveReflectedInViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3);) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - map.remove(2); - assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertEntrySet(map.entrySet(), new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertKeySet(keySet, new int[]{1, 3}); - assertKeySet(map.keySet(), new int[]{1, 3}); - assertValues(values, new CharSequence[]{"1", "3"}); - assertValues(map.values(), new CharSequence[]{"1", "3"}); + map.remove(2); + assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertEntrySet(map.entrySet(), new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertKeySet(keySet, new int[]{1, 3}); + assertKeySet(map.keySet(), new int[]{1, 3}); + assertValues(values, new CharSequence[]{"1", "3"}); + assertValues(map.values(), new CharSequence[]{"1", "3"}); + } - map.close(); } + @Test public void mapPutReflectedInViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - map.put(4, "4"); - assertMap(map, new int[]{4, 2, 3, 1}, new CharSequence[]{"4", "2", "3", "1"}); - assertEntrySet(entrySet, new int[]{4, 2, 3, 1}, new CharSequence[]{"4", "2", "3", "1"}); - assertEntrySet(map.entrySet(), new int[]{4, 2, 3, 1}, new CharSequence[]{"4", "2", "3", "1"}); - assertKeySet(keySet, new int[]{4, 2, 3, 1}); - assertKeySet(map.keySet(), new int[]{4, 2, 3, 1}); - assertValues(values, new CharSequence[]{"2", "1", "4", "3"}); - assertValues(map.values(), new CharSequence[]{"2", "1", "4", "3"}); - - map.close(); + map.put(4, "4"); + assertMap(map, new int[]{4, 2, 3, 1}, new CharSequence[]{"4", "2", "3", "1"}); + assertEntrySet(entrySet, new int[]{4, 2, 3, 1}, new CharSequence[]{"4", "2", "3", "1"}); + assertEntrySet(map.entrySet(), new int[]{4, 2, 3, 1}, new CharSequence[]{"4", "2", "3", "1"}); + assertKeySet(keySet, new int[]{4, 2, 3, 1}); + assertKeySet(map.keySet(), new int[]{4, 2, 3, 1}); + assertValues(values, new CharSequence[]{"2", "1", "4", "3"}); + assertValues(map.values(), new CharSequence[]{"2", "1", "4", "3"}); + } } + @Test public void entrySetRemoveReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - entrySet.remove(new AbstractMap.SimpleEntry(2, "2")); - assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertKeySet(keySet, new int[]{1, 3}); - assertValues(values, new CharSequence[]{"1", "3"}); - - map.close(); + entrySet.remove(new AbstractMap.SimpleEntry(2, "2")); + assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertKeySet(keySet, new int[]{1, 3}); + assertValues(values, new CharSequence[]{"1", "3"}); + } } + @Test public void keySetRemoveReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - keySet.remove(2); - assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertKeySet(keySet, new int[]{1, 3}); - assertValues(values, new CharSequence[]{"1", "3"}); + keySet.remove(2); + assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertKeySet(keySet, new int[]{1, 3}); + + assertValues(values, new CharSequence[]{"1", "3"}); + + } - map.close(); } @Test public void valuesRemoveReflectedInMap() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - values.remove("2"); - assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); - assertKeySet(keySet, new int[]{1, 3}); - assertValues(values, new CharSequence[]{"1", "3"}); + values.remove("2"); + assertMap(map, new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertEntrySet(entrySet, new int[]{1, 3}, new CharSequence[]{"1", "3"}); + assertKeySet(keySet, new int[]{1, 3}); + assertValues(values, new CharSequence[]{"1", "3"}); + + } - map.close(); } @Test public void entrySetIteratorRemoveReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Map refMap = new HashMap(map); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Map refMap = new HashMap(map); + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - Iterator> entryIterator = entrySet.iterator(); - entryIterator.next(); - refMap.remove(entryIterator.next().getKey()); - entryIterator.remove(); - int[] expectedKeys = Ints.toArray(refMap.keySet()); - CharSequence[] expectedValues = refMap.values().toArray(new CharSequence[0]); - assertMap(map, expectedKeys, expectedValues); - assertEntrySet(entrySet, expectedKeys, expectedValues); - assertKeySet(keySet, expectedKeys); - assertValues(values, expectedValues); - - map.close(); + Iterator> entryIterator = entrySet.iterator(); + entryIterator.next(); + refMap.remove(entryIterator.next().getKey()); + entryIterator.remove(); + int[] expectedKeys = Ints.toArray(refMap.keySet()); + CharSequence[] expectedValues = refMap.values().toArray(new CharSequence[0]); + assertMap(map, expectedKeys, expectedValues); + assertEntrySet(entrySet, expectedKeys, expectedValues); + assertKeySet(keySet, expectedKeys); + assertValues(values, expectedValues); + } } + @Test public void keySetIteratorRemoveReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Map refMap = new HashMap(map); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Map refMap = new HashMap(map); + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - Iterator keyIterator = keySet.iterator(); - keyIterator.next(); - refMap.remove(keyIterator.next()); - keyIterator.remove(); - int[] expectedKeys = Ints.toArray(refMap.keySet()); - CharSequence[] expectedValues = refMap.values().toArray(new CharSequence[0]); - assertMap(map, expectedKeys, expectedValues); - assertEntrySet(entrySet, expectedKeys, expectedValues); - assertKeySet(keySet, expectedKeys); - assertValues(values, expectedValues); + Iterator keyIterator = keySet.iterator(); + keyIterator.next(); + refMap.remove(keyIterator.next()); + keyIterator.remove(); + int[] expectedKeys = Ints.toArray(refMap.keySet()); + CharSequence[] expectedValues = refMap.values().toArray(new CharSequence[0]); + assertMap(map, expectedKeys, expectedValues); + assertEntrySet(entrySet, expectedKeys, expectedValues); + assertKeySet(keySet, expectedKeys); + assertValues(values, expectedValues); + + } - map.close(); } @Test public void valuesIteratorRemoveReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - HashBiMap refMap = HashBiMap.create(map); + try (ChronicleMap map = getViewTestMap(3)) { + HashBiMap refMap = HashBiMap.create(map); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); + + Iterator valueIterator = values.iterator(); + valueIterator.next(); + refMap.inverse().remove(valueIterator.next()); + valueIterator.remove(); + int[] expectedKeys = Ints.toArray(refMap.keySet()); + CharSequence[] expectedValues = new CharSequence[expectedKeys.length]; + for (int i = 0; i < expectedKeys.length; i++) { + expectedValues[i] = refMap.get(expectedKeys[i]); + } + assertMap(map, expectedKeys, expectedValues); + assertEntrySet(entrySet, expectedKeys, expectedValues); + assertKeySet(keySet, expectedKeys); + assertValues(values, expectedValues); - Iterator valueIterator = values.iterator(); - valueIterator.next(); - refMap.inverse().remove(valueIterator.next()); - valueIterator.remove(); - int[] expectedKeys = Ints.toArray(refMap.keySet()); - CharSequence[] expectedValues = new CharSequence[expectedKeys.length]; - for (int i = 0; i < expectedKeys.length; i++) { - expectedValues[i] = refMap.get(expectedKeys[i]); } - assertMap(map, expectedKeys, expectedValues); - assertEntrySet(entrySet, expectedKeys, expectedValues); - assertKeySet(keySet, expectedKeys); - assertValues(values, expectedValues); - map.close(); } @Test public void entrySetRemoveAllReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - entrySet.removeAll( - Arrays.asList( - new AbstractMap.SimpleEntry(1, "1"), - new AbstractMap.SimpleEntry(2, "2") - ) - ); - assertMap(map, new int[]{3}, new CharSequence[]{"3"}); - assertEntrySet(entrySet, new int[]{3}, new CharSequence[]{"3"}); - assertKeySet(keySet, new int[]{3}); - assertValues(values, new CharSequence[]{"3"}); + entrySet.removeAll( + Arrays.asList( + new AbstractMap.SimpleEntry(1, "1"), + new AbstractMap.SimpleEntry(2, "2") + ) + ); + assertMap(map, new int[]{3}, new CharSequence[]{"3"}); + assertEntrySet(entrySet, new int[]{3}, new CharSequence[]{"3"}); + assertKeySet(keySet, new int[]{3}); + assertValues(values, new CharSequence[]{"3"}); + + } - map.close(); } @Test public void keySetRemoveAllReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - keySet.removeAll(Arrays.asList(1, 2)); - assertMap(map, new int[]{3}, new CharSequence[]{"3"}); - assertEntrySet(entrySet, new int[]{3}, new CharSequence[]{"3"}); - assertKeySet(keySet, new int[]{3}); - assertValues(values, new CharSequence[]{"3"}); + keySet.removeAll(Arrays.asList(1, 2)); + assertMap(map, new int[]{3}, new CharSequence[]{"3"}); + assertEntrySet(entrySet, new int[]{3}, new CharSequence[]{"3"}); + assertKeySet(keySet, new int[]{3}); + assertValues(values, new CharSequence[]{"3"}); + + } - map.close(); } @Test public void valuesRemoveAllReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - values.removeAll(Arrays.asList("1", "2")); - assertMap(map, new int[]{3}, new CharSequence[]{"3"}); - assertEntrySet(entrySet, new int[]{3}, new CharSequence[]{"3"}); - assertKeySet(keySet, new int[]{3}); - assertValues(values, new CharSequence[]{"3"}); + values.removeAll(Arrays.asList("1", "2")); + assertMap(map, new int[]{3}, new CharSequence[]{"3"}); + assertEntrySet(entrySet, new int[]{3}, new CharSequence[]{"3"}); + assertKeySet(keySet, new int[]{3}); + assertValues(values, new CharSequence[]{"3"}); - map.close(); + } } + @Test public void entrySetRetainAllReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - entrySet.retainAll( - Arrays.asList( - new AbstractMap.SimpleEntry(1, "1"), - new AbstractMap.SimpleEntry(2, "2") - ) - ); - assertMap(map, new int[]{2, 1}, new CharSequence[]{"2", "1"}); - assertEntrySet(entrySet, new int[]{2, 1}, new CharSequence[]{"2", "1"}); - assertKeySet(keySet, new int[]{2, 1}); - assertValues(values, new CharSequence[]{"2", "1"}); + entrySet.retainAll( + Arrays.asList( + new AbstractMap.SimpleEntry(1, "1"), + new AbstractMap.SimpleEntry(2, "2") + ) + ); + assertMap(map, new int[]{2, 1}, new CharSequence[]{"2", "1"}); + assertEntrySet(entrySet, new int[]{2, 1}, new CharSequence[]{"2", "1"}); + assertKeySet(keySet, new int[]{2, 1}); + assertValues(values, new CharSequence[]{"2", "1"}); - map.close(); + } } + @Test public void keySetRetainAllReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - keySet.retainAll(Arrays.asList(1, 2)); - assertMap(map, new int[]{2, 1}, new CharSequence[]{"2", "1"}); - assertEntrySet(entrySet, new int[]{2, 1}, new CharSequence[]{"2", "1"}); - assertKeySet(keySet, new int[]{2, 1}); - assertValues(values, new CharSequence[]{"2", "1"}); + keySet.retainAll(Arrays.asList(1, 2)); + assertMap(map, new int[]{2, 1}, new CharSequence[]{"2", "1"}); + assertEntrySet(entrySet, new int[]{2, 1}, new CharSequence[]{"2", "1"}); + assertKeySet(keySet, new int[]{2, 1}); + assertValues(values, new CharSequence[]{"2", "1"}); + + } - map.close(); } @Test public void valuesRetainAllReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - values.retainAll(Arrays.asList("1", "2")); - assertMap(map, new int[]{2, 1}, new CharSequence[]{"2", "1"}); - assertEntrySet(entrySet, new int[]{2, 1}, new CharSequence[]{"2", "1"}); - assertKeySet(keySet, new int[]{2, 1}); - assertValues(values, new CharSequence[]{"2", "1"}); + values.retainAll(Arrays.asList("1", "2")); + assertMap(map, new int[]{2, 1}, new CharSequence[]{"2", "1"}); + assertEntrySet(entrySet, new int[]{2, 1}, new CharSequence[]{"2", "1"}); + assertKeySet(keySet, new int[]{2, 1}); + assertValues(values, new CharSequence[]{"2", "1"}); - map.close(); + } } @Test public void entrySetClearReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - entrySet.clear(); - org.junit.Assert.assertTrue(map.isEmpty()); - org.junit.Assert.assertTrue(entrySet.isEmpty()); - org.junit.Assert.assertTrue(keySet.isEmpty()); - org.junit.Assert.assertTrue(values.isEmpty()); + entrySet.clear(); + org.junit.Assert.assertTrue(map.isEmpty()); + org.junit.Assert.assertTrue(entrySet.isEmpty()); + org.junit.Assert.assertTrue(keySet.isEmpty()); + org.junit.Assert.assertTrue(values.isEmpty()); + + } - map.close(); } @Test public void keySetClearReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - keySet.clear(); - org.junit.Assert.assertTrue(map.isEmpty()); - org.junit.Assert.assertTrue(entrySet.isEmpty()); - org.junit.Assert.assertTrue(keySet.isEmpty()); - org.junit.Assert.assertTrue(values.isEmpty()); + keySet.clear(); + org.junit.Assert.assertTrue(map.isEmpty()); + org.junit.Assert.assertTrue(entrySet.isEmpty()); + org.junit.Assert.assertTrue(keySet.isEmpty()); + org.junit.Assert.assertTrue(values.isEmpty()); - map.close(); + } } @Test public void valuesClearReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(3); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + try (ChronicleMap map = getViewTestMap(3)) { + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - values.clear(); - org.junit.Assert.assertTrue(map.isEmpty()); - org.junit.Assert.assertTrue(entrySet.isEmpty()); - org.junit.Assert.assertTrue(keySet.isEmpty()); - org.junit.Assert.assertTrue(values.isEmpty()); + values.clear(); + org.junit.Assert.assertTrue(map.isEmpty()); + org.junit.Assert.assertTrue(entrySet.isEmpty()); + org.junit.Assert.assertTrue(keySet.isEmpty()); + org.junit.Assert.assertTrue(values.isEmpty()); + + } - map.close(); } @Test public void clearMapViaEntryIteratorRemoves() throws IOException { int noOfElements = 16 * 1024; - ChronicleMap map = getViewTestMap(noOfElements); + try (ChronicleMap map = getViewTestMap(noOfElements)) { + + int sum = 0; + for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) { + it.next(); + it.remove(); + ++sum; + + } + + assertEquals(noOfElements, sum); - int sum = 0; - for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) { - it.next(); - it.remove(); - ++sum; } - map.close(); - assertEquals(noOfElements, sum); } @Test public void clearMapViaKeyIteratorRemoves() throws IOException { int noOfElements = 16 * 1024; - ChronicleMap map = getViewTestMap(noOfElements); + try (ChronicleMap map = getViewTestMap(noOfElements)) { - Set keys = new HashSet(); - for (int i = 1; i <= noOfElements; i++) { - keys.add(i); + Set keys = new HashSet(); + for (int i = 1; i <= noOfElements; i++) { + keys.add(i); + } + + int sum = 0; + for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { + Object key = it.next(); + keys.remove(key); + it.remove(); + ++sum; + } + + assertEquals(noOfElements, sum); } - - int sum = 0; - for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { - Object key = it.next(); - keys.remove(key); - it.remove(); - ++sum; - } - map.close(); - - assertEquals(noOfElements, sum); } @Test(expected = IllegalStateException.class) @@ -1474,132 +1636,115 @@ public class ChronicleMapTest { @Test public void clearMapViaValueIteratorRemoves() throws IOException { int noOfElements = 16 * 1024; - ChronicleMap map = getViewTestMap(noOfElements); + try (ChronicleMap map = getViewTestMap(noOfElements)) { - int sum = 0; - for (Iterator it = map.values().iterator(); it.hasNext(); ) { - it.next(); - it.remove(); - ++sum; + int sum = 0; + for (Iterator it = map.values().iterator(); it.hasNext(); ) { + it.next(); + it.remove(); + ++sum; + } + + + assertEquals(noOfElements, sum); } - map.close(); - - assertEquals(noOfElements, sum); } @Test public void entrySetValueReflectedInMapAndOtherViews() throws IOException { - ChronicleMap map = getViewTestMap(0); + try (ChronicleMap map = getViewTestMap(0)) { - map.put(1, "A"); - Set> entrySet = map.entrySet(); - Set keySet = map.keySet(); - Collection values = map.values(); + map.put(1, "A"); + Set> entrySet = map.entrySet(); + Set keySet = map.keySet(); + Collection values = map.values(); - assertMap(map, new int[]{1}, new CharSequence[]{"A"}); - assertEntrySet(entrySet, new int[]{1}, new CharSequence[]{"A"}); - assertKeySet(keySet, new int[]{1}); - assertValues(values, new String[]{"A"}); + assertMap(map, new int[]{1}, new CharSequence[]{"A"}); + assertEntrySet(entrySet, new int[]{1}, new CharSequence[]{"A"}); + assertKeySet(keySet, new int[]{1}); + assertValues(values, new String[]{"A"}); - entrySet.iterator().next().setValue("B"); - assertMap(map, new int[]{1}, new CharSequence[]{"B"}); - assertEntrySet(entrySet, new int[]{1}, new CharSequence[]{"B"}); - assertEntrySet(map.entrySet(), new int[]{1}, new CharSequence[]{"B"}); - assertKeySet(keySet, new int[]{1}); - assertKeySet(map.keySet(), new int[]{1}); - assertValues(values, new String[]{"B"}); - assertValues(map.values(), new String[]{"B"}); + entrySet.iterator().next().setValue("B"); + assertMap(map, new int[]{1}, new CharSequence[]{"B"}); + assertEntrySet(entrySet, new int[]{1}, new CharSequence[]{"B"}); + assertEntrySet(map.entrySet(), new int[]{1}, new CharSequence[]{"B"}); + assertKeySet(keySet, new int[]{1}); + assertKeySet(map.keySet(), new int[]{1}); + assertValues(values, new String[]{"B"}); + assertValues(map.values(), new String[]{"B"}); - map.close(); - } - - private ChronicleMap getViewTestMap(int noOfElements) throws IOException { - ChronicleMap map = - ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .entries(noOfElements * 2 + 100) - .averageValueSize((noOfElements + "").length()) - .putReturnsNull(true) - .removeReturnsNull(true).create(); - - int[] expectedKeys = new int[noOfElements]; - String[] expectedValues = new String[noOfElements]; - for (int i = 1; i <= noOfElements; i++) { - String value = "" + i; - map.put(i, value); - expectedKeys[i - 1] = i; - expectedValues[i - 1] = value; } - return map; } + @Test public void testEntriesSpanningSeveralChunksPutRemoveReplace() throws IOException { ChronicleMapBuilder builder = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class) .entries(10) .minSegments(1) .actualChunkSize(10); - testEntriesSpanningSeveralChunksPutRemoveReplace( - (VanillaChronicleMap) - builder.createPersistedTo(getPersistenceFile()) - ); + try (VanillaChronicleMap map = + (VanillaChronicleMap) + builder.create()) { + String key = "k"; + String largeKey = "very large key"; + String value = "v"; + String largeValue = "vary large value"; + + map.put(key, largeValue); + assertEquals(largeValue, map.get(key)); + map.replace(key, value); + map.checkConsistency(); + assertEquals(value, map.get(key)); + map.replace(key, largeValue); + map.checkConsistency(); + assertEquals(largeValue, map.get(key)); + + map.put(largeKey, value); + map.checkConsistency(); + assertEquals(value, map.get(largeKey)); + map.replace(largeKey, largeValue); + map.checkConsistency(); + assertEquals(largeValue, map.get(largeKey)); + map.remove(largeKey); + map.checkConsistency(); + assertEquals(null, map.get(largeKey)); + map.put(largeKey, largeValue); + map.checkConsistency(); + assertEquals(largeValue, map.get(largeKey)); + map.replace(largeKey, largeValue, value); + map.checkConsistency(); + assertEquals(value, map.get(largeKey)); + + assertEquals(value, map.remove(largeKey)); + map.checkConsistency(); + assertEquals(largeValue, map.remove(key)); + map.checkConsistency(); + } + } - public void testEntriesSpanningSeveralChunksPutRemoveReplace( - VanillaChronicleMap map) { - String key = "k"; - String largeKey = "very large key"; - String value = "v"; - String largeValue = "vary large value"; - - map.put(key, largeValue); - assertEquals(largeValue, map.get(key)); - map.replace(key, value); - map.checkConsistency(); - assertEquals(value, map.get(key)); - map.replace(key, largeValue); - map.checkConsistency(); - assertEquals(largeValue, map.get(key)); - - map.put(largeKey, value); - map.checkConsistency(); - assertEquals(value, map.get(largeKey)); - map.replace(largeKey, largeValue); - map.checkConsistency(); - assertEquals(largeValue, map.get(largeKey)); - map.remove(largeKey); - map.checkConsistency(); - assertEquals(null, map.get(largeKey)); - map.put(largeKey, largeValue); - map.checkConsistency(); - assertEquals(largeValue, map.get(largeKey)); - map.replace(largeKey, largeValue, value); - map.checkConsistency(); - assertEquals(value, map.get(largeKey)); - - assertEquals(value, map.remove(largeKey)); - map.checkConsistency(); - assertEquals(largeValue, map.remove(key)); - map.checkConsistency(); - - map.close(); - } @Test public void equalsTest() throws IOException { - final ChronicleMap map1 = ChronicleMapBuilder - .of(Integer.class, String.class).create(); + try (final ChronicleMap map1 = ChronicleMapBuilder + .of(Integer.class, String.class).create()) { - map1.put(1, "one"); - map1.put(2, "two"); + map1.put(1, "one"); + map1.put(2, "two"); - final ChronicleMap map2 = ChronicleMapBuilder - .of(Integer.class, String.class).create(); + try (final ChronicleMap map2 = ChronicleMapBuilder + .of(Integer.class, String.class).create()) { + + map2.put(1, "one"); + map2.put(2, "two"); + + assertEquals(map1, map2); + } + } - map2.put(1, "one"); - map2.put(2, "two"); - assertEquals(map1, map2); } private static final class IncrementRunnable implements Runnable { @@ -1632,6 +1777,7 @@ public class ChronicleMapTest { e.printStackTrace(); } } + } @@ -1642,17 +1788,19 @@ public class ChronicleMapTest { .entries(1000) .averageKeySize("x".length()); - final ChronicleMap map = builder.create(); + try (final ChronicleMap map = builder.create()) { - LongValue value = nativeLongValue(); - try { - map.put("x", value); - } catch (IllegalStateException | NullPointerException e) { - // ok - return; + LongValue value = nativeLongValue(); + try { + map.put("x", value); + } catch (IllegalStateException | NullPointerException e) { + // ok + return; + } + throw new AssertionError("Should throw either IllegalStateException or " + + "NullPointerException, but succeed"); } - throw new AssertionError("Should throw either IllegalStateException or " + - "NullPointerException, but succeed"); + } @Test @@ -1662,54 +1810,54 @@ public class ChronicleMapTest { .entries(1000) .averageKeySize("one".length()); - final ChronicleMap map = builder.create(); + try (final ChronicleMap map = builder.create()) { - LongValue value = nativeLongValue(); + LongValue value = nativeLongValue(); + + // this will add the entry + try (WriteContext context = map.acquireUsingLocked("one", value)) { + assertEquals(0, context.value().getValue()); + assert value == context.value(); + LongValue value1 = context.value(); + value1.addValue(1); + } + + // check that the entry was added + try (ReadContext context = map.getUsingLocked("one", value)) { + assert value == context.value(); + assertEquals(true, context.present()); + assertEquals(1, context.value().getValue()); + } + + // this will remove the entry + try (WriteContext context = map.acquireUsingLocked("one", value)) { + assert value == context.value(); + context.removeEntry(); + } + + // check that the entry was removed + try (ReadContext context = map.getUsingLocked("one", value)) { + assertEquals(false, context.present()); + assertEquals(null, context.value()); + } + + try (WriteContext context = map.acquireUsingLocked("one", value)) { + assert value == context.value(); + assertEquals(0, context.value().getValue()); + } + + try (WriteContext context = map.acquireUsingLocked("one", value)) { + assert value == context.value(); + context.value().addValue(1); + } + + // check that the entry was removed + try (ReadContext context = map.getUsingLocked("one", value)) { + assert value == context.value(); + assertEquals(1, context.value().getValue()); + } - // this will add the entry - try (WriteContext context = map.acquireUsingLocked("one", value)) { - assertEquals(0, context.value().getValue()); - assert value == context.value(); - LongValue value1 = context.value(); - value1.addValue(1); } - - // check that the entry was added - try (ReadContext context = map.getUsingLocked("one", value)) { - assert value == context.value(); - assertEquals(true, context.present()); - assertEquals(1, context.value().getValue()); - } - - // this will remove the entry - try (WriteContext context = map.acquireUsingLocked("one", value)) { - assert value == context.value(); - context.removeEntry(); - } - - // check that the entry was removed - try (ReadContext context = map.getUsingLocked("one", value)) { - assertEquals(false, context.present()); - assertEquals(null, context.value()); - } - - try (WriteContext context = map.acquireUsingLocked("one", value)) { - assert value == context.value(); - assertEquals(0, context.value().getValue()); - } - - try (WriteContext context = map.acquireUsingLocked("one", value)) { - assert value == context.value(); - context.value().addValue(1); - } - - // check that the entry was removed - try (ReadContext context = map.getUsingLocked("one", value)) { - assert value == context.value(); - assertEquals(1, context.value().getValue()); - } - - map.close(); } @@ -1718,7 +1866,6 @@ public class ChronicleMapTest { TcpTransportAndNetworkConfig serverConfig = TcpTransportAndNetworkConfig.of(8877) .name("serverMap"); - File mapFile = getPersistenceFile(); // this test only appear to fail when we reuse the mapFile for (int i = 0; i < 2; i++) { @@ -1726,7 +1873,7 @@ public class ChronicleMapTest { try (ChronicleMap server = ChronicleMapBuilder.of(byte[].class, byte[][].class) .replication((byte) 1, serverConfig) .constantKeySizeBySample(new byte[14]) - .createPersistedTo(mapFile)) { + .create()) { try (ChronicleMap map2 = localClient(8877)) { @@ -1785,70 +1932,71 @@ public class ChronicleMapTest { @Test public void testOnheapAcquireUsingLocked() throws IOException { - ChronicleMapBuilder builder = ChronicleMapBuilder - .of(CharSequence.class, LongValue.class) - .entries(1000) - .averageKeySize("one".length()); File tmpFile = File.createTempFile("testAcquireUsingLocked", ".deleteme"); tmpFile.deleteOnExit(); - final ChronicleMap map = builder.createPersistedTo(tmpFile); + try (final ChronicleMap map = ChronicleMapBuilder + .of(CharSequence.class, LongValue.class) + .entries(1000) + .averageKeySize("one".length()).createPersistedTo(tmpFile)) { - LongValue value = DataValueClasses.newDirectReference(LongValue.class); + LongValue value = DataValueClasses.newDirectReference(LongValue.class); - try (ReadContext context = map.getUsingLocked("one", value)) { - assertEquals(false, context.present()); - assertEquals(null, context.value()); + try (ReadContext context = map.getUsingLocked("one", value)) { + assertEquals(false, context.present()); + assertEquals(null, context.value()); + } + + try (WriteContext context = map.acquireUsingLocked("one", value)) { + value.setValue(10); + } + + // this will add the entry + try (WriteContext context = map.acquireUsingLocked("one", value)) { + LongValue value1 = context.value(); + value1.addValue(1); + } + + // check that the entry was added + try (ReadContext context = map.getUsingLocked("one", value)) { + assertEquals(true, context.present()); + assertEquals(11, context.value().getValue()); + } + + // this will remove the entry + try (WriteContext context = map.acquireUsingLocked("one", value)) { + context.removeEntry(); + } + + // check that the entry was removed + try (ReadContext context = map.getUsingLocked("one", value)) { + assertEquals(false, context.present()); + assertEquals(null, context.value()); + } + + try (WriteContext context = map.acquireUsingLocked("one", value)) { + assertEquals(0, context.value().getValue()); + } + + value.setValue(1); + + try (ReadContext lockedEntry = map.getUsingLocked("one", value)) { + assertEquals(1, lockedEntry.value().getValue()); + } + + try (WriteContext context = map.acquireUsingLocked("one", value)) { + LongValue value1 = context.value(); + value1.addValue(1); + } + + // check that the entry was removed + try (ReadContext context = map.getUsingLocked("one", value)) { + LongValue value1 = context.value(); + assertEquals(2, value1.getValue()); + } } - - try (WriteContext context = map.acquireUsingLocked("one", value)) { - value.setValue(10); - } - - // this will add the entry - try (WriteContext context = map.acquireUsingLocked("one", value)) { - LongValue value1 = context.value(); - value1.addValue(1); - } - - // check that the entry was added - try (ReadContext context = map.getUsingLocked("one", value)) { - assertEquals(true, context.present()); - assertEquals(11, context.value().getValue()); - } - - // this will remove the entry - try (WriteContext context = map.acquireUsingLocked("one", value)) { - context.removeEntry(); - } - - // check that the entry was removed - try (ReadContext context = map.getUsingLocked("one", value)) { - assertEquals(false, context.present()); - assertEquals(null, context.value()); - } - - try (WriteContext context = map.acquireUsingLocked("one", value)) { - assertEquals(0, context.value().getValue()); - } - - value.setValue(1); - - try (ReadContext lockedEntry = map.getUsingLocked("one", value)) { - assertEquals(1, lockedEntry.value().getValue()); - } - - try (WriteContext context = map.acquireUsingLocked("one", value)) { - LongValue value1 = context.value(); - value1.addValue(1); - } - - // check that the entry was removed - try (ReadContext context = map.getUsingLocked("one", value)) { - LongValue value1 = context.value(); - assertEquals(2, value1.getValue()); - } - - map.close(); + tmpFile.delete(); } + } + diff --git a/src/test/java/net/openhft/chronicle/map/DemoChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/DemoChronicleMapTest.java index fb5013777..08c35fc09 100755 --- a/src/test/java/net/openhft/chronicle/map/DemoChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/DemoChronicleMapTest.java @@ -3,7 +3,6 @@ package net.openhft.chronicle.map; import net.openhft.lang.model.DataValueClasses; import net.openhft.lang.model.constraints.MaxSize; import net.openhft.lang.values.IntValue; -import org.junit.Ignore; import org.junit.Test; import java.io.File; @@ -51,6 +50,8 @@ public class DemoChronicleMapTest { assertNotNull(v); } } + + file.delete(); } @Test @@ -93,6 +94,7 @@ public class DemoChronicleMapTest { assertNotNull(v); } } + file.delete(); } } diff --git a/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java b/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java index 0b8852488..62ded4aa4 100644 --- a/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/EntryCountMapTest.java @@ -190,17 +190,26 @@ public class EntryCountMapTest { Random random = new Random(seed); int counter = minSize + random.nextInt(9999 + maxSize); final int stride = 1 + random.nextInt(100); - int maxKeySize = "key:".length() + + final int maxKeySize = "key:".length() + (int) round(log10(moreThanMaxSize(maxSize) * stride + counter)) + 1; - final ChronicleMap map = - getSharedMap(minSize, segments, maxKeySize); + + return es.submit(new Callable() { @Override public Void call() throws Exception { - testEntriesMaxSize0(segments, minSize, maxSize, seed, stride, map); - return null; + File f = null; + try (final ChronicleMap map = + getSharedMap(minSize, segments, maxKeySize)) { + f = map.file(); + testEntriesMaxSize0(segments, minSize, maxSize, seed, stride, map); + } finally { + if (f != null && f.exists()) + f.delete(); + return null; + } } }); + } void testEntriesMaxSize(int segments, int minSize, int maxSize, int seed) throws IOException { @@ -210,8 +219,12 @@ public class EntryCountMapTest { int stride = 1 + random.nextInt(100); int maxKeySize = "key:".length() + (int) round(log10(moreThanMaxSize(maxSize) * stride + counter)) + 1; - ChronicleMap map = getSharedMap(minSize, segments, maxKeySize); - testEntriesMaxSize0(segments, minSize, maxSize, counter, stride, map); + File file; + try (ChronicleMap map = getSharedMap(minSize, segments, maxKeySize)) { + file = map.file(); + testEntriesMaxSize0(segments, minSize, maxSize, counter, stride, map); + } + file.delete(); } void testEntriesMaxSize0(int segments, int minSize, int maxSize, int counter, int stride, diff --git a/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java b/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java index 00ee3ac75..1e83c5d33 100644 --- a/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java +++ b/src/test/java/net/openhft/chronicle/map/LargeEntriesTest.java @@ -41,7 +41,6 @@ import static org.junit.Assert.*; */ public class LargeEntriesTest { - @Ignore("Issues with TC") @Test public void testLargeStrings() throws ExecutionException, InterruptedException, IOException { final int ENTRIES = 250; @@ -49,7 +48,7 @@ public class LargeEntriesTest { File file = File.createTempFile("largeEntries" + System.currentTimeMillis(), ".deleteme"); file.deleteOnExit(); - final ChronicleMap map = ChronicleMapBuilder + try (final ChronicleMap map = ChronicleMapBuilder .of(String.class, String.class) .valueMarshaller(SnappyStringMarshaller.INSTANCE) .actualSegments(1) // to force an error. @@ -57,37 +56,38 @@ public class LargeEntriesTest { .averageKeySize(10) .averageValueSize(ENTRY_SIZE) .putReturnsNull(true) - .createPersistedTo(file); - { - warmUpCompression(ENTRY_SIZE); - int threads = 4; //Runtime.getRuntime().availableProcessors(); - ExecutorService es = Executors.newFixedThreadPool(threads); - final int block = ENTRIES / threads; - for (int i = 0; i < 3; i++) { - long start = System.currentTimeMillis(); - List> futureList = new ArrayList<>(); - for (int t = 0; t < threads; t++) { - final int finalT = t; - futureList.add(es.submit(new Runnable() { - @Override - public void run() { - exerciseLargeStrings(map, finalT * block, finalT * block + block, - ENTRY_SIZE); - } - })); + .createPersistedTo(file)) { + { + warmUpCompression(ENTRY_SIZE); + int threads = 4; //Runtime.getRuntime().availableProcessors(); + ExecutorService es = Executors.newFixedThreadPool(threads); + final int block = ENTRIES / threads; + for (int i = 0; i < 3; i++) { + long start = System.currentTimeMillis(); + List> futureList = new ArrayList<>(); + for (int t = 0; t < threads; t++) { + final int finalT = t; + futureList.add(es.submit(new Runnable() { + @Override + public void run() { + exerciseLargeStrings(map, finalT * block, finalT * block + block, + ENTRY_SIZE); + } + })); + } + for (Future future : futureList) { + future.get(); + } + long time = System.currentTimeMillis() - start; + long operations = 3; + System.out.printf("Put/Get rate was %.1f MB/s%n", + operations * ENTRIES * ENTRY_SIZE / 1e6 / (time / 1e3)); } - for (Future future : futureList) { - future.get(); - } - long time = System.currentTimeMillis() - start; - long operations = 3; - System.out.printf("Put/Get rate was %.1f MB/s%n", - operations * ENTRIES * ENTRY_SIZE / 1e6 / (time / 1e3)); + es.shutdown(); + } - es.shutdown(); - if (es.isTerminated()) - map.close(); } + file.delete(); } private void warmUpCompression(int entrySize) { diff --git a/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java b/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java index a3bf2b922..e9b6fbc76 100644 --- a/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java +++ b/src/test/java/net/openhft/chronicle/map/StatelessClientTest.java @@ -24,7 +24,6 @@ import net.openhft.lang.io.ByteBufferBytes; import net.openhft.lang.io.Bytes; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -151,8 +150,7 @@ public class StatelessClientTest { } } - // TODO ignore why? - @Ignore + @Test(timeout = 10000) public void testBufferOverFlowPutAllAndEntrySet() throws IOException, InterruptedException { int port = s_port++; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java index d68078dd9..adbfe3872 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationBootStrapTest.java @@ -55,8 +55,9 @@ public class TCPSocketReplicationBootStrapTest { ChronicleMapBuilder map2aBuilder = newTcpSocketShmBuilder(Integer.class, CharSequence.class, (byte) 2, 8092, new InetSocketAddress("localhost", 8091)); + File persistenceFile = getPersistenceFile(); final ChronicleMap map2a = - map2aBuilder.createPersistedTo(getPersistenceFile()); + map2aBuilder.createPersistedTo(persistenceFile); map2a.put(10, "EXAMPLE-10"); // this will be the last time that map1 go an update from map2 long lastModificationTime; @@ -85,6 +86,9 @@ public class TCPSocketReplicationBootStrapTest { // add data into it waitTillEqual(5000); assertEquals("ADDED WHEN DISCONNECTED TO MAP1", map1.get(11)); + + map2.file().delete(); + persistenceFile.delete(); } @Test diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java index a075907ad..ffa7090a0 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java @@ -24,7 +24,6 @@ import net.openhft.lang.model.DataValueClasses; import net.openhft.lang.values.IntValue; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import java.io.Closeable; @@ -34,7 +33,6 @@ import java.nio.ByteBuffer; import java.util.Random; import java.util.Set; -import static net.openhft.chronicle.map.Builder.getPersistenceFile; import static net.openhft.chronicle.map.Builder.newTcpSocketShmBuilder; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -59,7 +57,7 @@ public class TCPSocketReplicationIntValueTest { ((Byteable) value).bytes(new ByteBufferBytes(ByteBuffer.allocateDirect(4)), 0); map1Builder = newTcpSocketShmBuilder(IntValue.class, CharSequence.class, (byte) 1, s_port, new InetSocketAddress("localhost", s_port + 1)); - map1 = map1Builder.createPersistedTo(getPersistenceFile()); + map1 = map1Builder.create(); map2 = newTcpSocketShmBuilder(IntValue.class, CharSequence.class, (byte) 2, s_port + 1) .entries(Builder.SIZE) @@ -72,7 +70,9 @@ public class TCPSocketReplicationIntValueTest { for (final Closeable closeable : new Closeable[]{map1, map2}) { try { + closeable.close(); + } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java b/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java index 1bee40e87..6d91c0fc9 100644 --- a/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java @@ -25,7 +25,6 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; -import java.io.File; import java.io.IOException; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -39,12 +38,6 @@ public class TimeBasedReplicationTest extends JSR166TestCase { public static final byte IDENTIFIER = 1; - private static File getPersistenceFile() { - String TMP = System.getProperty("java.io.tmpdir"); - File file = new File(TMP + "/chm-test" + System.nanoTime()); - file.deleteOnExit(); - return file; - } Set threads; diff --git a/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java b/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java index 2ef6178ef..a2b88fe74 100644 --- a/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java @@ -78,7 +78,7 @@ public class UDPSocketReplicationTest { } @Test - @Ignore + @Ignore("just used for manual testing") public void testBufferOverflow() throws IOException, InterruptedException { for (int i = 0; i < 1024; i++) { diff --git a/src/test/java/net/openhft/chronicle/map/jsr166/map/MapCheck.java b/src/test/java/net/openhft/chronicle/map/jsr166/map/MapCheck.java index 9682814f7..6e1b035c3 100644 --- a/src/test/java/net/openhft/chronicle/map/jsr166/map/MapCheck.java +++ b/src/test/java/net/openhft/chronicle/map/jsr166/map/MapCheck.java @@ -111,12 +111,6 @@ public class MapCheck { serTest(size); } - private static File getPersistenceFile() { - String TMP = System.getProperty("java.io.tmpdir"); - File file = new File(TMP + "/chm-test" + System.nanoTime()); - file.deleteOnExit(); - return file; - } static Map newMap() { try { diff --git a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java index 5f7c21f1a..f5c7d5d4f 100755 --- a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java @@ -308,7 +308,7 @@ public class StatelessChronicleMapTest extends JSR166TestCase { static int s_port = 11050; @Test(timeout = 30000) - @Ignore + @Ignore("performance test") public void testByteArrayPerf() throws IOException { ByteBuffer key = ByteBuffer.allocate(8); ByteBuffer value = ByteBuffer.allocate(50);