From 25a3f42477169a50bb75dd870000ec391acb725d Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Fri, 10 Oct 2014 13:45:34 +0100 Subject: [PATCH] HCOLL-164 removed QueueReplicator as is was only used for testing and on profiling leaving a lot of threads handing around, and we don't use it anyway, so simpler to just delete it. --- .../net/openhft/chronicle/map/Builder.java | 51 - .../map/MultiMapTimeBaseReplicationTest.java | 278 ------ .../chronicle/map/QueueReplicator.java | 258 ----- .../map/ReplicatedChronicleMapTest2.java | 892 ------------------ 4 files changed, 1479 deletions(-) delete mode 100644 src/test/java/net/openhft/chronicle/map/MultiMapTimeBaseReplicationTest.java delete mode 100644 src/test/java/net/openhft/chronicle/map/QueueReplicator.java delete mode 100644 src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest2.java diff --git a/src/test/java/net/openhft/chronicle/map/Builder.java b/src/test/java/net/openhft/chronicle/map/Builder.java index f0adb7e24..0f03b4516 100644 --- a/src/test/java/net/openhft/chronicle/map/Builder.java +++ b/src/test/java/net/openhft/chronicle/map/Builder.java @@ -17,12 +17,10 @@ package net.openhft.chronicle.map; -import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.net.InetSocketAddress; -import java.util.concurrent.ArrayBlockingQueue; import static java.util.concurrent.TimeUnit.SECONDS; import static net.openhft.chronicle.map.Replicators.tcp; @@ -58,55 +56,6 @@ public class Builder { } - static ChronicleMap newShm( - int size, final ArrayBlockingQueue input, - final ArrayBlockingQueue output, - final byte localIdentifier, byte externalIdentifier, - Class kClass, Class vClass) throws IOException { - Replicator queue = QueueReplicator.of(localIdentifier, externalIdentifier, input, output); - return ChronicleMapBuilder.of(kClass, vClass) - .entries(size) - .addReplicator(queue) - .create(getPersistenceFile()); - } - - static MapProvider> newShmIntInt( - int size, final ArrayBlockingQueue input, - final ArrayBlockingQueue output, - final byte localIdentifier, byte externalIdentifier) throws IOException { - - Replicator queue = QueueReplicator.of(localIdentifier, externalIdentifier, input, output); - final ReplicatedChronicleMap result = - (ReplicatedChronicleMap) ChronicleMapBuilder - .of(Integer.class, Integer.class) - .entries(size) - .addReplicator(queue) - .create(getPersistenceFile()); - QueueReplicator q = null; - for (Closeable closeable : result.closeables) { - if (closeable instanceof QueueReplicator) { - q = (QueueReplicator) closeable; - break; - } - } - if (q == null) - throw new AssertionError(); - final QueueReplicator finalQ = q; - return new MapProvider>() { - - @Override - public ReplicatedChronicleMap getMap() { - return result; - } - - @Override - public boolean isQueueEmpty() { - return finalQ.isEmpty(); - } - - }; - } - public static > T newTcpSocketShmIntString( final byte identifier, final int serverPort, diff --git a/src/test/java/net/openhft/chronicle/map/MultiMapTimeBaseReplicationTest.java b/src/test/java/net/openhft/chronicle/map/MultiMapTimeBaseReplicationTest.java deleted file mode 100644 index 0458e408a..000000000 --- a/src/test/java/net/openhft/chronicle/map/MultiMapTimeBaseReplicationTest.java +++ /dev/null @@ -1,278 +0,0 @@ -/* - * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.openhft.chronicle.map; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - -import java.io.IOException; -import java.util.concurrent.ArrayBlockingQueue; - -import static net.openhft.chronicle.map.Replica.ModificationNotifier.NOP; -import static org.junit.Assert.assertEquals; - -/** - * @author Rob Austin. - */ -public class MultiMapTimeBaseReplicationTest { - - private Replica.ModificationIterator segmentModificationIterator2; - private Replica.ModificationIterator segmentModificationIterator1; - private Builder.MapProvider> mapP1; - private Builder.MapProvider> mapP2; - private ReplicatedChronicleMap map2; - private ReplicatedChronicleMap map1; - - - private ArrayBlockingQueue map1ToMap2; - private ArrayBlockingQueue map2ToMap1; - - @Before - public void setup() throws IOException { - final TimeProvider timeProvider = Mockito.mock(TimeProvider.class); - - map1ToMap2 = new ArrayBlockingQueue(10000); - map2ToMap1 = new ArrayBlockingQueue(10000); - - mapP1 = Builder.newShmIntInt(20000, map2ToMap1, map1ToMap2, (byte) 1, (byte) 2); - map1 = mapP1.getMap(); - segmentModificationIterator1 = map1.acquireModificationIterator((byte) 2, NOP); - - mapP2 = Builder.newShmIntInt(20000, map1ToMap2, map2ToMap1, (byte) 2, (byte) 1); - map2 = mapP2.getMap(); - - segmentModificationIterator2 = map2.acquireModificationIterator((byte) 1, NOP); - - Mockito.when(timeProvider.currentTimeMillis()).thenReturn((long) 1); - } - - - @Test - public void testPut2Remove2Remove2Put1() throws IOException, InterruptedException { - - map2.put(1, 1, (byte) 2, 1399459457425L); - map2.remove(1, null, (byte) 2, 1399459457425L); - map2.remove(1, null, (byte) 2, 1399459457426L); - // waitTillFinished0(); - map1.put(1, 895, (byte) 1, 1399459457426L); - - - // we will check 10 times that there all the work queues are empty - waitTillEqual(5000); - - assertEquals(map1, map2); - - } - - - @Test - public void testPut1Remove2Remove2() throws IOException, InterruptedException { - - map1.put(1, 895, (byte) 1, 1399459457425L); - waitTillFinished0(); - map2.remove(1, null, (byte) 2, 1399459457425L); - map2.remove(1, null, (byte) 2, 1399459457426L); - - - // we will check 10 times that there all the work queues are empty - waitTillEqual(5000); - assertEquals(map1, map2); - - } - - - @Test - public void testRemove1put2() throws IOException, InterruptedException { - - map2.remove(1, null, (byte) 2, 1399459457425L); - waitTillFinished0(); - map1.put(1, 895, (byte) 1, 1399459457425L); - - // we will check 10 times that there all the work queues are empty - waitTillEqual(5000); - - - assertEquals(map1, map2); - } - - - @Test - public void testRemove1put2Flip() throws IOException, InterruptedException { - - map1.remove(1, null, (byte) 1, 1399459457425L); - waitTillFinished0(); - map2.put(1, 895, (byte) 2, 1399459457425L); - - // we will check 10 times that there all the work queues are empty - waitTillEqual(5000); - - - assertEquals(map1, map2); - } - - - @Test - public void testPut1Put1CrazyTimes() throws IOException, InterruptedException { - - map1.put(1, 894, (byte) 1, 1399459457425L); - waitTillFinished0(); - map2.put(1, 895, (byte) 2, 0L); - - // we will check 10 times that there all the work queues are empty - waitTillEqual(5000); - - - assertEquals(map1, map2); - } - - - @Test - public void testPut1Put1SameTimes() throws IOException, InterruptedException { - - map1.put(1, 10, (byte) 1, 0L); - waitTillFinished0(); - map2.put(1, 20, (byte) 2, 0L); - - // we will check 10 times that there all the work queues are empty - waitTillEqual(5000); - - - assertEquals(map1, map2); - } - - @Test - public void testPut1Put1SameTimesFlip() throws IOException, InterruptedException { - - map2.put(1, 894, (byte) 2, 0L); - waitTillFinished0(); - map1.put(1, 895, (byte) 1, 0L); - - // we will check 10 times that there all the work queues are empty - waitTillEqual(5000); - - - assertEquals(map1, map2); - } - - @Test - public void testPut1Put1CrazyTimesMapFlip() throws IOException, InterruptedException { - - map2.put(1, 894, (byte) 2, 1399459457425L); - waitTillFinished0(); - map1.put(1, 895, (byte) 1, 0L); - - // we will check 10 times that there all the work queues are empty - waitTillEqual(5000); - - - assertEquals(map1, map2); - } - - - @Test - public void testRemovePut() throws IOException, InterruptedException { - - - map2.remove(1, null, (byte) 2, 1399459457425L); - waitTillFinished0(); - map1.put(1, 1, (byte) 1, 1399459457425L); - - waitTillEqual(5000); - - assertEquals(map1, map2); - - } - - - @Test - public void testRemovePutFlip() throws IOException, InterruptedException { - - - map1.remove(1, null, (byte) 1, 1399459457425L); - waitTillFinished0(); - map2.put(1, 1, (byte) 2, 1399459457425L); - waitTillEqual(5000); - // we will check 10 times that there all the work queues are empty - - assertEquals(map1, map2); - - - } - - - @Test - public void testPutRemove() throws IOException, InterruptedException { - - - map2.put(1, 1, (byte) 2, 1399459457425L); - waitTillFinished0(); - map1.remove(1, null, (byte) 1, 1399459457425L); - waitTillEqual(5000); - // we will check 10 times that there all the work queues are empty - - assertEquals(map1, map2); - - } - - - @Test - public void testPutRemoveFilp() throws IOException, InterruptedException { - - - map1.put(1, 1, (byte) 1, 1399459457425L); - waitTillFinished0(); - map2.remove(1, null, (byte) 2, 1399459457425L); - waitTillEqual(5000); - // we will check 10 times that there all the work queues are empty - - assertEquals(map1, map2); - - } - - - private void waitTillFinished0() throws InterruptedException { - int i = 0; - for (; i < 2; i++) { - if (!(map2ToMap1.isEmpty() && map2ToMap1.isEmpty() && - !segmentModificationIterator1.hasNext() && - !segmentModificationIterator2.hasNext() && - mapP1.isQueueEmpty() && mapP2.isQueueEmpty())) { - i = 0; - } - Thread.sleep(1); - } - } - - /** - * waits until map1 and map2 show the same value - * - * @param timeOutMs timeout in milliseconds - * @throws InterruptedException - */ - private void waitTillEqual(final int timeOutMs) throws InterruptedException { - int t = 0; - for (; t < timeOutMs; t++) { - waitTillFinished0(); - if (map1.equals(map2)) - break; - Thread.sleep(1); - } - } - - -} diff --git a/src/test/java/net/openhft/chronicle/map/QueueReplicator.java b/src/test/java/net/openhft/chronicle/map/QueueReplicator.java deleted file mode 100644 index fa0fe9468..000000000 --- a/src/test/java/net/openhft/chronicle/map/QueueReplicator.java +++ /dev/null @@ -1,258 +0,0 @@ -/* - * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.openhft.chronicle.map; - -import net.openhft.lang.io.ByteBufferBytes; -import net.openhft.lang.io.Bytes; -import org.jetbrains.annotations.NotNull; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.Closeable; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; -import java.util.concurrent.atomic.AtomicBoolean; - -import static java.lang.System.arraycopy; -import static net.openhft.chronicle.map.Replica.EntryExternalizable; -import static net.openhft.chronicle.map.Replica.ModificationIterator; -import static net.openhft.chronicle.map.Replica.ModificationNotifier.NOP; - -/** - * This class replicates data from one ReplicatedShareHashMap to another using a queue it was originally - * written to test the logic in the {@code ReplicatedChronicleMap} - * - * @author Rob Austin. - */ - -public class QueueReplicator implements Closeable { - - public static final short MAX_NUMBER_OF_ENTRIES_PER_CHUNK = 10; - private static final Logger LOG = LoggerFactory.getLogger(ReplicatedChronicleMap.class); - private final AtomicBoolean isWritingEntry = new AtomicBoolean(true); - private final AtomicBoolean isReadingEntry = new AtomicBoolean(true); - private final ByteBufferBytes entryBuffer; - private ByteBufferBytes buffer; - - public QueueReplicator(@NotNull final ModificationIterator modificationIterator, - @NotNull final BlockingQueue input, - @NotNull final BlockingQueue output, - final int entrySize, - @NotNull final EntryExternalizable externalizable) { - - //todo HCOLL-71 fix the 128 padding - final int entrySize0 = entrySize + 128; - entryBuffer = new ByteBufferBytes(ByteBuffer.allocateDirect(entrySize0)); - - // in bound - Executors.newSingleThreadExecutor(new ThreadFactory() { - - @Override - public Thread newThread(Runnable r) { - final Thread thread = new Thread(r, "reader-map" + externalizable); - thread.setDaemon(true); - return thread; - } - - }).execute(new Runnable() { - - @Override - public void run() { - - // this is used in nextEntry() below, its what could be described as callback method - - try { - - for (; ; ) { - - byte[] item = null; - try { - - - for (; ; ) { - isReadingEntry.set(true); - item = input.poll(); - if (item == null) { - isReadingEntry.set(false); - Thread.sleep(1); - } else { - break; - } - - } - - final ByteBufferBytes bufferBytes = new ByteBufferBytes(ByteBuffer.wrap(item)); - - while (bufferBytes.remaining() > 0) { - - final long entrySize = bufferBytes.readStopBit(); - - final long position = bufferBytes.position(); - final long limit = bufferBytes.limit(); - - bufferBytes.limit(position + entrySize); - externalizable.readExternalEntry(bufferBytes); - - bufferBytes.position(position); - bufferBytes.limit(limit); - - // skip onto the next entry - bufferBytes.skip(entrySize); - - } - isReadingEntry.set(false); - - } catch (InterruptedException e1) { - LOG.warn("", e1); - } - - } - } catch (Exception e) { - LOG.warn("", e); - } - - } - - }); - - // out bound - Executors.newSingleThreadExecutor().execute(new Runnable() { - - - @Override - public void run() { - - buffer = new ByteBufferBytes(ByteBuffer.allocate(entrySize0 * MAX_NUMBER_OF_ENTRIES_PER_CHUNK)); - - // this is used in nextEntry() below, its what could be described as callback method - final Replica.EntryCallback entryCallback = - new Replica.EntryCallback() { - @Override - public boolean onEntry(Bytes entry, final int chronicleId) { - - entryBuffer.clear(); - externalizable.writeExternalEntry(entry, entryBuffer, chronicleId); - - if (entryBuffer.position() == 0) - return false; - - // write the entry len - buffer.writeStopBit(entryBuffer.position()); - - // write the entry - entryBuffer.flip(); - buffer.write(entryBuffer); - - return true; - } - - @Override - public void onBeforeEntry() { - isWritingEntry.set(true); - } - - }; - - try { - for (; ; ) { - - final boolean wasDataRead = modificationIterator.nextEntry(entryCallback, 0); - - if (wasDataRead) { - isWritingEntry.set(false); - } else if (buffer.position() == 0) { - isWritingEntry.set(false); - continue; - } - - if (buffer.remaining() <= entrySize0 && ((wasDataRead || buffer.position() == 0))) - continue; - - // we are going to create an byte[] so that the buffer can be copied into this. - final byte[] source = buffer.buffer().array(); - final int length = (int) buffer.position(); - final byte[] dest = new byte[length]; - - arraycopy(source, 0, dest, 0, length); - - try { - output.put(dest); - } catch (InterruptedException e1) { - LOG.warn("", e1); - break; - } - - // clear the buffer for reuse, we can store a maximum of - // MAX_NUMBER_OF_ENTRIES_PER_CHUNK in this buffer - buffer.clear(); - - } - } catch (Exception e) { - LOG.warn("", e); - } - } - - - }); - - - } - - public static Replicator of(final byte localIdentifier, - final byte externalIdentifier, - @NotNull final BlockingQueue input, - @NotNull final BlockingQueue output) { - return new Replicator() { - boolean used = false; - - @Override - public byte identifier() { - return localIdentifier; - } - - @Override - protected Closeable applyTo(ChronicleMapBuilder builder, - Replica map, - EntryExternalizable entryExternalizable) - throws IOException { - if (used) - throw new IllegalStateException(); - used = true; - return new QueueReplicator( - map.acquireModificationIterator(externalIdentifier, NOP), - input, output, builder.entrySize(), entryExternalizable); - } - }; - } - - /** - * @return true indicates that all the data has been processed ( it lock free so can not be relied upon ) - */ - public boolean isEmpty() { - final boolean b = isWritingEntry.get() || isReadingEntry.get(); - return !b && buffer.position() == 0; - } - - @Override - public void close() throws IOException { - // do nothing - } -} - diff --git a/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest2.java b/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest2.java deleted file mode 100644 index 90e6aa327..000000000 --- a/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest2.java +++ /dev/null @@ -1,892 +0,0 @@ -/* - * Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.openhft.chronicle.map; - -import net.openhft.chronicle.map.jrs166.JSR166TestCase; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -import java.io.File; -import java.io.IOException; -import java.util.*; -import java.util.concurrent.ArrayBlockingQueue; - -import static org.junit.Assert.*; - -/* - * Originally written by Doug Lea with assistance from members of JCP JSR-166 - * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/publicdomain/zero/1.0/ - * Other contributors include Andrew Wright, Jeffrey Hayes, - * Pat Fisher, Mike Judd. Then modified by the Open HFT team. - */ - -@RunWith(value = Parameterized.class) -public class ReplicatedChronicleMapTest2 extends JSR166TestCase { - - boolean canReplicate; - - public ReplicatedChronicleMapTest2(Boolean canReplicate) { - this.canReplicate = canReplicate; - } - - @Parameterized.Parameters - public static Collection data() { - - return Arrays.asList(new Object[][]{ - { - Boolean.TRUE - } - - }); - } - - private static File getPersistenceFile() { - String TMP = System.getProperty("java.io.tmpdir"); - File file = new File(TMP + "/chm-test" + System.nanoTime()); - file.deleteOnExit(); - return file; - } - - ChronicleMap newShmIntString(int size) throws IOException { - - final ArrayBlockingQueue map1ToMap2 = new ArrayBlockingQueue(100); - final ArrayBlockingQueue map2ToMap1 = new ArrayBlockingQueue(100); - - final ChronicleMap map1 = - Builder.newShm(size, map1ToMap2, map2ToMap1, (byte) 1, (byte) 2, - Integer.class, CharSequence.class); - final ChronicleMap map2 = - Builder.newShm(size, map2ToMap1, map1ToMap2, (byte) 2, (byte) 1, - Integer.class, CharSequence.class); - - return new ReplicationCheckingMap(map1, map2); - - - } - - - ChronicleMap newShmStringString(int size) throws IOException { - - final ArrayBlockingQueue map1ToMap2 = new ArrayBlockingQueue(100); - final ArrayBlockingQueue map2ToMap1 = new ArrayBlockingQueue(100); - - final ChronicleMap map1 = - Builder.newShm(size, map1ToMap2, map2ToMap1, (byte) 1, (byte) 2, - CharSequence.class, CharSequence.class); - final ChronicleMap map2 = - Builder.newShm(size, map2ToMap1, map1ToMap2, (byte) 2, (byte) 1, - CharSequence.class, CharSequence.class); - - return new ReplicationCheckingMap(map1, map2); - - } - - - ChronicleMap newShmIntString() throws IOException { - return ChronicleMapBuilder.of(Integer.class, CharSequence.class) - - .forceReplicatedImpl() - .create(getPersistenceFile()); - } - - - /** - * Returns a new map from Integers 1-5 to Strings "A"-"E". - */ - private ChronicleMap map5() throws IOException { - ChronicleMap map = newShmIntString(5); - assertTrue(map.isEmpty()); - map.put(JSR166TestCase.one, "A"); - map.put(JSR166TestCase.two, "B"); - map.put(JSR166TestCase.three, "C"); - map.put(JSR166TestCase.four, "D"); - map.put(JSR166TestCase.five, "E"); - assertFalse(map.isEmpty()); - assertEquals(5, map.size()); - return map; - } - - /** - * clear removes all pairs - */ - @Test - public void testClear() throws IOException { - ChronicleMap map = map5(); - map.clear(); - assertEquals(0, map.size()); - } - - /** - * clear removes all pairs - */ - @Test - public void testKeySetRemove() throws IOException { - ChronicleMap map = map5(); - - System.out.print("map.keySet()=" + map.keySet()); - for (Object k : map.keySet()) { - map.remove(k); - } - - - assertEquals(0, map.size()); - } - - - /** - * Maps with same contents are equal - */ - @Test - public void testEquals() throws IOException { - ChronicleMap map1 = map5(); - ChronicleMap map2 = map5(); - assertEquals(map1, map2); - assertEquals(map2, map1); - map1.clear(); - assertFalse(map1.equals(map2)); - assertFalse(map2.equals(map1)); - } - - /** - * contains returns true for contained value - */ - @Test - public void testContains() throws IOException { - ChronicleMap map = map5(); - assertTrue(map.containsValue("A")); - assertFalse(map.containsValue("Z")); - } - - /** - * containsKey returns true for contained key - */ - @Test - public void testContainsKey() throws IOException { - ChronicleMap map = map5(); - assertTrue(map.containsKey(JSR166TestCase.one)); - assertFalse(map.containsKey(JSR166TestCase.zero)); - } - - /** - * containsValue returns true for held values - */ - @Test - public void testContainsValue() throws IOException { - ChronicleMap map = map5(); - assertTrue(map.containsValue("A")); - assertFalse(map.containsValue("Z")); - } - - - /** - * Inserted elements that are subclasses of the same Comparable - * class are found. - */ - /* @Test public void testComparableFamily() throws IOException { - ChronicleMap m = - newShmBiBoolean(); - for (int i = 0; i < 1000; i++) { - assertTrue(m.put(new CI(i), true) == null); - } - for (int i = 0; i < 1000; i++) { - assertTrue(m.containsKey(new CI(i))); - assertTrue(m.containsKey(new DI(i))); - } - }*/ - - /** - * TODO : fix test - * Elements of classes with erased generic type parameters based - * on Comparable can be inserted and found. - */ - /* @Test public void testGenericComparable() throws IOException { - ChronicleMap m = - newShmBiBoolean(); - for (int i = 0; i < 1000; i++) { - BI bi = new BI(i); - BS bs = new BS(String.valueOf(i)); - LexicographicList bis = new LexicographicList(bi); - LexicographicList bss = new LexicographicList(bs); - assertTrue(m.putIfAbsent(bis, true) == null); - assertTrue(m.containsKey(bis)); - if (m.putIfAbsent(bss, true) == null) - assertTrue(m.containsKey(bss)); - assertTrue(m.containsKey(bis)); - } - for (int i = 0; i < 1000; i++) { - assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); - } - } -*/ - - /** - * Elements of non-comparable classes equal to those of classes - * with erased generic type parameters based on Comparable can be - * inserted and found. - */ - /* @Test public void testGenericComparable2() throws IOException { - ChronicleMap m = - newShmListBoolean(); - for (int i = 0; i < 1000; i++) { - m.put(new ArrayList(Collections.singleton(new BI(i))), true); - } - - for (int i = 0; i < 1000; i++) { - LexicographicList bis = new LexicographicList(new BI(i)); - assertTrue(m.containsKey(bis)); - } - } -*/ - - /** - * get returns the correct element at the given key, or null if not present - */ - @Test - public void testGet() throws IOException { - ChronicleMap map = map5(); - assertEquals("A", (String) map.get(JSR166TestCase.one)); - ChronicleMap empty = newShmIntString(); - assertNull(map.get(JSR166TestCase.notPresent)); - } - - /** - * isEmpty is true of empty map and false for non-empty - */ - @Test - public void testIsEmpty() throws IOException { - ChronicleMap empty = newShmIntString(); - ChronicleMap map = map5(); - assertTrue(empty.isEmpty()); - assertFalse(map.isEmpty()); - } - - /** - * keySet returns a Set containing all the keys - */ - @Test - public void testKeySet() throws IOException { - ChronicleMap map = map5(); - Set s = map.keySet(); - assertEquals(5, s.size()); - assertTrue(s.contains(JSR166TestCase.one)); - assertTrue(s.contains(JSR166TestCase.two)); - assertTrue(s.contains(JSR166TestCase.three)); - assertTrue(s.contains(JSR166TestCase.four)); - assertTrue(s.contains(JSR166TestCase.five)); - } - - /** - * keySet.toArray returns contains all keys - */ - @Test - public void testKeySetToArray() throws IOException { - ChronicleMap map = map5(); - Set s = map.keySet(); - Object[] ar = s.toArray(); - assertTrue(s.containsAll(Arrays.asList(ar))); - assertEquals(5, ar.length); - ar[0] = JSR166TestCase.m10; - assertFalse(s.containsAll(Arrays.asList(ar))); - } - - /** - * Values.toArray contains all values - */ - @Test - public void testValuesToArray() throws IOException { - ChronicleMap map = map5(); - Collection v = map.values(); - Object[] ar = v.toArray(); - ArrayList s = new ArrayList(Arrays.asList(ar)); - assertEquals(5, ar.length); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); - } - - /** - * TODO : enumeration returns an enumeration containing the correct - * elements - */ - /* @Test public void testEnumeration() throws IOException { - ChronicleMap map = map5(); - Enumeration e = map.elements(); - int count = 0; - while (e.hasMoreElements()) { - count++; - e.nextElement(); - } - assertEquals(5, count); - }*/ - - /** - * entrySet.toArray contains all entries - */ - @Test - public void testEntrySetToArray() throws IOException { - ChronicleMap map = map5(); - Set s = map.entrySet(); - Object[] ar = s.toArray(); - assertEquals(5, ar.length); - for (int i = 0; i < 5; ++i) { - assertTrue(map.containsKey(((Map.Entry) (ar[i])).getKey())); - assertTrue(map.containsValue(((Map.Entry) (ar[i])).getValue())); - } - } - - /** - * values collection contains all values - */ - @Test - public void testValues() throws IOException { - ChronicleMap map = map5(); - Collection s = map.values(); - assertEquals(5, s.size()); - assertTrue(s.contains("A")); - assertTrue(s.contains("B")); - assertTrue(s.contains("C")); - assertTrue(s.contains("D")); - assertTrue(s.contains("E")); - } - - /** - * TODO : keys returns an enumeration containing all the keys from the map - */ - /* @Test public void testKeys() throws IOException { - ChronicleMap map = map5(); - Enumeration e = map.keys(); - int count = 0; - while (e.hasMoreElements()) { - count++; - e.nextElement(); - } - assertEquals(5, count); - }*/ - - /** - * entrySet contains all pairs - */ - @Test - public void testEntrySet() throws IOException { - ChronicleMap map = map5(); - Set s = map.entrySet(); - assertEquals(5, s.size()); - Iterator it = s.iterator(); - while (it.hasNext()) { - Map.Entry e = (Map.Entry) it.next(); - assertTrue( - (e.getKey().equals(JSR166TestCase.one) && e.getValue().equals("A")) || - (e.getKey().equals(JSR166TestCase.two) && e.getValue().equals("B")) || - (e.getKey().equals(JSR166TestCase.three) && e.getValue().equals("C")) || - (e.getKey().equals(JSR166TestCase.four) && e.getValue().equals("D")) || - (e.getKey().equals(JSR166TestCase.five) && e.getValue().equals("E")) - ); - } - } - - /** - * putAll adds all key-value pairs from the given map - */ - @Test - public void testPutAll() throws IOException { - ChronicleMap empty = newShmIntString(); - ChronicleMap map = map5(); - empty.putAll(map); - assertEquals(5, empty.size()); - assertTrue(empty.containsKey(JSR166TestCase.one)); - assertTrue(empty.containsKey(JSR166TestCase.two)); - assertTrue(empty.containsKey(JSR166TestCase.three)); - assertTrue(empty.containsKey(JSR166TestCase.four)); - assertTrue(empty.containsKey(JSR166TestCase.five)); - } - - /** - * putIfAbsent works when the given key is not present - */ - @Test - public void testPutIfAbsent() throws IOException { - ChronicleMap map = map5(); - map.putIfAbsent(JSR166TestCase.six, "Z"); - assertTrue(map.containsKey(JSR166TestCase.six)); - } - - /** - * putIfAbsent does not add the §pair if the key is already present - */ - @Test - public void testPutIfAbsent2() throws IOException { - ChronicleMap map = map5(); - final Object z = map.putIfAbsent(JSR166TestCase.one, "Z"); - assertEquals("A", z); - } - - /** - * replace fails when the given key is not present - */ - @Test - public void testReplace() throws IOException { - ChronicleMap map = map5(); - assertNull(map.replace(JSR166TestCase.six, "Z")); - assertFalse(map.containsKey(JSR166TestCase.six)); - } - - /** - * replace succeeds if the key is already present - */ - @Test - public void testReplace2() throws IOException { - ChronicleMap map = map5(); - assertNotNull(map.replace(JSR166TestCase.one, "Z")); - assertEquals("Z", map.get(JSR166TestCase.one)); - } - - /** - * replace value fails when the given key not mapped to expected value - */ - @Test - public void testReplaceValue() throws IOException { - ChronicleMap map = map5(); - assertEquals("A", map.get(JSR166TestCase.one)); - assertFalse(map.replace(JSR166TestCase.one, "Z", "Z")); - assertEquals("A", map.get(JSR166TestCase.one)); - } - - /** - * replace value succeeds when the given key mapped to expected value - */ - @Test - public void testReplaceValue2() throws IOException { - ChronicleMap map = map5(); - assertEquals("A", map.get(JSR166TestCase.one)); - assertTrue(map.replace(JSR166TestCase.one, "A", "Z")); - assertEquals("Z", map.get(JSR166TestCase.one)); - } - - /** - * remove removes the correct key-value pair from the map - */ - @Test - public void testRemove() throws IOException { - ChronicleMap map = map5(); - map.remove(JSR166TestCase.five); - assertEquals(4, map.size()); - assertFalse(map.containsKey(JSR166TestCase.five)); - } - - /** - * remove(key,value) removes only if pair present - */ - @Test - public void testRemove2() throws IOException { - ChronicleMap map = map5(); - map.remove(JSR166TestCase.five, "E"); - assertEquals(4, map.size()); - assertFalse(map.containsKey(JSR166TestCase.five)); - map.remove(JSR166TestCase.four, "A"); - assertEquals(4, map.size()); - assertTrue(map.containsKey(JSR166TestCase.four)); - } - - /** - * size returns the correct values - */ - @Test - public void testSize() throws IOException { - ChronicleMap map = map5(); - ChronicleMap empty = newShmIntString(); - assertEquals(0, empty.size()); - assertEquals(5, map.size()); - } - - /** - * toString contains toString of elements - */ - @Test - public void testToString() throws IOException { - ChronicleMap map = map5(); - String s = map.toString(); - for (int i = 1; i <= 5; ++i) { - assertTrue(s.contains(String.valueOf(i))); - } - } - - /** - * get(null) throws NPE - */ - @Test - public void testGet_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.get(null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * containsKey(null) throws NPE - */ - @Test - public void testContainsKey_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.containsKey(null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * put(null,x) throws NPE - */ - @Test - public void testPut1_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.put(null, "whatever"); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * put(x, null) throws NPE - */ - @Test - public void testPut2_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.put(JSR166TestCase.notPresent, null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * putIfAbsent(null, x) throws NPE - */ - @Test - public void testPutIfAbsent1_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.putIfAbsent(null, "whatever"); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - // Exception tests - - /** - * TODO : Cannot create with negative capacity - */ - /* @Test public void testConstructor1() { - try { - newShmIntString(-1, 0, 1); - shouldThrow(); - } catch (IllegalArgumentException success) { - } - }*/ - - /** - * TODO : Cannot create with negative concurrency level - */ - /*@Test public void testConstructor2() { - try { - newShmIntString(1, 0, -1); - shouldThrow(); - } catch (IllegalArgumentException success) { - } - }*/ - - /** - * TODO :Cannot create with only negative capacity - */ - /* @Test public void testConstructor3() { - try { - newShmIntString(-1); - shouldThrow(); - } catch (IllegalArgumentException success) { - } - }*/ - - /** - * replace(null, x) throws NPE - */ - @Test - public void testReplace_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.replace(null, "whatever"); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * replace(null, x, y) throws NPE - */ - @Test - public void testReplaceValue_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.replace(null, "A", "whatever"); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * containsValue(null) throws NPE - */ -/* @Test public void testContainsValue_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.containsValue(null); - shouldThrow(); - } catch (NullPointerException success) { - } - }*/ - - /** - * todo : contains(null) throws NPE - */ -/* - @Test public void testContains_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.contains(null); - shouldThrow(); - } catch (NullPointerException success) { - } - } -*/ - - /** - * putIfAbsent(x, null) throws NPE - */ - @Test - public void testPutIfAbsent2_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.putIfAbsent(JSR166TestCase.notPresent, null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * replace(x, null) throws NPE - */ - @Test - public void testReplace2_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.replace(JSR166TestCase.notPresent, null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * replace(x, null, y) throws NPE - */ - @Test - public void testReplaceValue2_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.replace(JSR166TestCase.notPresent, null, "A"); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * replace(x, y, null) throws NPE - */ - @Test - public void testReplaceValue3_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmIntString(5); - c.replace(JSR166TestCase.notPresent, "A", null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * remove(null) throws NPE - */ - @Test - public void testRemove1_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmStringString(5); - c.put("sadsdf", "asdads"); - c.remove(null); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * remove(null, x) throws NPE - */ - @Test - public void testRemove2_NullPointerException() throws IOException { - try { - ChronicleMap c = newShmStringString(5); - c.put("sadsdf", "asdads"); - c.remove(null, "whatever"); - shouldThrow(); - } catch (NullPointerException success) { - } - } - - /** - * remove(x, null) returns false - */ - @Test - public void testRemove3() throws IOException { - ChronicleMap c = newShmStringString(5); - c.put("sadsdf", "asdads"); - assertFalse(c.remove("sadsdf", null)); - } - - // classes for testing Comparable fallbacks - static class BI implements Comparable { - private final int value; - - BI(int value) { - this.value = value; - } - - public int compareTo(BI other) { - return Integer.compare(value, other.value); - } - - public boolean equals(Object x) { - return (x instanceof BI) && ((BI) x).value == value; - } - - public int hashCode() { - return 42; - } - } - - static class CI extends BI { - CI(int value) { - super(value); - } - } - - static class DI extends BI { - DI(int value) { - super(value); - } - } - - static class BS implements Comparable { - private final String value; - - BS(String value) { - this.value = value; - } - - public int compareTo(BS other) { - return value.compareTo(other.value); - } - - public boolean equals(Object x) { - return (x instanceof BS) && value.equals(((BS) x).value); - } - - public int hashCode() { - return 42; - } - } - - static class LexicographicList> extends ArrayList - implements Comparable> { - private static final long serialVersionUID = 0; - static long total; - static long n; - - LexicographicList(Collection c) { - super(c); - } - - LexicographicList(E e) { - super(Collections.singleton(e)); - } - - public int compareTo(LexicographicList other) { - long start = System.currentTimeMillis(); - int common = Math.min(size(), other.size()); - int r = 0; - for (int i = 0; i < common; i++) { - if ((r = get(i).compareTo(other.get(i))) != 0) - break; - } - if (r == 0) - r = Integer.compare(size(), other.size()); - total += System.currentTimeMillis() - start; - n++; - return r; - } - } - - /** - * A deserialized map equals original - */ - /* @Test public void testSerialization() throws Exception { - Map x = map5(); - Map y = serialClone(x); - - assertNotSame(x, y); - assertEquals(x.size(), y.size()); - assertEquals(x, y); - assertEquals(y, x); - }*/ - - /** - * TODO : SetValue of an EntrySet entry sets value in the map. - */ - /*@Test public void testSetValueWriteThrough() { - // Adapted from a bug report by Eric Zoerner - ChronicleMap map = newShmIntString(2, 5.0f, 1); - assertTrue(map.isEmpty()); - for (int i = 0; i < 20; i++) - map.put(new Integer(i), new Integer(i)); - assertFalse(map.isEmpty()); - Map.Entry entry1 = (Map.Entry) map.entrySet().iterator().next(); - // Unless it happens to be first (in which case remainder of - // test is skipped), remove a possibly-colliding key from map - // which, under some implementations, may cause entry1 to be - // cloned in map - if (!entry1.getKey().equals(new Integer(16))) { - map.remove(new Integer(16)); - entry1.setValue("XYZ"); - assertTrue(map.containsValue("XYZ")); // fails if write-through broken - } - }*/ - -} -