/
AvoidStaleNearCacheReadsTest.java
109 lines (95 loc) · 3.68 KB
/
AvoidStaleNearCacheReadsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package org.infinispan.client.hotrod.near;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.VersionedValue;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.client.hotrod.configuration.NearCacheMode;
import org.infinispan.client.hotrod.test.SingleHotRodServerTest;
import org.infinispan.commons.util.concurrent.NotifyingFuture;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.IntStream;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertNull;
@Test(groups = "functional", testName = "client.hotrod.near.AvoidStaleNearCacheReadsTest")
public class AvoidStaleNearCacheReadsTest extends SingleHotRodServerTest {
@AfterMethod(alwaysRun=true)
@Override
protected void clearContent() {
super.clearContent();
remoteCacheManager.getCache().clear(); // Clear the near cache too
}
@Override
protected RemoteCacheManager getRemoteCacheManager() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer().host("127.0.0.1").port(hotrodServer.getPort());
builder.nearCache().mode(NearCacheMode.INVALIDATED).maxEntries(-1);
return new RemoteCacheManager(builder.build());
}
public void testAvoidStaleReadsAfterPutRemove() {
repeated((i, remote) -> {
String value = "v" + i;
remote.put(1, value);
assertEquals(value, remote.get(1));
remote.remove(1);
assertNull(remote.get(1));
});
}
public void testAvoidStaleReadsAfterPutAll() {
repeated((i, remote) -> {
String value = "v" + i;
Map<Integer, String> map = new HashMap<>();
map.put(1, value);
remote.putAll(map);
assertEquals(value, remote.get(1));
});
}
public void testAvoidStaleReadsAfterReplace() {
repeated((i, remote) -> {
String value = "v" + i;
remote.replace(1, value);
VersionedValue<String> versioned = remote.getVersioned(1);
assertEquals(value, versioned.getValue());
});
}
public void testAvoidStaleReadsAfterReplaceWithVersion() {
repeated((i, remote) -> {
String value = "v" + i;
VersionedValue<String> versioned = remote.getVersioned(1);
remote.replaceWithVersion(1, value, versioned.getVersion());
assertEquals(value, remote.get(1));
});
}
public void testAvoidStaleReadsAfterPutAsyncRemoveVersioned() {
repeated((i, remote) -> {
String value = "v" + i;
await(remote.putAsync(1, value));
VersionedValue<String> versioned = remote.getVersioned(1);
assertEquals(value, versioned.getValue());
remote.removeWithVersion(1, versioned.getVersion());
assertNull(remote.get(1));
});
}
private void repeated(BiConsumer<Integer, RemoteCache<Integer, String>> c) {
RemoteCache<Integer, String> remote = remoteCacheManager.getCache();
remote.putIfAbsent(1, "v0");
IntStream.range(1, 1000).forEach(i -> {
c.accept(i, remote);
});
}
static <T> T await(Future<T> f) {
try {
return f.get(10000, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e ) {
throw new AssertionError(e);
}
}
}