Merge remote-tracking branch 'origin/master'

This commit is contained in:
Peter Lawrey
2014-12-04 09:58:27 +02:00
31 changed files with 1220 additions and 303 deletions
+242
View File
@@ -0,0 +1,242 @@
package eg;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class BGChronicleTest {
private static final int CHRONICLE_HEARTBEAT_SECONDS = 5;
public static final long MAX_NUMBER_OF_EATERIES = 3000000L;
public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_PORT = 9050;
Random rnd = new Random(System.currentTimeMillis());
byte[] key = new byte[17];
byte[] value = new byte[11];
public ChronicleMap<byte[], byte[]> startChronicleMapServer(int port) throws InterruptedException, IOException {
TcpTransportAndNetworkConfig serverConfig = TcpTransportAndNetworkConfig.of(port)
.heartBeatInterval(CHRONICLE_HEARTBEAT_SECONDS, TimeUnit.SECONDS).name("serverMap")
.autoReconnectedUponDroppedConnection(true);
return ChronicleMapBuilder.of(byte[].class, byte[].class)
.entries(MAX_NUMBER_OF_EATERIES)
.keySize(17)
.valueSize(11)
.putReturnsNull(true)
.removeReturnsNull(true)
.replication((byte) 1, serverConfig)
.create();
}
/**
* over loop-back on my mac I get around 50,000 messages per second of my local network ( LAN ),
* I get around 730 messages per second
*
* @param hostNameOrIpAddress
* @param port
* @param numberOfMessages
* @throws IOException
*/
public void startChronicleMapClient(String hostNameOrIpAddress, final int port, final long
numberOfMessages) throws IOException {
System.out.println("client starting");
try (ChronicleMap<byte[], byte[]> map = ChronicleMapBuilder.of(byte[].class, byte[].class)
.keySize(17)
.valueSize(11)
.putReturnsNull(true)
.removeReturnsNull(true)
.statelessClient(new InetSocketAddress(hostNameOrIpAddress, port)).create()) {
System.out.println("client started");
long start = System.nanoTime();
for (long count = 0; count < numberOfMessages; count++) {
if (rnd.nextBoolean()) {
// puts
rnd.nextBytes(key);
rnd.nextBytes(value);
map.put(key, value);
} else {
//gets
rnd.nextBytes(key);
map.get(key);
}
if (count % 50000 == 0) {
logMessagesPerSecond(count, start);
}
}
logMessagesPerSecond(numberOfMessages, start);
}
}
/**
* over loop-back on my mac I get around 50,000 messages per second of my local network ( LAN ),
* I get around 730 messages per second
*
* @param hostNameOrIpAddress
* @param port
* @param numberOfMessages
* @throws IOException
*/
public void startReplicatingChronicleMapClient(String hostNameOrIpAddress, final int port,
final long numberOfMessages) throws
IOException {
System.out.println("client starting");
final InetSocketAddress serverHostPort = new InetSocketAddress(hostNameOrIpAddress, port);
TcpTransportAndNetworkConfig clientConfig = TcpTransportAndNetworkConfig.of(port + 1, serverHostPort)
.heartBeatInterval(CHRONICLE_HEARTBEAT_SECONDS, TimeUnit.SECONDS).name("clientMap")
.autoReconnectedUponDroppedConnection(true);
try (ChronicleMap<byte[], byte[]> map = ChronicleMapBuilder.of(byte[].class, byte[].class)
.entries(MAX_NUMBER_OF_EATERIES)
.keySize(17)
.valueSize(11)
.putReturnsNull(true)
.removeReturnsNull(true)
.replication((byte) 2, clientConfig)
.create()) {
System.out.println("client started");
long start = System.nanoTime();
for (long count = 0; count < numberOfMessages; count++) {
if (rnd.nextBoolean()) {
// puts
rnd.nextBytes(key);
rnd.nextBytes(value);
map.put(key, value);
} else {
//gets
rnd.nextBytes(key);
map.get(key);
}
if (count % 50000 == 0) {
logMessagesPerSecond(count, start);
}
}
logMessagesPerSecond(numberOfMessages, start);
}
}
private void logMessagesPerSecond(double numberOfMessages, long start) {
double messagesPerSecond = numberOfMessages / (double) TimeUnit.NANOSECONDS
.toSeconds(System.nanoTime() - start);
System.out.println("messages per seconds " + messagesPerSecond);
}
/**
* for loop back args = []
*
* for server - eg.BGChronicleTest server for client - eg.BGChronicleTest client
*
* @param args
* @throws IOException
* @throws InterruptedException
*/
public static void main(String... args) throws IOException, InterruptedException {
final BGChronicleTest o = new BGChronicleTest();
ChronicleMap<byte[], byte[]> serverMap = null;
try {
// loop-back
if (args.length == 0) {
serverMap = o.startChronicleMapServer(DEFAULT_PORT);
o.startChronicleMapClient(DEFAULT_HOST, DEFAULT_PORT, MAX_NUMBER_OF_EATERIES);
} else {
// server
if (args.length > 0 && args[0].equalsIgnoreCase("server")) {
int port = getPort(args, 1);
serverMap = o.startChronicleMapServer(port);
System.out.println("server ON");
} else
System.out.println("server if OFF");
// client
if (args.length == 0 || (args.length > 1 && args[0].equalsIgnoreCase("client"))) {
// stateless replicating client
String host = getHost(args, 1);
int port = getPort(args, 2);
System.out.println("client ON....");
o.startChronicleMapClient(host, port, MAX_NUMBER_OF_EATERIES);
// replicating client
} else if (args.length > 1 && args[0].equalsIgnoreCase("replicatingClient")) {
String host = getHost(args, 1);
int port = getPort(args, 2);
System.out.println("client ON....");
o.startReplicatingChronicleMapClient(host, port, MAX_NUMBER_OF_EATERIES);
} else {
System.out.println("client if OFF");
for (; ; ) {
Thread.sleep(1000);
}
}
System.out.println("client if OFF");
for (; ; ) {
Thread.sleep(1000);
}
}
} finally {
if (serverMap != null)
serverMap.close();
}
}
private static String getHost(String[] args, int index) {
String host = args.length > index && args[index] != null ? args[index] : DEFAULT_HOST;
System.out.println(host);
return host;
}
private static int getPort(String[] args, int index) {
int port = args.length > index && args[index] != null ? Integer.valueOf(args[index]) :
DEFAULT_PORT;
System.out.println(port);
return port;
}
public void test() throws IOException, InterruptedException {
main();
}
}
@@ -26,7 +26,7 @@ import java.io.IOException;
/**
* @author Rob Austin.
*/
public class TestBooleanValues {
public class BooleanValuesTest {
/**
* see issue http://stackoverflow.com/questions/26219313/strange-npe-from-chronicle-map-toy-code
@@ -20,10 +20,11 @@ import static org.junit.Assert.*;
@Ignore
public class CHMUseCasesTest {
/**
* String is not as efficient as CharSequence as a key or value but easier to use The key can only be on heap and
* variable length serialised.
* String is not as efficient as CharSequence as a key or value but easier to use The key can
* only be on heap and variable length serialised.
*/
@Test
@Ignore("Not yet implemented")
public void testStringStringMap() throws ExecutionException, InterruptedException {
/* TODO run the same test for multiple types of map stores
for(ChronicleMapBuilder<String, String> chmb : Arrays.asList(
@@ -72,10 +73,11 @@ public class CHMUseCasesTest {
}
/**
* CharSequence is more efficient when object creation is avoided. The key can only be on heap and variable length
* serialised.
* CharSequence is more efficient when object creation is avoided. The key can only be on heap
* and variable length serialised.
*/
@Test
@Ignore("Not yet implemented")
public void testCharSequenceCharSequenceMap() throws ExecutionException, InterruptedException {
try (ChronicleMap<CharSequence, CharSequence> map = ChronicleMapBuilder
.of(CharSequence.class, CharSequence.class)
@@ -138,6 +140,7 @@ public class CHMUseCasesTest {
* StringValue represents any bean which contains a String Value
*/
@Test
@Ignore("Not yet implemented")
public void testStringValueStringValueMap() {
try (ChronicleMap<StringValue, StringValue> map = ChronicleMapBuilder
.of(StringValue.class, StringValue.class)
@@ -229,6 +232,7 @@ public class CHMUseCasesTest {
}
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testIntegerIntegerMap() throws ExecutionException, InterruptedException {
try (ChronicleMap<Integer, Integer> map = ChronicleMapBuilder
.of(Integer.class, Integer.class)
@@ -287,6 +291,7 @@ public class CHMUseCasesTest {
}
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testLongLongMap() throws ExecutionException, InterruptedException {
try (ChronicleMap<Long, Long> map = ChronicleMapBuilder
.of(Long.class, Long.class)
@@ -334,6 +339,7 @@ public class CHMUseCasesTest {
}
@Test
@Ignore("Not yet implemented")
public void testDoubleDoubleMap() throws ExecutionException, InterruptedException {
try (ChronicleMap<Double, Double> map = ChronicleMapBuilder
.of(Double.class, Double.class)
@@ -381,6 +387,7 @@ public class CHMUseCasesTest {
}
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testByteArrayByteArrayMap() throws ExecutionException, InterruptedException {
try (ChronicleMap<byte[], byte[]> map = ChronicleMapBuilder
.of(byte[].class, byte[].class)
@@ -431,6 +438,7 @@ public class CHMUseCasesTest {
}
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testByteBufferByteBufferMap() throws ExecutionException, InterruptedException {
try (ChronicleMap<ByteBuffer, ByteBuffer> map = ChronicleMapBuilder
.of(ByteBuffer.class, ByteBuffer.class)
@@ -529,6 +537,7 @@ public class CHMUseCasesTest {
}
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testByteBufferDirectByteBufferMap() throws ExecutionException, InterruptedException {
try (ChronicleMap<ByteBuffer, ByteBuffer> map = ChronicleMapBuilder
.of(ByteBuffer.class, ByteBuffer.class)
@@ -589,6 +598,7 @@ public class CHMUseCasesTest {
}
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testIntValueIntValueMap() {
try (ChronicleMap<IntValue, IntValue> map = ChronicleMapBuilder
.of(IntValue.class, IntValue.class)
@@ -671,6 +681,7 @@ public class CHMUseCasesTest {
* For unsigned int -> unsigned int entries, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testUnsignedIntValueUnsignedIntValueMap() {
try (ChronicleMap<UnsignedIntValue, UnsignedIntValue> map = ChronicleMapBuilder
.of(UnsignedIntValue.class, UnsignedIntValue.class)
@@ -753,6 +764,7 @@ public class CHMUseCasesTest {
* For int values, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testIntValueShortValueMap() {
try (ChronicleMap<IntValue, ShortValue> map = ChronicleMapBuilder
.of(IntValue.class, ShortValue.class)
@@ -835,6 +847,7 @@ public class CHMUseCasesTest {
* For int -> unsigned short values, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testIntValueUnsignedShortValueMap() {
try (ChronicleMap<IntValue, UnsignedShortValue> map = ChronicleMapBuilder
.of(IntValue.class, UnsignedShortValue.class)
@@ -917,6 +930,7 @@ public class CHMUseCasesTest {
* For int values, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testIntValueCharValueMap() {
try (ChronicleMap<IntValue, CharValue> map = ChronicleMapBuilder
.of(IntValue.class, CharValue.class)
@@ -999,6 +1013,7 @@ public class CHMUseCasesTest {
* For int-> byte entries, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testIntValueUnsignedByteMap() {
try (ChronicleMap<IntValue, UnsignedByteValue> map = ChronicleMapBuilder
.of(IntValue.class, UnsignedByteValue.class)
@@ -1081,6 +1096,7 @@ public class CHMUseCasesTest {
* For int values, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testIntValueBooleanValueMap() {
try (ChronicleMap<IntValue, BooleanValue> map = ChronicleMapBuilder
.of(IntValue.class, BooleanValue.class)
@@ -1163,6 +1179,7 @@ public class CHMUseCasesTest {
* For float values, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testFloatValueFloatValueMap() {
try (ChronicleMap<FloatValue, FloatValue> map = ChronicleMapBuilder
.of(FloatValue.class, FloatValue.class)
@@ -1245,18 +1262,24 @@ public class CHMUseCasesTest {
* For double values, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-225 Add support for heap and native object in the same map")
public void testDoubleValueDoubleValueMap() {
try (ChronicleMap<DoubleValue, DoubleValue> map = ChronicleMapBuilder
.of(DoubleValue.class, DoubleValue.class)
.entrySize(16)
.create()) {
DoubleValue key1 = DataValueClasses.newDirectInstance(DoubleValue.class);
DoubleValue key2 = DataValueClasses.newInstance(DoubleValue.class);
DoubleValue value1 = DataValueClasses.newDirectInstance(DoubleValue.class);
DoubleValue value2 = DataValueClasses.newInstance(DoubleValue.class);
key1.setValue(1);
value1.setValue(11);
assertEquals(null, map.get(key1));
map.put(key1, value1);
assertEquals(value1, map.get(key1));
key2.setValue(2);
@@ -1325,6 +1348,7 @@ public class CHMUseCasesTest {
* For long values, the key can be on heap or off heap.
*/
@Test
@Ignore("HCOLL-226 implement disableOversizedEntries()")
public void testLongValueLongValueMap() {
try (ChronicleMap<LongValue, LongValue> map = ChronicleMapBuilder
.of(LongValue.class, LongValue.class)
@@ -1416,30 +1440,29 @@ public class CHMUseCasesTest {
.valueMarshaller(ListMarshaller.of(new StringMarshaller(8)))
.create()) {
map.put("1", Collections.<String>emptyList());
map.put("2", Arrays.asList("one"));
map.put("2", Arrays.asList("two-A"));
List<String> list1 = new ArrayList<>();
try (WriteContext wc = map.acquireUsingLocked("1", list1)) {
list1.add("two");
assertEquals(Arrays.asList("two"), list1);
assertEquals(Collections.EMPTY_LIST, list1);
}
List<String> list2 = new ArrayList<>();
try (ReadContext rc = map.getUsingLocked("1", list2)) {
assertTrue(rc.present());
assertEquals(Arrays.asList("two"), list2);
}
try (WriteContext wc = map.acquireUsingLocked("2", list1)) {
list1.add("three");
assertEquals(Arrays.asList("three"), list1);
}
try (ReadContext rc = map.getUsingLocked("2", list2)) {
assertTrue(rc.present());
assertEquals(Arrays.asList("one", "three"), list2);
list2.add("two-B"); // this is not written as it only a read context
assertEquals(Arrays.asList("two-A", "two-B"), list2);
}
try (ReadContext rc = map.getUsingLocked("2", list2)) {
assertTrue(rc.present());
assertEquals(Arrays.asList("two-A"), list2);
}
}
}
@Test
@Ignore("Fix LinkedHashSet Marshaller")
public void testSetValue() {
try (ChronicleMap<String, Set<String>> map = ChronicleMapBuilder
.of(String.class, (Class<Set<String>>) (Class) Set.class)
@@ -551,7 +551,8 @@ public class ChronicleMapTest {
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();
}
@@ -1398,8 +1399,7 @@ public class ChronicleMapTest {
}
}
@Ignore("JIRA raised for failing test HCOLL-222")
@Test
@Test(expected = NullPointerException.class)
public void testPutLongValue() throws IOException {
final ChronicleMapBuilder<CharSequence, LongValue> builder = ChronicleMapBuilder
.of(CharSequence.class, LongValue.class)
@@ -106,14 +106,13 @@ public class ConstantSizeBySampleTest {
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ExternalizableData))
if (!(obj instanceof SerializableData))
return false;
return Arrays.equals(((ExternalizableData) obj).data, data);
return Arrays.equals(((SerializableData) obj).data, data);
}
}
@Test
@Ignore
public void testUnexpectedlyLongConstantSerializableValues() throws IOException {
try ( ChronicleMap<Long, SerializableData> map =
ChronicleMapBuilder.of(Long.class, SerializableData.class)
@@ -35,7 +35,7 @@ import java.util.concurrent.atomic.AtomicReference;
*
* @author Rob Austin.
*/
public class EventListenerTestWithTCPSocketReplication {
public class EventListenerWithTCPSocketReplicationTest {
ChronicleMap<Integer, CharSequence> map1;
ChronicleMap<Integer, CharSequence> map2;
@@ -180,7 +180,6 @@ public class EventListenerTestWithTCPSocketReplication {
}
@Test
@Ignore("TODO Failing test")
public void testRemoteNotifyRemove() throws IOException, InterruptedException {
// ---------- SERVER1 1 ----------
@@ -0,0 +1,208 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.model.Byteable;
import net.openhft.lang.model.DataValueClasses;
import net.openhft.lang.values.IntValue;
import org.junit.*;
import java.io.Closeable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author Rob Austin.
*/
public class NoTcpReplicationSoakTest {
private ReplicatedChronicleMap<Integer, ?, ?, CharSequence, ?, ?> map1;
private ReplicatedChronicleMap<Integer, ?, ?, CharSequence, ?, ?> map2;
private IntValue value;
static int s_port = 8010;
private long time;
TimeProvider timeProvider = new TimeProvider() {
@Override
public long currentTimeMillis() {
return time;
}
};
@Before
public void setup() throws IOException {
value = DataValueClasses.newDirectReference(IntValue.class);
((Byteable) value).bytes(new ByteBufferBytes(ByteBuffer.allocateDirect(4)), 0);
{
map1 = (ReplicatedChronicleMap) ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.name("map1")
.replication((byte) 1)
.timeProvider(timeProvider)
.create();
}
{
map2 = (ReplicatedChronicleMap) ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.name("map2")
.replication((byte) 2)
.timeProvider(timeProvider)
.create();
}
s_port += 2;
}
@After
public void tearDown() throws InterruptedException {
for (final Closeable closeable : new Closeable[]{map1, map2}) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.gc();
}
Set<Thread> threads;
@Before
public void sampleThreads() {
threads = Thread.getAllStackTraces().keySet();
}
@After
public void checkThreadsShutdown() {
StatelessClientTest.checkThreadsShutdown(threads);
}
AtomicInteger task = new AtomicInteger();
@Test
@Ignore
public void testSoakTestWithRandomData() throws IOException, InterruptedException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
System.out.print("SoakTesting ");
for (int j = 1; j < 10000; j++) {
if (j % 100 == 0)
System.out.print(".");
Random rnd = new Random(j);
if (rnd.nextInt(10) < 2)
time++;
final long t = time;
final int key = rnd.nextInt(1000);
// add random delay
addRandomDelay(executorService, rnd);
if (rnd.nextBoolean()) {
final CharSequence value = "test" + j;
if (rnd.nextBoolean()) {
map1.put(key, value);
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
map2.put0(key, value, true, (byte) 1, t);
task.decrementAndGet();
}
});
} else {
map2.put(key, value);
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
map1.put0(key, value, true, (byte) 2, t);
task.decrementAndGet();
}
});
}
} else {
if (rnd.nextBoolean()) {
map1.remove(key);
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
map2.remoteRemove(key, (byte) 1, t);
task.decrementAndGet();
}
});
} else {
map2.remove(key);
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
map1.remoteRemove(key, (byte) 2, t);
task.decrementAndGet();
}
});
}
}
}
while (task.get() > 0) {
}
Assert.assertEquals(new TreeMap(map1), new TreeMap(map2));
}
private void addRandomDelay(ExecutorService executorService, Random rnd) {
if (rnd.nextInt(30) < 2) {
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
task.decrementAndGet();
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}
}
@@ -161,14 +161,13 @@ public class TCPSocketReplicationIntValueTest {
for (; t < timeOutMs; t++) {
if (map1.equals(map2))
break;
Thread.sleep(25);
Thread.sleep(1);
}
assertEquals(map1, map2);
}
// TODO test this with larger sizes.
@Test(timeout = 12000)
@Ignore("Doesn't work, maps need to check equality.")
public void testSoakTestWithRandomData() throws IOException, InterruptedException {
final long start = System.currentTimeMillis();
System.out.print("SoakTesting ");
@@ -61,12 +61,12 @@ public class TCPSocketReplicationPostConnectionTest {
s_port += 2;
map1 = TCPSocketReplication4WayMapTest.newTcpSocketShmIntString((byte) 1, port);
map1.put(5, "EXAMPLE-2");
Thread.sleep(1);
Thread.sleep(10);
map2 = TCPSocketReplication4WayMapTest.newTcpSocketShmIntString((byte) 2, port + 1,
new InetSocketAddress("localhost", port));
// allow time for the recompilation to resolve
waitTillEqual(500);
waitTillEqual(15000);
assertEquals(map1, map2);
assertTrue(!map1.isEmpty());
@@ -28,7 +28,6 @@ import java.net.InetSocketAddress;
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 net.openhft.chronicle.map.TCPSocketReplication4WayMapTest.newTcpSocketShmIntString;
import static org.junit.Assert.assertEquals;
@@ -52,7 +51,7 @@ public class TCPSocketReplicationTest {
ChronicleMapBuilder<Integer, CharSequence> map1Builder =
newTcpSocketShmBuilder(Integer.class, CharSequence.class,
(byte) 1, port, new InetSocketAddress("localhost", port + 1));
map1 = map1Builder.createPersistedTo(getPersistenceFile());
map1 = map1Builder.create();
map2 = newTcpSocketShmIntString((byte) 2, port + 1);
s_port += 2;
}
@@ -116,6 +115,46 @@ public class TCPSocketReplicationTest {
assertTrue(!map1.isEmpty());
}
@Test
public void test2() throws IOException, InterruptedException {
map2.put(1, "EXAMPLE-1");
map2.remove(1);
map1.put(1, "EXAMPLE-2");
map1.remove(1);
map2.put(1, "EXAMPLE-1");
// allow time for the recompilation to resolve
waitTillEqual(5000);
assertEquals(map1, map2);
assertTrue(!map1.isEmpty());
}
@Test
public void test4() throws IOException, InterruptedException {
map2.put(1, "EXAMPLE-1");
map2.remove(1);
map1.put(1, "EXAMPLE-2");
map1.remove(1);
map2.put(1, "EXAMPLE-1");
map2.remove(1);
// allow time for the recompilation to resolve
waitTillEqual(5000);
assertEquals(map1, map2);
assertTrue(map1.isEmpty());
}
@Test
public void testBufferOverflow() throws IOException, InterruptedException {
@@ -168,7 +207,7 @@ public class TCPSocketReplicationTest {
}
}
System.out.println("");
waitTillEqual(10000);
waitTillEqual(1000);
}
}
@@ -0,0 +1,146 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.model.Byteable;
import net.openhft.lang.model.DataValueClasses;
import net.openhft.lang.values.IntValue;
import org.junit.*;
import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author Rob Austin.
*/
public class TcpReplicationSoakTest {
private ChronicleMap<Integer, CharSequence> map1;
private ChronicleMap<Integer, CharSequence> map2;
private IntValue value;
static int s_port = 8010;
@Before
public void setup() throws IOException {
value = DataValueClasses.newDirectReference(IntValue.class);
((Byteable) value).bytes(new ByteBufferBytes(ByteBuffer.allocateDirect(4)), 0);
final InetSocketAddress endpoint = new InetSocketAddress("localhost", s_port + 1);
{
final TcpTransportAndNetworkConfig tcpConfig1 = TcpTransportAndNetworkConfig.of(s_port,
endpoint).autoReconnectedUponDroppedConnection(true).name(" map1")
.heartBeatInterval(1, TimeUnit.SECONDS);
map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(Builder.SIZE + Builder.SIZE)
.actualSegments(1)
.name("map1")
.replication((byte) 1, tcpConfig1)
.create();
}
{
final TcpTransportAndNetworkConfig tcpConfig2 = TcpTransportAndNetworkConfig.of
(s_port + 1).autoReconnectedUponDroppedConnection(true).name("map2")
.heartBeatInterval(1, TimeUnit.SECONDS);
map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(Builder.SIZE + Builder.SIZE)
.name("map2")
.replication((byte) 2, tcpConfig2)
.create();
}
s_port += 2;
}
@After
public void tearDown() throws InterruptedException {
for (final Closeable closeable : new Closeable[]{map1, map2}) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.gc();
}
Set<Thread> threads;
@Before
public void sampleThreads() {
threads = Thread.getAllStackTraces().keySet();
}
@After
public void checkThreadsShutdown() {
StatelessClientTest.checkThreadsShutdown(threads);
}
@Test
public void testSoakTestWithRandomData() throws IOException, InterruptedException {
System.out.print("SoakTesting ");
for (int j = 1; j < 2 * Builder.SIZE; j++) {
if (j % 100 == 0)
System.out.print(".");
Random rnd = new Random(j);
for (int i = 1; i < 10; i++) {
final int select = rnd.nextInt(2);
final ChronicleMap<Integer, CharSequence> map = select > 0 ? map1 : map2;
if (rnd.nextBoolean()) {
map.put(rnd.nextInt(Builder.SIZE), "test" + j);
} else {
map.remove(rnd.nextInt(Builder.SIZE));
}
}
}
Thread.sleep(1000);
System.out.println("\nwaiting till equal");
waitTillEqual(10000);
Assert.assertEquals(new TreeMap(map1), new TreeMap(map2));
}
private void waitTillEqual(final int timeOutMs) throws InterruptedException {
Map map1UnChanged = new HashMap();
Map map2UnChanged = new HashMap();
int numberOfTimesTheSame = 0;
for (int t = 0; t < timeOutMs + 100; t++) {
if (map1.equals(map2)) {
if (map1.equals(map1UnChanged) && map2.equals(map2UnChanged)) {
numberOfTimesTheSame++;
} else {
numberOfTimesTheSame = 0;
map1UnChanged = new HashMap(map1);
map2UnChanged = new HashMap(map2);
}
Thread.sleep(1);
if (numberOfTimesTheSame == 100) {
System.out.println("same");
break;
}
}
Thread.sleep(1);
}
}
}
@@ -0,0 +1,166 @@
package net.openhft.chronicle.map;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.hash.replication.TimeProvider;
import net.openhft.lang.io.ByteBufferBytes;
import net.openhft.lang.model.Byteable;
import net.openhft.lang.model.DataValueClasses;
import net.openhft.lang.values.IntValue;
import org.junit.*;
import org.mockito.Mockito;
import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author Rob Austin.
*/
public class TcpTimeBasedReplicationSoakTest {
private ChronicleMap<Integer, CharSequence> map1;
private ChronicleMap<Integer, CharSequence> map2;
private IntValue value;
static int s_port = 8010;
private TimeProvider timeProvider;
int t = 0;
@Before
public void setup() throws IOException {
value = DataValueClasses.newDirectReference(IntValue.class);
((Byteable) value).bytes(new ByteBufferBytes(ByteBuffer.allocateDirect(4)), 0);
final InetSocketAddress endpoint = new InetSocketAddress("localhost", s_port + 1);
timeProvider = new TimeProvider() {
Random rnd = new Random(4);
@Override
public long currentTimeMillis() {
if (rnd.nextBoolean())
return t++;
else
return t;
}
};
{
final TcpTransportAndNetworkConfig tcpConfig1 = TcpTransportAndNetworkConfig.of(s_port,
endpoint);
map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(Builder.SIZE)
.timeProvider(timeProvider)
.replication((byte) 1, tcpConfig1)
.create();
}
{
final TcpTransportAndNetworkConfig tcpConfig2 = TcpTransportAndNetworkConfig.of(s_port + 1);
map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(Builder.SIZE)
.timeProvider(timeProvider)
.replication((byte) 2, tcpConfig2)
.create();
}
s_port += 2;
}
@After
public void tearDown() throws InterruptedException {
for (final Closeable closeable : new Closeable[]{map1, map2}) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.gc();
}
Set<Thread> threads;
@Before
public void sampleThreads() {
threads = Thread.getAllStackTraces().keySet();
}
@After
public void checkThreadsShutdown() {
StatelessClientTest.checkThreadsShutdown(threads);
}
@Test
@Ignore
public void testSoakTestWithRandomData() throws IOException, InterruptedException {
Thread.sleep(100);
// for (int i = 30; i < 50; i++) {
System.out.print("SoakTesting ");
int max = 1000000;
for (int j = 1; j < max; j++) {
if (j % 100 == 0)
System.out.print(".");
Random rnd = new Random(System.currentTimeMillis());
final ChronicleMap<Integer, CharSequence> map = rnd.nextBoolean() ? map1 : map2;
if (rnd.nextBoolean()) {
map.put((int) rnd.nextInt(100), "test" + j);
} else {
map.remove((int) rnd.nextInt(100));
}
}
System.out.println("\nwaiting till equal");
waitTillUnchanged(1000);
System.out.println("time t=" + t);
Assert.assertEquals(new TreeMap(map1), new TreeMap(map2));
}
private void waitTillUnchanged(final int timeOutMs) throws InterruptedException {
Map map1UnChanged = new HashMap();
Map map2UnChanged = new HashMap();
int numberOfTimesTheSame = 0;
for (int t = 0; t < timeOutMs + 100; t++) {
if (map1.equals(map1UnChanged) && map2.equals(map2UnChanged)) {
numberOfTimesTheSame++;
} else {
numberOfTimesTheSame = 0;
map1UnChanged = new HashMap(map1);
map2UnChanged = new HashMap(map2);
}
Thread.sleep(50);
if (numberOfTimesTheSame == 100) {
break;
}
}
}
private void late(TimeProvider timeProvider) {
Mockito.when(timeProvider.currentTimeMillis()).thenReturn(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(5));
}
}
@@ -22,6 +22,8 @@ import net.openhft.chronicle.hash.replication.SingleChronicleHashReplication;
import net.openhft.chronicle.hash.replication.UdpTransportConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.Closeable;
import java.io.IOException;
@@ -75,8 +77,8 @@ public class UDPSocketReplicationTest {
System.gc();
}
/* @Test
@Ignore
@Test
@Ignore
public void testBufferOverflow() throws IOException, InterruptedException {
for (int i = 0; i < 1024; i++) {
@@ -84,7 +86,7 @@ public class UDPSocketReplicationTest {
map2.put(i * 2, "E");
System.out.println("" + map2);
}
}*/
}
}
@@ -23,7 +23,6 @@ import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.chronicle.map.jrs166.JSR166TestCase;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.*;
@@ -39,40 +38,17 @@ import static org.junit.Assert.*;
public class ChronicleMapTest extends JSR166TestCase {
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 ChronicleMap<Integer, CharSequence> newShmIntString(int size) throws IOException {
return ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(size).create();
}
static ChronicleMap<ArrayList, CharSequence> newShmListBoolean(int size) throws IOException {
return ChronicleMapBuilder.of(ArrayList.class, CharSequence.class)
.entries(size).create();
}
static ChronicleMap<ArrayList, CharSequence> newShmListBoolean() throws IOException {
return ChronicleMapBuilder.of(ArrayList.class, CharSequence.class).create();
}
static ChronicleMap<CharSequence, CharSequence> newStrStrMap(int size) throws IOException {
return ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
.entries(size).create();
}
static ChronicleMap<Integer, CharSequence> newShmIntString() throws IOException {
return ChronicleMapBuilder.of(Integer.class, CharSequence.class).create();
}
static ChronicleMap<BI, Boolean> newShmBiBoolean() throws IOException {
return ChronicleMapBuilder.of(BI.class, Boolean.class).create();
}
/**
* Returns a new map from Integers 1-5 to Strings "A"-"E".
*/