/* * 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.File; 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.TcpReplicationConfig.of; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; /** * @author Rob Austin. */ public class TwoMapOnDifferentServers { private ChronicleMap map1; private ChronicleMap map2; @Before public void setup() throws IOException { final TcpReplicationConfig tcpConfig = of(8076, new InetSocketAddress("localhost", 8077)) .heartBeatInterval(1, SECONDS); File persistenceFile = getPersistenceFile(); map1 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000) .replicators((byte) 1, tcpConfig).create(); map2 = ChronicleMapBuilder.of(Integer.class, CharSequence.class) .entries(20000) .replicators((byte) 2, of(8077).heartBeatInterval(1, SECONDS)).create(); } @After public void tearDown() throws InterruptedException { for (final Closeable closeable : new Closeable[]{map1, map2}) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } @Test public void test3() throws IOException, InterruptedException { map1.put(5, "EXAMPLE-2"); // allow time for the recompilation to resolve waitTillEqual(5000); assertEquals(map1, map2); assertTrue(!map1.isEmpty()); } /** * * waits until map1 and map2 show the same value * * @param timeOutMs timeout in milliseconds * @throws InterruptedException */ private void waitTillEqual(final int timeOutMs) throws InterruptedException { int t = 0; for (; t < timeOutMs; t++) { if (map1.equals(map2)) break; Thread.sleep(1); } } }