forked from voldemort/voldemort
/
EndToEndTest.java
195 lines (169 loc) · 8.51 KB
/
EndToEndTest.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package voldemort.server;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import voldemort.ServerTestUtils;
import voldemort.TestUtils;
import voldemort.client.ClientConfig;
import voldemort.client.SocketStoreClientFactory;
import voldemort.client.StoreClient;
import voldemort.client.StoreClientFactory;
import voldemort.cluster.Cluster;
import voldemort.cluster.Node;
import voldemort.store.socket.SocketStoreFactory;
import voldemort.store.socket.clientrequest.ClientRequestExecutorPool;
import voldemort.versioning.Occurred;
import voldemort.versioning.VectorClock;
import voldemort.versioning.Version;
import voldemort.versioning.Versioned;
/**
* Provides an unmocked end to end unit test of a Voldemort cluster.
*
*/
@RunWith(Parameterized.class)
public class EndToEndTest {
private static final String STORE_NAME = "test-readrepair-memory";
private static final String STORES_XML = "test/common/voldemort/config/stores.xml";
private final SocketStoreFactory socketStoreFactory = new ClientRequestExecutorPool(2,
10000,
100000,
32 * 1024);
private final boolean useNio;
private List<VoldemortServer> servers;
private Cluster cluster;
private StoreClient<String, String> storeClient;
public EndToEndTest(boolean useNio) {
this.useNio = useNio;
}
@Parameters
public static Collection<Object[]> configs() {
return Arrays.asList(new Object[][] { { true }, { false } });
}
@Before
public void setUp() throws IOException {
cluster = ServerTestUtils.getLocalCluster(2, new int[][] { { 0, 2, 4, 6 }, { 1, 3, 5, 7 } });
servers = new ArrayList<VoldemortServer>();
servers.add(ServerTestUtils.startVoldemortServer(socketStoreFactory,
ServerTestUtils.createServerConfig(useNio,
0,
TestUtils.createTempDir()
.getAbsolutePath(),
null,
STORES_XML,
new Properties()),
cluster));
servers.add(ServerTestUtils.startVoldemortServer(socketStoreFactory,
ServerTestUtils.createServerConfig(useNio,
1,
TestUtils.createTempDir()
.getAbsolutePath(),
null,
STORES_XML,
new Properties()),
cluster));
Node node = cluster.getNodeById(0);
String bootstrapUrl = "tcp://" + node.getHost() + ":" + node.getSocketPort();
StoreClientFactory storeClientFactory = new SocketStoreClientFactory(new ClientConfig().setBootstrapUrls(bootstrapUrl));
storeClient = storeClientFactory.getStoreClient(STORE_NAME);
}
@After
public void tearDown() {
socketStoreFactory.close();
}
/**
* Test the basic get/getAll/put/delete functionality.
*/
@Test
public void testSanity() {
storeClient.put("Belarus", "Minsk");
storeClient.put("Russia", "Moscow");
storeClient.put("Ukraine", "Kiev");
storeClient.put("Kazakhstan", "Almaty");
Versioned<String> v1 = storeClient.get("Belarus");
assertEquals("get/put work as expected", "Minsk", v1.getValue());
storeClient.put("Kazakhstan", "Astana");
Versioned<String> v2 = storeClient.get("Kazakhstan");
assertEquals("clobbering a value works as expected, we have read-your-writes consistency",
"Astana",
v2.getValue());
Map<String, Versioned<String>> capitals = storeClient.getAll(Arrays.asList("Russia",
"Ukraine",
"Japan"));
assertEquals("getAll works as expected", "Moscow", capitals.get("Russia").getValue());
assertEquals("getAll works as expected", "Kiev", capitals.get("Ukraine").getValue());
assertTrue("getAll works as expected", capitals.get("Japan") == null);
storeClient.delete("Ukraine");
assertNull("delete works as expected", storeClient.get("Ukraine"));
}
/**
* Test the new put that returns the new version
*/
@Test
public void testPutReturnVersion() {
Version baseVersion = new VectorClock();
Version oldVersion = null;
Version newVersion = null;
Versioned<String> getVersioned = null;
String oldValue = null;
String newValue = null;
for(int i = 0; i < 5; i++) {
oldValue = "value" + i;
newValue = "value" + (i + 1);
oldVersion = storeClient.put("key1", Versioned.value(oldValue, baseVersion));
newVersion = storeClient.put("key1",
Versioned.value(newValue,
((VectorClock) oldVersion).clone()));
getVersioned = storeClient.get("key1");
baseVersion = newVersion;
verifyResults(oldVersion, newVersion, getVersioned, newValue);
}
}
@Test
public void testUnversionedPutReturnVersion() {
Version oldVersion = null;
Version newVersion = null;
Versioned<String> getVersioned = null;
String oldValue = null;
String newValue = null;
for(int i = 0; i < 5; i++) {
oldValue = "value" + i;
newValue = "value" + i + 1;
oldVersion = storeClient.put("key1", oldValue);
newVersion = storeClient.put("key1", newValue);
assertEquals("Version did not advance", Occurred.AFTER, newVersion.compare(oldVersion));
getVersioned = storeClient.get("key1");
verifyResults(oldVersion, newVersion, getVersioned, newValue);
}
}
private void verifyResults(Version oldVersion,
Version newVersion,
Versioned<String> getVersioned,
String newValue) {
// make sure version advances between two puts
assertEquals("Versions of put did not advance",
Occurred.AFTER,
newVersion.compare(oldVersion));
// make sure version of last put equals version of the get
assertEquals("Version of put is larger than version of get",
Occurred.BEFORE /* before can mean equal, funny! */,
newVersion.compare(getVersioned.getVersion()));
assertEquals("Version of put is smaller than version of get",
Occurred.BEFORE /* before can mean equal, funny! */,
getVersioned.getVersion().compare(newVersion));
// make sure we get what we just put in
assertEquals("Value of put does not match value of get", newValue, getVersioned.getValue());
}
}