adding net.openhft.chronicle.map.NoTcpReplicationSoakTest

This commit is contained in:
Rob Austin
2014-12-02 20:08:26 +00:00
parent 622a62f206
commit c4417ddbe4
6 changed files with 579 additions and 95 deletions
@@ -0,0 +1,230 @@
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() {
task.decrementAndGet();
map2.put0(key, value, true, (byte) 1, t);
}
});
} else {
map2.put(key, value);
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
task.decrementAndGet();
map1.put0(key, value, true, (byte) 2, t);
}
});
}
} else {
if (rnd.nextBoolean()) {
map1.remove(key);
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
task.decrementAndGet();
map2.remoteRemove(key, (byte) 1, time);
}
});
} else {
map2.remove(key);
task.incrementAndGet();
executorService.submit(new Runnable() {
@Override
public void run() {
task.decrementAndGet();
map1.remoteRemove(key, (byte) 2, time);
}
});
}
}
}
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();
}
}
});
}
}
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(500);
if (numberOfTimesTheSame == 100) {
System.out.println("same");
break;
}
}
Thread.sleep(1);
}
}
}
@@ -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);
}
}
@@ -11,9 +11,7 @@ import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.*;
/**
* @author Rob Austin.
@@ -35,25 +33,26 @@ public class TcpReplicationSoakTest {
{
final TcpTransportAndNetworkConfig tcpConfig1 = TcpTransportAndNetworkConfig.of(s_port,
endpoint)
.heartBeatInterval(1L, TimeUnit.SECONDS)
.autoReconnectedUponDroppedConnection(true);
endpoint).autoReconnectedUponDroppedConnection(true).name(" map1");
map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(Builder.SIZE)
.entries(Builder.SIZE + Builder.SIZE)
.actualSegments(1)
.name("map1")
.replication((byte) 1, tcpConfig1)
.create();
}
{
final TcpTransportAndNetworkConfig tcpConfig2 = TcpTransportAndNetworkConfig.of(s_port + 1)
.heartBeatInterval(1L, TimeUnit.SECONDS)
.autoReconnectedUponDroppedConnection(true);
final TcpTransportAndNetworkConfig tcpConfig2 = TcpTransportAndNetworkConfig.of
(s_port + 1).autoReconnectedUponDroppedConnection(true).name("map2");
map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class)
.entries(Builder.SIZE)
.entries(Builder.SIZE + Builder.SIZE)
.name("map2")
.replication((byte) 2, tcpConfig2)
.create();
}
s_port += 2;
}
@@ -86,8 +85,8 @@ public class TcpReplicationSoakTest {
}
// TODO test this with larger sizes.
@Test(timeout = 20000)
@Test
@Ignore
public void testSoakTestWithRandomData() throws IOException, InterruptedException {
System.out.print("SoakTesting ");
@@ -99,32 +98,47 @@ public class TcpReplicationSoakTest {
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));
}
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);
waitTillEqual(10000);
Assert.assertEquals(map1, map2);
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)) {
numberOfTimesTheSame++;
Thread.sleep(1);
if (numberOfTimesTheSame == 100)
if (map1.equals(map1UnChanged) && map2.equals(map2UnChanged)) {
numberOfTimesTheSame++;
} else {
numberOfTimesTheSame = 0;
map1UnChanged = new HashMap(map1);
map2UnChanged = new HashMap(map2);
}
Thread.sleep(500);
if (numberOfTimesTheSame == 100) {
System.out.println("same");
break;
} else numberOfTimesTheSame = 0;
}
}
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));
}
}