diff --git a/src/test/java/net/openhft/chronicle/map/EventListenerTestWithTCPSocketReplication.java b/src/test/java/net/openhft/chronicle/map/EventListenerTestWithTCPSocketReplication.java index 6ae09b72a..9dcf2ff26 100644 --- a/src/test/java/net/openhft/chronicle/map/EventListenerTestWithTCPSocketReplication.java +++ b/src/test/java/net/openhft/chronicle/map/EventListenerTestWithTCPSocketReplication.java @@ -26,6 +26,7 @@ import org.junit.Test; import java.io.IOException; import java.net.InetSocketAddress; import java.util.concurrent.atomic.AtomicBoolean; + /** * Test ReplicatedChronicleMap where the Replicated is over a TCP Socket * @@ -35,64 +36,67 @@ import java.util.concurrent.atomic.AtomicBoolean; public class EventListenerTestWithTCPSocketReplication { + @Test + public void test() throws IOException, InterruptedException { - @Test - public void test() throws IOException, InterruptedException { + ChronicleMap map1; + ChronicleMap map2; + final AtomicBoolean putWasCalled = new AtomicBoolean(false); - ChronicleMap map1; - ChronicleMap map2; - final AtomicBoolean putWasCalled = new AtomicBoolean(false); + int udpPort = 1234; + MapEventListener> eventListener = new MapEventListener>() { + private static final long serialVersionUID = 1L; - int udpPort = 1234; - MapEventListener> eventListener = new MapEventListener>() { - private static final long serialVersionUID = 1L; - - @Override - public void onPut(ChronicleMap map, Bytes entry, int metaDataBytes, boolean added, Integer key, - CharSequence value) { - putWasCalled.getAndSet(true); - } - }; - - // ---------- SERVER1 1 ---------- - { - - // we connect the maps via a TCP socket connection on port 8077 - - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8076, new InetSocketAddress("localhost", 8077)); - ChronicleMapBuilder map1Builder = - ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .entries(20000L) - .replicators((byte) 1, tcpConfig); - - map1 = map1Builder.create(); + @Override + public void onPut(ChronicleMap map, Bytes entry, int metaDataBytes, boolean added, Integer key, + CharSequence value) { + putWasCalled.getAndSet(true); } - // ---------- SERVER2 2 on the same server as ---------- + }; - { - TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8077); - ChronicleMapBuilder map2Builder = - ChronicleMapBuilder.of(Integer.class, CharSequence.class) - .entries(20000L) - .replicators((byte) 2, tcpConfig) - .eventListener(eventListener); + // ---------- SERVER1 1 ---------- + { - map2 = map2Builder.create(); - } + // we connect the maps via a TCP socket connection on port 8077 - // we will stores some data into one map here - map1.put(5, "EXAMPLE"); - int t = 0; - for (; t < 5000; t++) { - if (map1.equals(map2)) { - break; - } - Thread.sleep(1); - } + TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8076, new InetSocketAddress("localhost", 8077)); + ChronicleMapBuilder map1Builder = + ChronicleMapBuilder.of(Integer.class, CharSequence.class) + .entries(20000L) + .replicators((byte) 1, tcpConfig); - Assert.assertEquals("Maps should be equal", map1, map2); - Assert.assertTrue("Map 1 should not be empty", !map1.isEmpty()); - Assert.assertTrue("Map 2 should not be empty", !map2.isEmpty()); - Assert.assertTrue("The eventListener.onPut should have been called", putWasCalled.get()); + map1 = map1Builder.create(); } - } \ No newline at end of file + // ---------- SERVER2 2 on the same server as ---------- + + { + TcpReplicationConfig tcpConfig = TcpReplicationConfig.of(8077); + ChronicleMapBuilder map2Builder = + ChronicleMapBuilder.of(Integer.class, CharSequence.class) + .entries(20000L) + .replicators((byte) 2, tcpConfig) + .eventListener(eventListener); + + map2 = map2Builder.create(); + } + + // we will stores some data into one map here + map1.put(5, "EXAMPLE"); + int t = 0; + for (; t < 5000; t++) { + if (map1.equals(map2)) { + break; + } + Thread.sleep(1); + } + + Assert.assertEquals("Maps should be equal", map1, map2); + Assert.assertTrue("Map 1 should not be empty", !map1.isEmpty()); + Assert.assertTrue("Map 2 should not be empty", !map2.isEmpty()); + Assert.assertTrue("The eventListener.onPut should have been called", putWasCalled.get()); + + + map1.close(); + map2.close(); + } +} \ No newline at end of file