diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleSet.java b/src/main/java/net/openhft/chronicle/map/ChronicleSet.java index aefb69e5b..5d3b92abf 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleSet.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleSet.java @@ -1,12 +1,8 @@ package net.openhft.chronicle.map; +import java.io.Closeable; import java.util.Set; -/** - * Created by peter on 09/10/14. - * - * TODO to be moved to Chronicle-Map - */ -public interface ChronicleSet extends Set { +public interface ChronicleSet extends Set, Closeable { public long longSize(); } diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleSetBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleSetBuilder.java index 633b12082..ed9f0182b 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleSetBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleSetBuilder.java @@ -429,6 +429,11 @@ public class ChronicleSetBuilder implements Cloneable { public long longSize() { return m.longSize(); } + + @Override + public void close() throws IOException { + m.close(); + } } } diff --git a/src/test/java/net/openhft/chronicle/map/NodeDiscoveryTest.java b/src/test/java/net/openhft/chronicle/map/NodeDiscoveryTest.java deleted file mode 100644 index e7fd74ddb..000000000 --- a/src/test/java/net/openhft/chronicle/map/NodeDiscoveryTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package net.openhft.chronicle.map; - -import org.junit.Ignore; -import org.junit.Test; - -public class NodeDiscoveryTest { - - @Test - @Ignore - public void testDiscoverMap() throws Exception { - - - final NodeDiscovery nodeDiscovery = new NodeDiscovery(); - - final ChronicleMap map = nodeDiscovery.discoverMap(); - - Thread.sleep(1000000); - } - - -} \ No newline at end of file diff --git a/src/test/java/net/openhft/chronicle/map/SetForTCPSocketReplicationTest3way.java b/src/test/java/net/openhft/chronicle/map/SetForTCPSocketReplicationTest3way.java new file mode 100644 index 000000000..fe1ce5692 --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/SetForTCPSocketReplicationTest3way.java @@ -0,0 +1,155 @@ +/* + * 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.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.Closeable; +import java.io.IOException; +import java.net.InetSocketAddress; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static net.openhft.chronicle.map.Builder.getPersistenceFile; +import static net.openhft.chronicle.map.Replicators.tcp; +import static net.openhft.chronicle.map.TcpReplicationConfig.of; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Test Replicated Chronicle Set where the replication is over a TCP Socket + * + * @author Rob Austin. + */ + +public class SetForTCPSocketReplicationTest3way { + + + private ChronicleSet set1; + private ChronicleSet set2; + private ChronicleSet set3; + + + private static > T newTcpSocketIntSet( + final byte identifier, + final int serverPort, + final InetSocketAddress... endpoints) throws IOException { + return (T) ChronicleSetBuilder.of(Integer.class) + .addReplicator(tcp(identifier, of(serverPort, endpoints). + heartBeatInterval(1L, SECONDS))).create(getPersistenceFile()); + } + + + @Before + public void setup() throws IOException { + set1 = newTcpSocketIntSet((byte) 1, 8076, new InetSocketAddress("localhost", 8077), + new InetSocketAddress("localhost", 8079)); + set2 = newTcpSocketIntSet((byte) 2, 8077, new InetSocketAddress("localhost", 8079)); + set3 = newTcpSocketIntSet((byte) 3, 8079); + } + + @After + public void tearDown() throws InterruptedException { + Thread.sleep(5); + for (final Closeable closeable : new Closeable[]{set1, set2, set3}) { + try { + closeable.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + @Test + public void test3() throws IOException, InterruptedException { + + set3.add(5); + + // allow time for the recompilation to resolve + waitTillEqual(15000); + + assertEquals(set1, set2); + assertEquals(set3, set2); + assertTrue(!set1.isEmpty()); + + } + + @Test + public void test() throws IOException, InterruptedException { + + set1.add(1); + set1.add(2); + set1.add(2); + + set2.add(5); + set2.add(6); + + set1.remove(2); + set2.remove(3); + set1.remove(3); + set2.add(5); + + // allow time for the recompilation to resolve + waitTillEqual(5000); + + assertEquals(set1, set2); + assertEquals(set3, set3); + assertTrue(!set1.isEmpty()); + + } + + + @Test + public void testClear() throws IOException, InterruptedException { + + set1.add(1); + set1.add(2); + set1.add(2); + + set2.add(5); + set2.add(6); + + set1.clear(); + + set2.add(5); + + // allow time for the recompilation to resolve + waitTillEqual(5000); + + assertEquals(set1, set2); + assertEquals(set3, set3); + assertTrue(!set1.isEmpty()); + + } + + + private void waitTillEqual(final int timeOutMs) throws InterruptedException { + int t = 0; + for (; t < timeOutMs; t++) { + if (set1.equals(set2) && + set1.equals(set3)) + break; + Thread.sleep(1); + } + + } +} + + +