mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Make TimeProvider non-extensible and non-configurable; Fix a bug in
ReplicatedQueryAlloc.CleanupAction
This commit is contained in:
@@ -16,21 +16,17 @@
|
||||
|
||||
package net.openhft.chronicle.map;
|
||||
|
||||
import net.openhft.chronicle.core.values.IntValue;
|
||||
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
|
||||
import net.openhft.chronicle.hash.replication.TimeProvider;
|
||||
import net.openhft.chronicle.set.Builder;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author Rob Austin.
|
||||
@@ -41,47 +37,17 @@ public class TcpTimeBasedReplicationSoakTest {
|
||||
private ChronicleMap<Integer, CharSequence> map1;
|
||||
private ChronicleMap<Integer, CharSequence> map2;
|
||||
static int s_port = 8010;
|
||||
private TimeProvider timeProvider;
|
||||
int t = 0;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException {
|
||||
|
||||
final InetSocketAddress endpoint = new InetSocketAddress("localhost", s_port + 1);
|
||||
timeProvider = new TimeProvider() {
|
||||
List<Long> moments = new ArrayList<>();
|
||||
Random rnd = new Random(4);
|
||||
|
||||
{
|
||||
moments.add(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long currentTime() {
|
||||
|
||||
if (rnd.nextBoolean()) {
|
||||
moments.add(System.currentTimeMillis());
|
||||
return t++;
|
||||
} else {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long systemTimeIntervalBetween(
|
||||
long earlierTime, long laterTime, TimeUnit systemTimeIntervalUnit) {
|
||||
long earlierMillis = moments.get((int) earlierTime);
|
||||
long laterMillis = moments.get((int) laterTime);
|
||||
long intervalMillis = laterMillis - earlierMillis;
|
||||
return systemTimeIntervalUnit.convert(intervalMillis, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
};
|
||||
|
||||
ChronicleMapBuilder<Integer, CharSequence> builder = ChronicleMapBuilder
|
||||
.of(Integer.class, CharSequence.class)
|
||||
.averageValue("test-1000000")
|
||||
.entries(Builder.SIZE)
|
||||
.timeProvider(timeProvider);
|
||||
.entries(Builder.SIZE);
|
||||
{
|
||||
TcpTransportAndNetworkConfig tcpConfig1 =
|
||||
TcpTransportAndNetworkConfig.of(s_port, endpoint);
|
||||
@@ -179,9 +145,5 @@ public class TcpTimeBasedReplicationSoakTest {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void late(TimeProvider timeProvider) {
|
||||
Mockito.when(timeProvider.currentTime()).thenReturn(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(5));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,8 +21,13 @@ import net.openhft.chronicle.hash.replication.TimeProvider;
|
||||
import net.openhft.chronicle.map.jsr166.JSR166TestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
@@ -36,10 +41,11 @@ import static org.junit.Assert.assertEquals;
|
||||
/**
|
||||
* @author Rob Austin.
|
||||
*/
|
||||
@Ignore("issues with running tests")
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(TimeProvider.class)
|
||||
public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
|
||||
public static final byte IDENTIFIER = 1;
|
||||
|
||||
|
||||
Set<Thread> threads;
|
||||
|
||||
@@ -57,20 +63,19 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
ChronicleMap<CharSequence, CharSequence>> action)
|
||||
throws IOException, InterruptedException {
|
||||
|
||||
final TimeProvider timeProvider = Mockito.mock(TimeProvider.class);
|
||||
PowerMockito.mockStatic(TimeProvider.class);
|
||||
ChronicleMapBuilder<CharSequence, CharSequence> builder = ChronicleMapBuilder
|
||||
.of(CharSequence.class, CharSequence.class)
|
||||
.averageKey("key-1")
|
||||
.averageValue("value-1")
|
||||
.entries(10)
|
||||
.cleanupRemovedEntries(false)
|
||||
.timeProvider(timeProvider);
|
||||
.cleanupRemovedEntries(false);
|
||||
try (ChronicleMap<CharSequence, CharSequence> map = builder
|
||||
.replication((byte) 1, TcpTransportAndNetworkConfig.of(8086)).create()) {
|
||||
try (ChronicleMap<CharSequence, CharSequence> map2 = builder
|
||||
.replication((byte) 2, TcpTransportAndNetworkConfig
|
||||
.of(8087, new InetSocketAddress("localhost", 8086))).create()) {
|
||||
current(timeProvider);
|
||||
current();
|
||||
|
||||
// we do a put at the current time
|
||||
map.put("key-1", "value-1");
|
||||
@@ -79,12 +84,12 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
|
||||
// now test assume that we receive a late update to the map,
|
||||
// the following update should be ignored
|
||||
late(timeProvider);
|
||||
late();
|
||||
|
||||
action.accept(map, map2);
|
||||
|
||||
// we'll now flip the time back to the current in order to do the read the result
|
||||
current(timeProvider);
|
||||
current();
|
||||
waitTillEqual(map, map2, 5000);
|
||||
assertEquals(map.size(), 1);
|
||||
// prove that late update to map2 ignored
|
||||
@@ -104,15 +109,14 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
@Test
|
||||
public void testIgnoreALatePutIfAbsent() throws IOException {
|
||||
|
||||
final TimeProvider timeProvider = Mockito.mock(TimeProvider.class);
|
||||
PowerMockito.mockStatic(TimeProvider.class);
|
||||
try (ChronicleMap map = ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
|
||||
.averageKey("key-1").averageValue("value-1")
|
||||
.entries(10)
|
||||
.cleanupRemovedEntries(false)
|
||||
.timeProvider(timeProvider)
|
||||
.replication((byte) 1)
|
||||
.create()) {
|
||||
current(timeProvider);
|
||||
current();
|
||||
|
||||
// we do a put at the current time
|
||||
map.put("key-1", "value-1");
|
||||
@@ -121,14 +125,14 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
|
||||
// now test assume that we receive a late update to the map, the following update
|
||||
// should be ignored
|
||||
late(timeProvider);
|
||||
late();
|
||||
|
||||
// should NOT throw LateUpdateException because the value is present and it is just
|
||||
// returned.
|
||||
assertEquals("value-1", map.putIfAbsent("key-1", "value-2"));
|
||||
|
||||
// we'll now flip the time back to the current in order to do the read the result
|
||||
current(timeProvider);
|
||||
current();
|
||||
assertEquals(1, map.size());
|
||||
assertEquals(map.get("key-1"), "value-1");
|
||||
}
|
||||
@@ -177,13 +181,12 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
@Test
|
||||
public void testRemoveFollowedByLatePut() throws IOException, InterruptedException {
|
||||
|
||||
final TimeProvider timeProvider = Mockito.mock(TimeProvider.class);
|
||||
PowerMockito.mockStatic(TimeProvider.class);
|
||||
try (ChronicleMap<CharSequence, CharSequence> map =
|
||||
ChronicleMapBuilder.of(CharSequence.class, CharSequence.class)
|
||||
.averageKey("key-1").averageValue("value-1")
|
||||
.entries(10)
|
||||
.cleanupRemovedEntries(false)
|
||||
.timeProvider(timeProvider)
|
||||
.replication((byte) 1, TcpTransportAndNetworkConfig.of(8086))
|
||||
.create()) {
|
||||
try (ChronicleMap<CharSequence, CharSequence> map2 =
|
||||
@@ -191,11 +194,10 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
.averageKey("key-1").averageValue("value-1")
|
||||
.entries(10)
|
||||
.cleanupRemovedEntries(false)
|
||||
.timeProvider(timeProvider)
|
||||
.replication((byte) 2, TcpTransportAndNetworkConfig.of(8087,
|
||||
new InetSocketAddress("localhost", 8086)))
|
||||
.create()) {
|
||||
current(timeProvider);
|
||||
current();
|
||||
|
||||
// we do a put at the current time
|
||||
map.put("key-1", "value-1");
|
||||
@@ -205,13 +207,13 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
|
||||
// now test assume that we receive a late update to the map,
|
||||
// the following update should be ignored
|
||||
late(timeProvider);
|
||||
late();
|
||||
|
||||
map2.put("key-1", "value-2");
|
||||
assertEquals("value-2", map2.get("key-1"));
|
||||
|
||||
// we'll now flip the time back to the current in order to do the read the result
|
||||
current(timeProvider);
|
||||
current();
|
||||
waitTillEqual(map, map2, 5000);
|
||||
assertEquals("map: " + map + ", map2: " + map2, 0, map.size());
|
||||
// prove that late put was ignored
|
||||
@@ -220,12 +222,12 @@ public class TimeBasedReplicationTest extends JSR166TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private void current(TimeProvider timeProvider) {
|
||||
Mockito.when(timeProvider.currentTime()).thenReturn(System.currentTimeMillis());
|
||||
private void current() {
|
||||
Mockito.when(TimeProvider.currentTime()).thenReturn(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
private void late(TimeProvider timeProvider) {
|
||||
Mockito.when(timeProvider.currentTime())
|
||||
private void late() {
|
||||
Mockito.when(TimeProvider.currentTime())
|
||||
.thenReturn(System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(10));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user