HCOLL-230 - its not possible unfortunaly to read and write a socketConnection between threads, all the access to the socket connections has to be on a single

This commit is contained in:
Rob Austin
2014-12-05 20:04:29 +00:00
parent dae1f3d7d3
commit a10f395db7
4 changed files with 393 additions and 186 deletions
@@ -0,0 +1,45 @@
package eg;
import net.openhft.lang.model.constraints.MaxSize;
/**
* Created by Vanitha on 12/5/2014.
*/
public interface TestIntrumentVOInterface {
public int getSizeOfInstrumentIDArray();
public void setSizeOfInstrumentIDArray(int sizeOfInstrumentIDArray);
public String getSymbol();
public void setSymbol(@MaxSize(20) String symbol);
public String getCurrencyCode();
public void setCurrencyCode(@MaxSize(4) String currencyCode);
public void setInstrumentIDAt(@MaxSize(2) int location, TestInstrumentIDVOInterface instrumentID);
public TestInstrumentIDVOInterface getInstrumentIDAt(int location);
interface TestInstrumentIDVOInterface {
public String getIdSource();
public void setIdSource(@MaxSize(6) String idSource);
public String getSecurityId();
public void setSecurityId(@MaxSize(100) String securityId);
}
}
@@ -0,0 +1,110 @@
package eg;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import net.openhft.chronicle.map.ChronicleMap;
import net.openhft.chronicle.map.ChronicleMapBuilder;
import net.openhft.lang.model.DataValueClasses;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
/**
* Created by Vanitha on 12/5/2014.
*/
public class TestNesterInterfaceMarshal {
/**
* Test that both maps are created first and then populated the first map so that second will get the contents
* @throws Exception
*/
@Test
public void testReplicatedMap() throws Exception {
ChronicleMap<CharSequence,TestIntrumentVOInterface> map =createReplicatedMap1();
ChronicleMap<CharSequence,TestIntrumentVOInterface> replicatedMap=createReplicatedMap2();
//Store some data into MAP1
TestIntrumentVOInterface intrumentVOInterface= DataValueClasses.newDirectInstance(TestIntrumentVOInterface.class);
intrumentVOInterface.setSymbol("FIXFlyer");
intrumentVOInterface.setCurrencyCode("USA");
intrumentVOInterface.setSizeOfInstrumentIDArray(2);
TestIntrumentVOInterface.TestInstrumentIDVOInterface instrumentIDVOInterface= intrumentVOInterface.getInstrumentIDAt(0);
instrumentIDVOInterface.setIdSource("CUSIP");
instrumentIDVOInterface.setSecurityId("TEST");
intrumentVOInterface.setInstrumentIDAt(0,instrumentIDVOInterface);
map.put("KEY1",intrumentVOInterface);
Assert.assertNotNull(map);
Assert.assertNotNull(replicatedMap);
int t = 0;
for (; t < 5000; t++) {
if (map.equals(replicatedMap))
break;
Thread.sleep(1);
}
Assert.assertEquals(map, replicatedMap);
Assert.assertTrue(!replicatedMap.isEmpty());
System.out.println(map.get("KEY1"));
System.out.println(replicatedMap.get("KEY1"));
map.close();
replicatedMap.close();
}
protected ChronicleMap createReplicatedMap1() {
ChronicleMap chronicleMap = null;
TcpTransportAndNetworkConfig tcpTransportAndNetworkConfig=TcpTransportAndNetworkConfig.of(8076).heartBeatInterval(1L, TimeUnit.SECONDS);
try {
ChronicleMapBuilder builder = ChronicleMapBuilder.of(CharSequence.class, TestIntrumentVOInterface.class)
.entries(5000L).entrySize(2000)
.replication((byte) 1, tcpTransportAndNetworkConfig);
chronicleMap = builder.create();
} catch (Exception e) {
System.out.println("Error(s) creating instrument cache: " + e);
}
return chronicleMap;
}
protected ChronicleMap createReplicatedMap2() {
ChronicleMap chronicleMap = null;
TcpTransportAndNetworkConfig tcpTransportAndNetworkConfig=TcpTransportAndNetworkConfig.of(8077,new InetSocketAddress("127.0.0.1",8076)).heartBeatInterval(1L, TimeUnit.SECONDS);
try {
ChronicleMapBuilder builder = ChronicleMapBuilder.of(CharSequence.class, TestIntrumentVOInterface.class)
.putReturnsNull(true)
.removeReturnsNull(true)
.entries(5000L).entrySize(2000)
.replication((byte)2,tcpTransportAndNetworkConfig) ;
chronicleMap = builder.create();
} catch (Exception e) {
System.out.println("*********************Error(s) creating launcher instrument cache: " + e);
}
return chronicleMap;
}
}
@@ -18,7 +18,6 @@
package net.openhft.chronicle.map;
import junit.framework.Assert;
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
import org.junit.After;
import org.junit.Before;
@@ -29,7 +28,7 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -571,14 +570,14 @@ public class StatelessClientTest {
@Test
public void testThreadSafeness() throws IOException, InterruptedException {
Thread.sleep(5000);
int nThreads = 1;
final ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
final ExecutorService executorService = Executors.newFixedThreadPool(3);
int count = 1000000;
int count = 100000;
final CountDownLatch latch = new CountDownLatch(count * 2);
final AtomicInteger got = new AtomicInteger();
long startTime = System.currentTimeMillis();
// server
try (ChronicleMap<Integer, Integer> server = ChronicleMapBuilder.of(Integer.class, Integer.class)
.putReturnsNull(true)
@@ -587,7 +586,6 @@ public class StatelessClientTest {
// stateless client
try (ChronicleMap<Integer, Integer> client = ChronicleMapBuilder.of(Integer.class,
Integer.class)
.putReturnsNull(true)
.statelessClient(new InetSocketAddress("localhost", 8059)).create()) {
for (int i = 0; i < count; i++) {
@@ -598,7 +596,6 @@ public class StatelessClientTest {
@Override
public void run() {
try {
System.out.println("put " + j + ", Thread" + Thread.currentThread().getName());
client.put(j, j);
latch.countDown();
} catch (Error | Exception e) {
@@ -611,19 +608,25 @@ public class StatelessClientTest {
for (int i = 0; i < count; i++) {
final int j = i;
final AtomicBoolean issue = new AtomicBoolean();
executorService.submit(new Runnable() {
@Override
public void run() {
try {
System.out.println("get " + j + ", Thread" + Thread.currentThread().getName());
while (client.get(j) == null) {
System.out.println("waiting");
Integer result = client.get(j);
if (result == null) {
executorService.submit(this);
return;
}
if (j != client.get(j))
issue.set(true);
if (result.equals(j)) {
got.incrementAndGet();
} else {
System.out.println("expected j=" + j + " but got back=" + result);
}
latch.countDown();
} catch (Error | Exception e) {
@@ -634,14 +637,16 @@ public class StatelessClientTest {
});
if (issue.get())
Assert.fail();
}
latch.await();
System.out.println("..............................CLosing");
latch.await();
System.out.println("" + count + " messages took " +
TimeUnit.MILLISECONDS.toSeconds(System
.currentTimeMillis() - startTime) + " seconds, using " + nThreads + "" +
" threads");
assertEquals(count, got.get());
}
} finally {
executorService.shutdownNow();