blob: 28be8dc5ab820f075ae65a169dd78eb33e2aee98 [file] [log] [blame]
Jonathan Hart584d2f32015-01-27 19:46:14 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jonathan Hart77bdd262015-02-03 09:07:48 -080016package org.onosproject.store.ecmap;
Jonathan Hart584d2f32015-01-27 19:46:14 -080017
18import com.google.common.collect.ComparisonChain;
Brian O'Connor5eb77c82015-03-02 18:09:39 -080019import com.google.common.collect.ImmutableSet;
Brian O'Connoreeaea2c2015-03-05 16:24:34 -080020import com.google.common.collect.Lists;
Madan Jampanib28e4ad2015-02-19 12:31:37 -080021import com.google.common.util.concurrent.MoreExecutors;
Madan Jampani3e033bd2015-04-08 13:03:49 -070022
Jonathan Hart584d2f32015-01-27 19:46:14 -080023import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.packet.IpAddress;
27import org.onlab.util.KryoNamespace;
28import org.onosproject.cluster.ClusterService;
29import org.onosproject.cluster.ControllerNode;
30import org.onosproject.cluster.DefaultControllerNode;
31import org.onosproject.cluster.NodeId;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070032import org.onosproject.event.AbstractEvent;
Jonathan Hart584d2f32015-01-27 19:46:14 -080033import org.onosproject.store.Timestamp;
34import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
35import org.onosproject.store.cluster.messaging.ClusterMessage;
36import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
37import org.onosproject.store.cluster.messaging.MessageSubject;
Madan Jampani3e033bd2015-04-08 13:03:49 -070038import org.onosproject.store.impl.LogicalTimestamp;
Jonathan Hart63939a32015-05-08 11:57:03 -070039import org.onosproject.store.service.WallClockTimestamp;
Jonathan Hart584d2f32015-01-27 19:46:14 -080040import org.onosproject.store.serializers.KryoNamespaces;
41import org.onosproject.store.serializers.KryoSerializer;
Jonathan Hart6ec029a2015-03-24 17:12:35 -070042import org.onosproject.store.service.EventuallyConsistentMap;
43import org.onosproject.store.service.EventuallyConsistentMapEvent;
44import org.onosproject.store.service.EventuallyConsistentMapListener;
Jonathan Hart584d2f32015-01-27 19:46:14 -080045
Jonathan Hart584d2f32015-01-27 19:46:14 -080046import java.util.ArrayList;
47import java.util.HashMap;
48import java.util.HashSet;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070049import java.util.List;
Jonathan Hart584d2f32015-01-27 19:46:14 -080050import java.util.Map;
51import java.util.Objects;
52import java.util.Set;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070053import java.util.concurrent.CompletableFuture;
Jonathan Hart584d2f32015-01-27 19:46:14 -080054import java.util.concurrent.CountDownLatch;
Madan Jampaniec5ae342015-04-13 15:43:10 -070055import java.util.concurrent.Executor;
Madan Jampani2af244a2015-02-22 13:12:01 -080056import java.util.concurrent.ExecutorService;
Jonathan Hart584d2f32015-01-27 19:46:14 -080057import java.util.concurrent.TimeUnit;
58import java.util.concurrent.atomic.AtomicLong;
Madan Jampani2bfa94c2015-04-11 05:03:49 -070059import java.util.function.Consumer;
60import java.util.function.Function;
Jonathan Hart584d2f32015-01-27 19:46:14 -080061
62import static com.google.common.base.Preconditions.checkArgument;
63import static junit.framework.TestCase.assertFalse;
64import static org.easymock.EasyMock.*;
Brian O'Connor5eb77c82015-03-02 18:09:39 -080065import static org.junit.Assert.*;
Jonathan Hart584d2f32015-01-27 19:46:14 -080066
67/**
68 * Unit tests for EventuallyConsistentMapImpl.
69 */
70public class EventuallyConsistentMapImplTest {
71
72 private EventuallyConsistentMap<String, String> ecMap;
73
74 private ClusterService clusterService;
75 private ClusterCommunicationService clusterCommunicator;
Jonathan Hart5ec32ba2015-02-05 13:33:58 -080076 private SequentialClockService<String, String> clockService;
Jonathan Hart584d2f32015-01-27 19:46:14 -080077
78 private static final String MAP_NAME = "test";
Brian O'Connoreeaea2c2015-03-05 16:24:34 -080079 private static final MessageSubject UPDATE_MESSAGE_SUBJECT
Jonathan Hart584d2f32015-01-27 19:46:14 -080080 = new MessageSubject("ecm-" + MAP_NAME + "-update");
Jonathan Hart584d2f32015-01-27 19:46:14 -080081 private static final MessageSubject ANTI_ENTROPY_MESSAGE_SUBJECT
82 = new MessageSubject("ecm-" + MAP_NAME + "-anti-entropy");
83
84 private static final String KEY1 = "one";
85 private static final String KEY2 = "two";
86 private static final String VALUE1 = "oneValue";
87 private static final String VALUE2 = "twoValue";
88
89 private final ControllerNode self =
90 new DefaultControllerNode(new NodeId("local"), IpAddress.valueOf(1));
91
Brian O'Connoreeaea2c2015-03-05 16:24:34 -080092 private ClusterMessageHandler updateHandler;
Jonathan Hart584d2f32015-01-27 19:46:14 -080093 private ClusterMessageHandler antiEntropyHandler;
94
95 /*
96 * Serialization is a bit tricky here. We need to serialize in the tests
97 * to set the expectations, which will use this serializer here, but the
98 * EventuallyConsistentMap will use its own internal serializer. This means
99 * this serializer must be set up exactly the same as map's internal
100 * serializer.
101 */
102 private static final KryoSerializer SERIALIZER = new KryoSerializer() {
103 @Override
104 protected void setupKryoPool() {
105 serializerPool = KryoNamespace.newBuilder()
106 // Classes we give to the map
107 .register(KryoNamespaces.API)
108 .register(TestTimestamp.class)
109 // Below is the classes that the map internally registers
Madan Jampani3e033bd2015-04-08 13:03:49 -0700110 .register(LogicalTimestamp.class)
Jonathan Hart584d2f32015-01-27 19:46:14 -0800111 .register(WallClockTimestamp.class)
Jonathan Hartf9108232015-02-02 16:37:35 -0800112 .register(PutEntry.class)
113 .register(RemoveEntry.class)
Jonathan Hart584d2f32015-01-27 19:46:14 -0800114 .register(ArrayList.class)
Jonathan Hart584d2f32015-01-27 19:46:14 -0800115 .register(AntiEntropyAdvertisement.class)
116 .register(HashMap.class)
117 .build();
118 }
119 };
120
121 @Before
122 public void setUp() throws Exception {
123 clusterService = createMock(ClusterService.class);
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800124 expect(clusterService.getLocalNode()).andReturn(self).anyTimes();
125 expect(clusterService.getNodes()).andReturn(ImmutableSet.of(self)).anyTimes();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800126 replay(clusterService);
127
128 clusterCommunicator = createMock(ClusterCommunicationService.class);
129
130 // Add expectation for adding cluster message subscribers which
131 // delegate to our ClusterCommunicationService implementation. This
132 // allows us to get a reference to the map's internal cluster message
133 // handlers so we can induce events coming in from a peer.
134 clusterCommunicator.addSubscriber(anyObject(MessageSubject.class),
Madan Jampani2af244a2015-02-22 13:12:01 -0800135 anyObject(ClusterMessageHandler.class), anyObject(ExecutorService.class));
Jonathan Hart584d2f32015-01-27 19:46:14 -0800136 expectLastCall().andDelegateTo(new TestClusterCommunicationService()).times(3);
137
138 replay(clusterCommunicator);
139
140 clockService = new SequentialClockService<>();
141
142 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
143 .register(KryoNamespaces.API)
144 .register(TestTimestamp.class);
145
Madan Jampani175e8fd2015-05-20 14:10:45 -0700146 ecMap = new EventuallyConsistentMapBuilderImpl<String, String>(
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700147 clusterService, clusterCommunicator)
148 .withName(MAP_NAME)
149 .withSerializer(serializer)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700150 .withTimestampProvider((k, v) -> clockService.getTimestamp(k, v))
Jonathan Hart6ec029a2015-03-24 17:12:35 -0700151 .withCommunicationExecutor(MoreExecutors.newDirectExecutorService())
152 .build();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800153
154 // Reset ready for tests to add their own expectations
155 reset(clusterCommunicator);
156 }
157
158 @After
159 public void tearDown() {
160 reset(clusterCommunicator);
161 ecMap.destroy();
162 }
163
Ray Milkey8dc82082015-02-20 16:22:38 -0800164 @SuppressWarnings("unchecked")
165 private EventuallyConsistentMapListener<String, String> getListener() {
166 return createMock(EventuallyConsistentMapListener.class);
167 }
168
Jonathan Hart584d2f32015-01-27 19:46:14 -0800169 @Test
170 public void testSize() throws Exception {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800171 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800172
173 assertEquals(0, ecMap.size());
174 ecMap.put(KEY1, VALUE1);
175 assertEquals(1, ecMap.size());
176 ecMap.put(KEY1, VALUE2);
177 assertEquals(1, ecMap.size());
178 ecMap.put(KEY2, VALUE2);
179 assertEquals(2, ecMap.size());
180 for (int i = 0; i < 10; i++) {
181 ecMap.put("" + i, "" + i);
182 }
183 assertEquals(12, ecMap.size());
184 ecMap.remove(KEY1);
185 assertEquals(11, ecMap.size());
186 ecMap.remove(KEY1);
187 assertEquals(11, ecMap.size());
188 }
189
190 @Test
191 public void testIsEmpty() throws Exception {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800192 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800193
194 assertTrue(ecMap.isEmpty());
195 ecMap.put(KEY1, VALUE1);
196 assertFalse(ecMap.isEmpty());
197 ecMap.remove(KEY1);
198 assertTrue(ecMap.isEmpty());
199 }
200
201 @Test
202 public void testContainsKey() throws Exception {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800203 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800204
205 assertFalse(ecMap.containsKey(KEY1));
206 ecMap.put(KEY1, VALUE1);
207 assertTrue(ecMap.containsKey(KEY1));
208 assertFalse(ecMap.containsKey(KEY2));
209 ecMap.remove(KEY1);
210 assertFalse(ecMap.containsKey(KEY1));
211 }
212
213 @Test
214 public void testContainsValue() throws Exception {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800215 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800216
217 assertFalse(ecMap.containsValue(VALUE1));
218 ecMap.put(KEY1, VALUE1);
219 assertTrue(ecMap.containsValue(VALUE1));
220 assertFalse(ecMap.containsValue(VALUE2));
221 ecMap.put(KEY1, VALUE2);
222 assertFalse(ecMap.containsValue(VALUE1));
223 assertTrue(ecMap.containsValue(VALUE2));
224 ecMap.remove(KEY1);
225 assertFalse(ecMap.containsValue(VALUE2));
226 }
227
228 @Test
229 public void testGet() throws Exception {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800230 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800231
232 CountDownLatch latch;
233
234 // Local put
235 assertNull(ecMap.get(KEY1));
236 ecMap.put(KEY1, VALUE1);
237 assertEquals(VALUE1, ecMap.get(KEY1));
238
239 // Remote put
240 ClusterMessage message
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800241 = generatePutMessage(KEY2, VALUE2, clockService.getTimestamp(KEY2, VALUE2));
Jonathan Hart584d2f32015-01-27 19:46:14 -0800242
243 // Create a latch so we know when the put operation has finished
244 latch = new CountDownLatch(1);
245 ecMap.addListener(new TestListener(latch));
246
247 assertNull(ecMap.get(KEY2));
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800248 updateHandler.handle(message);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800249 assertTrue("External listener never got notified of internal event",
250 latch.await(100, TimeUnit.MILLISECONDS));
251 assertEquals(VALUE2, ecMap.get(KEY2));
252
253 // Local remove
254 ecMap.remove(KEY2);
255 assertNull(ecMap.get(KEY2));
256
257 // Remote remove
258 ClusterMessage removeMessage
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800259 = generateRemoveMessage(KEY1, clockService.getTimestamp(KEY1, VALUE1));
Jonathan Hart584d2f32015-01-27 19:46:14 -0800260
261 // Create a latch so we know when the remove operation has finished
262 latch = new CountDownLatch(1);
263 ecMap.addListener(new TestListener(latch));
264
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800265 updateHandler.handle(removeMessage);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800266 assertTrue("External listener never got notified of internal event",
267 latch.await(100, TimeUnit.MILLISECONDS));
268 assertNull(ecMap.get(KEY1));
269 }
270
271 @Test
272 public void testPut() throws Exception {
273 // Set up expectations of external events to be sent to listeners during
274 // the test. These don't use timestamps so we can set them all up at once.
275 EventuallyConsistentMapListener<String, String> listener
Ray Milkey8dc82082015-02-20 16:22:38 -0800276 = getListener();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800277 listener.event(new EventuallyConsistentMapEvent<>(
278 EventuallyConsistentMapEvent.Type.PUT, KEY1, VALUE1));
279 listener.event(new EventuallyConsistentMapEvent<>(
280 EventuallyConsistentMapEvent.Type.PUT, KEY1, VALUE2));
281 replay(listener);
282
283 ecMap.addListener(listener);
284
285 // Set up expected internal message to be broadcast to peers on first put
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800286 expectSpecificMulticastMessage(generatePutMessage(KEY1, VALUE1, clockService
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700287 .peekAtNextTimestamp()), UPDATE_MESSAGE_SUBJECT, clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800288
289 // Put first value
290 assertNull(ecMap.get(KEY1));
291 ecMap.put(KEY1, VALUE1);
292 assertEquals(VALUE1, ecMap.get(KEY1));
293
294 verify(clusterCommunicator);
295
296 // Set up expected internal message to be broadcast to peers on second put
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800297 expectSpecificMulticastMessage(generatePutMessage(
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700298 KEY1, VALUE2, clockService.peekAtNextTimestamp()), UPDATE_MESSAGE_SUBJECT, clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800299
300 // Update same key to a new value
301 ecMap.put(KEY1, VALUE2);
302 assertEquals(VALUE2, ecMap.get(KEY1));
303
304 verify(clusterCommunicator);
305
306 // Do a put with a older timestamp than the value already there.
307 // The map data should not be changed and no notifications should be sent.
308 reset(clusterCommunicator);
309 replay(clusterCommunicator);
310
311 clockService.turnBackTime();
312 ecMap.put(KEY1, VALUE1);
313 // Value should not have changed.
314 assertEquals(VALUE2, ecMap.get(KEY1));
315
316 verify(clusterCommunicator);
317
318 // Check that our listener received the correct events during the test
319 verify(listener);
320 }
321
322 @Test
323 public void testRemove() throws Exception {
324 // Set up expectations of external events to be sent to listeners during
325 // the test. These don't use timestamps so we can set them all up at once.
326 EventuallyConsistentMapListener<String, String> listener
Ray Milkey8dc82082015-02-20 16:22:38 -0800327 = getListener();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800328 listener.event(new EventuallyConsistentMapEvent<>(
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700329 EventuallyConsistentMapEvent.Type.REMOVE, KEY1, VALUE1));
330 listener.event(new EventuallyConsistentMapEvent<>(
Jonathan Hart584d2f32015-01-27 19:46:14 -0800331 EventuallyConsistentMapEvent.Type.REMOVE, KEY1, null));
Jonathan Hart584d2f32015-01-27 19:46:14 -0800332 listener.event(new EventuallyConsistentMapEvent<>(
333 EventuallyConsistentMapEvent.Type.PUT, KEY1, VALUE1));
334 listener.event(new EventuallyConsistentMapEvent<>(
335 EventuallyConsistentMapEvent.Type.PUT, KEY2, VALUE2));
336 replay(listener);
337
338 ecMap.addListener(listener);
339
340 // Put in an initial value
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800341 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800342 ecMap.put(KEY1, VALUE1);
343 assertEquals(VALUE1, ecMap.get(KEY1));
344
345 // Remove the value and check the correct internal cluster messages
346 // are sent
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800347 expectSpecificMulticastMessage(generateRemoveMessage(KEY1, clockService.peekAtNextTimestamp()),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700348 UPDATE_MESSAGE_SUBJECT, clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800349
350 ecMap.remove(KEY1);
351 assertNull(ecMap.get(KEY1));
352
353 verify(clusterCommunicator);
354
355 // Remove the same value again. Even though the value is no longer in
356 // the map, we expect that the tombstone is updated and another remove
357 // event is sent to the cluster and external listeners.
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800358 expectSpecificMulticastMessage(generateRemoveMessage(KEY1, clockService.peekAtNextTimestamp()),
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700359 UPDATE_MESSAGE_SUBJECT, clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800360
361 ecMap.remove(KEY1);
362 assertNull(ecMap.get(KEY1));
363
364 verify(clusterCommunicator);
365
366
367 // Put in a new value for us to try and remove
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800368 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800369
370 ecMap.put(KEY2, VALUE2);
371
372 clockService.turnBackTime();
373
374 // Remove should have no effect, since it has an older timestamp than
375 // the put. Expect no notifications to be sent out
376 reset(clusterCommunicator);
377 replay(clusterCommunicator);
378
379 ecMap.remove(KEY2);
380
381 verify(clusterCommunicator);
382
383 // Check that our listener received the correct events during the test
384 verify(listener);
385 }
386
387 @Test
388 public void testPutAll() throws Exception {
389 // putAll() with an empty map is a no-op - no messages will be sent
390 reset(clusterCommunicator);
391 replay(clusterCommunicator);
392
393 ecMap.putAll(new HashMap<>());
394
395 verify(clusterCommunicator);
396
397 // Set up the listener with our expected events
398 EventuallyConsistentMapListener<String, String> listener
Ray Milkey8dc82082015-02-20 16:22:38 -0800399 = getListener();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800400 listener.event(new EventuallyConsistentMapEvent<>(
401 EventuallyConsistentMapEvent.Type.PUT, KEY1, VALUE1));
402 listener.event(new EventuallyConsistentMapEvent<>(
403 EventuallyConsistentMapEvent.Type.PUT, KEY2, VALUE2));
404 replay(listener);
405
406 ecMap.addListener(listener);
407
408 // Expect a multi-update inter-instance message
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700409 expectSpecificBroadcastMessage(generatePutMessage(KEY1, VALUE1, KEY2, VALUE2), UPDATE_MESSAGE_SUBJECT,
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800410 clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800411
412 Map<String, String> putAllValues = new HashMap<>();
413 putAllValues.put(KEY1, VALUE1);
414 putAllValues.put(KEY2, VALUE2);
415
416 // Put the values in the map
417 ecMap.putAll(putAllValues);
418
419 // Check the correct messages and events were sent
420 verify(clusterCommunicator);
421 verify(listener);
422 }
423
424 @Test
425 public void testClear() throws Exception {
426 EventuallyConsistentMapListener<String, String> listener
Ray Milkey8dc82082015-02-20 16:22:38 -0800427 = getListener();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800428 listener.event(new EventuallyConsistentMapEvent<>(
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700429 EventuallyConsistentMapEvent.Type.REMOVE, KEY1, VALUE1));
Jonathan Hart584d2f32015-01-27 19:46:14 -0800430 listener.event(new EventuallyConsistentMapEvent<>(
Madan Jampani43e9c9c2015-06-26 14:16:46 -0700431 EventuallyConsistentMapEvent.Type.REMOVE, KEY2, VALUE2));
Jonathan Hart584d2f32015-01-27 19:46:14 -0800432 replay(listener);
433
434 // clear() on an empty map is a no-op - no messages will be sent
435 reset(clusterCommunicator);
436 replay(clusterCommunicator);
437
438 assertTrue(ecMap.isEmpty());
439 ecMap.clear();
440 verify(clusterCommunicator);
441
442 // Put some items in the map
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800443 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800444 ecMap.put(KEY1, VALUE1);
445 ecMap.put(KEY2, VALUE2);
446
447 ecMap.addListener(listener);
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700448 expectSpecificBroadcastMessage(generateRemoveMessage(KEY1, KEY2), UPDATE_MESSAGE_SUBJECT, clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800449
450 ecMap.clear();
451
452 verify(clusterCommunicator);
453 verify(listener);
454 }
455
456 @Test
457 public void testKeySet() throws Exception {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800458 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800459
460 assertTrue(ecMap.keySet().isEmpty());
461
462 // Generate some keys
463 Set<String> keys = new HashSet<>();
464 for (int i = 1; i <= 10; i++) {
465 keys.add("" + i);
466 }
467
468 // Put each key in the map
469 keys.forEach(k -> ecMap.put(k, "value" + k));
470
471 // Check keySet() returns the correct value
472 assertEquals(keys, ecMap.keySet());
473
474 // Update the value for one of the keys
475 ecMap.put(keys.iterator().next(), "new-value");
476
477 // Check the key set is still the same
478 assertEquals(keys, ecMap.keySet());
479
480 // Remove a key
481 String removeKey = keys.iterator().next();
482 keys.remove(removeKey);
483 ecMap.remove(removeKey);
484
485 // Check the key set is still correct
486 assertEquals(keys, ecMap.keySet());
487 }
488
489 @Test
490 public void testValues() throws Exception {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800491 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800492
493 assertTrue(ecMap.values().isEmpty());
494
495 // Generate some values
496 Map<String, String> expectedValues = new HashMap<>();
497 for (int i = 1; i <= 10; i++) {
498 expectedValues.put("" + i, "value" + i);
499 }
500
501 // Add them into the map
502 expectedValues.entrySet().forEach(e -> ecMap.put(e.getKey(), e.getValue()));
503
504 // Check the values collection is correct
505 assertEquals(expectedValues.values().size(), ecMap.values().size());
506 expectedValues.values().forEach(v -> assertTrue(ecMap.values().contains(v)));
507
508 // Update the value for one of the keys
509 Map.Entry<String, String> first = expectedValues.entrySet().iterator().next();
510 expectedValues.put(first.getKey(), "new-value");
511 ecMap.put(first.getKey(), "new-value");
512
513 // Check the values collection is still correct
514 assertEquals(expectedValues.values().size(), ecMap.values().size());
515 expectedValues.values().forEach(v -> assertTrue(ecMap.values().contains(v)));
516
517 // Remove a key
518 String removeKey = expectedValues.keySet().iterator().next();
519 expectedValues.remove(removeKey);
520 ecMap.remove(removeKey);
521
522 // Check the values collection is still correct
523 assertEquals(expectedValues.values().size(), ecMap.values().size());
524 expectedValues.values().forEach(v -> assertTrue(ecMap.values().contains(v)));
525 }
526
527 @Test
528 public void testEntrySet() throws Exception {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800529 expectPeerMessage(clusterCommunicator);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800530
531 assertTrue(ecMap.entrySet().isEmpty());
532
533 // Generate some values
534 Map<String, String> expectedValues = new HashMap<>();
535 for (int i = 1; i <= 10; i++) {
536 expectedValues.put("" + i, "value" + i);
537 }
538
539 // Add them into the map
540 expectedValues.entrySet().forEach(e -> ecMap.put(e.getKey(), e.getValue()));
541
542 // Check the entry set is correct
543 assertTrue(entrySetsAreEqual(expectedValues, ecMap.entrySet()));
544
545 // Update the value for one of the keys
546 Map.Entry<String, String> first = expectedValues.entrySet().iterator().next();
547 expectedValues.put(first.getKey(), "new-value");
548 ecMap.put(first.getKey(), "new-value");
549
550 // Check the entry set is still correct
551 assertTrue(entrySetsAreEqual(expectedValues, ecMap.entrySet()));
552
553 // Remove a key
554 String removeKey = expectedValues.keySet().iterator().next();
555 expectedValues.remove(removeKey);
556 ecMap.remove(removeKey);
557
558 // Check the entry set is still correct
559 assertTrue(entrySetsAreEqual(expectedValues, ecMap.entrySet()));
560 }
561
562 private static boolean entrySetsAreEqual(Map<String, String> expectedMap, Set<Map.Entry<String, String>> actual) {
563 if (expectedMap.entrySet().size() != actual.size()) {
564 return false;
565 }
566
567 for (Map.Entry<String, String> e : actual) {
568 if (!expectedMap.containsKey(e.getKey())) {
569 return false;
570 }
571 if (!Objects.equals(expectedMap.get(e.getKey()), e.getValue())) {
572 return false;
573 }
574 }
575 return true;
576 }
577
578 @Test
579 public void testDestroy() throws Exception {
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800580 clusterCommunicator.removeSubscriber(UPDATE_MESSAGE_SUBJECT);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800581 clusterCommunicator.removeSubscriber(ANTI_ENTROPY_MESSAGE_SUBJECT);
582
583 replay(clusterCommunicator);
584
585 ecMap.destroy();
586
587 verify(clusterCommunicator);
588
589 try {
590 ecMap.get(KEY1);
591 fail("get after destroy should throw exception");
592 } catch (IllegalStateException e) {
593 assertTrue(true);
594 }
595
596 try {
597 ecMap.put(KEY1, VALUE1);
598 fail("put after destroy should throw exception");
599 } catch (IllegalStateException e) {
600 assertTrue(true);
601 }
602 }
603
604 private ClusterMessage generatePutMessage(String key, String value, Timestamp timestamp) {
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800605 PutEntry<String, String> event = new PutEntry<>(key, value, timestamp);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800606
607 return new ClusterMessage(
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800608 clusterService.getLocalNode().id(), UPDATE_MESSAGE_SUBJECT,
609 SERIALIZER.encode(Lists.newArrayList(event)));
Jonathan Hart584d2f32015-01-27 19:46:14 -0800610 }
611
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700612 private List<PutEntry<String, String>> generatePutMessage(
613 String key1, String value1, String key2, String value2) {
Jonathan Hartf9108232015-02-02 16:37:35 -0800614 ArrayList<PutEntry<String, String>> list = new ArrayList<>();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800615
616 Timestamp timestamp1 = clockService.peek(1);
617 Timestamp timestamp2 = clockService.peek(2);
618
Jonathan Hartf9108232015-02-02 16:37:35 -0800619 PutEntry<String, String> pe1 = new PutEntry<>(key1, value1, timestamp1);
620 PutEntry<String, String> pe2 = new PutEntry<>(key2, value2, timestamp2);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800621
622 list.add(pe1);
623 list.add(pe2);
624
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700625 return list;
Jonathan Hart584d2f32015-01-27 19:46:14 -0800626 }
627
628 private ClusterMessage generateRemoveMessage(String key, Timestamp timestamp) {
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800629 RemoveEntry<String, String> event = new RemoveEntry<>(key, timestamp);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800630
631 return new ClusterMessage(
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800632 clusterService.getLocalNode().id(), UPDATE_MESSAGE_SUBJECT,
633 SERIALIZER.encode(Lists.newArrayList(event)));
Jonathan Hart584d2f32015-01-27 19:46:14 -0800634 }
635
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700636 private List<RemoveEntry<String, String>> generateRemoveMessage(String key1, String key2) {
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800637 ArrayList<RemoveEntry<String, String>> list = new ArrayList<>();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800638
639 Timestamp timestamp1 = clockService.peek(1);
640 Timestamp timestamp2 = clockService.peek(2);
641
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800642 RemoveEntry<String, String> re1 = new RemoveEntry<>(key1, timestamp1);
643 RemoveEntry<String, String> re2 = new RemoveEntry<>(key2, timestamp2);
Jonathan Hart584d2f32015-01-27 19:46:14 -0800644
645 list.add(re1);
646 list.add(re2);
647
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700648 return list;
Jonathan Hart584d2f32015-01-27 19:46:14 -0800649 }
650
651 /**
652 * Sets up a mock ClusterCommunicationService to expect a specific cluster
653 * message to be broadcast to the cluster.
654 *
655 * @param m message we expect to be sent
656 * @param clusterCommunicator a mock ClusterCommunicationService to set up
657 */
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800658 //FIXME rename
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700659 private static <T> void expectSpecificBroadcastMessage(
660 T message,
661 MessageSubject subject,
662 ClusterCommunicationService clusterCommunicator) {
Jonathan Hart584d2f32015-01-27 19:46:14 -0800663 reset(clusterCommunicator);
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700664 clusterCommunicator.<T>multicast(eq(message), eq(subject), anyObject(Function.class), anyObject(Set.class));
665 expectLastCall().anyTimes();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800666 replay(clusterCommunicator);
667 }
668
669 /**
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800670 * Sets up a mock ClusterCommunicationService to expect a specific cluster
671 * message to be multicast to the cluster.
672 *
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700673 * @param message message we expect to be sent
674 * @param subject subject we expect to be sent to
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800675 * @param clusterCommunicator a mock ClusterCommunicationService to set up
676 */
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800677 //FIXME rename
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700678 private static <T> void expectSpecificMulticastMessage(T message, MessageSubject subject,
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800679 ClusterCommunicationService clusterCommunicator) {
680 reset(clusterCommunicator);
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700681 clusterCommunicator.<T>multicast(eq(message), eq(subject), anyObject(Function.class), anyObject(Set.class));
682 expectLastCall().anyTimes();
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800683 replay(clusterCommunicator);
684 }
685
686
687 /**
688 * Sets up a mock ClusterCommunicationService to expect a multicast cluster message
Jonathan Hart584d2f32015-01-27 19:46:14 -0800689 * that is sent to it. This is useful for unit tests where we aren't
690 * interested in testing the messaging component.
691 *
692 * @param clusterCommunicator a mock ClusterCommunicationService to set up
693 */
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800694 //FIXME rename
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700695 private <T> void expectPeerMessage(ClusterCommunicationService clusterCommunicator) {
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800696 reset(clusterCommunicator);
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800697// expect(clusterCommunicator.multicast(anyObject(ClusterMessage.class),
698// anyObject(Iterable.class)))
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700699 expect(clusterCommunicator.<T>unicast(
700 anyObject(),
701 anyObject(MessageSubject.class),
702 anyObject(Function.class),
703 anyObject(NodeId.class)))
Madan Jampani175e8fd2015-05-20 14:10:45 -0700704 .andReturn(CompletableFuture.completedFuture(null))
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800705 .anyTimes();
706 replay(clusterCommunicator);
707 }
708
709 /**
710 * Sets up a mock ClusterCommunicationService to expect a broadcast cluster message
711 * that is sent to it. This is useful for unit tests where we aren't
712 * interested in testing the messaging component.
713 *
714 * @param clusterCommunicator a mock ClusterCommunicationService to set up
715 */
716 private void expectBroadcastMessage(ClusterCommunicationService clusterCommunicator) {
Jonathan Hart584d2f32015-01-27 19:46:14 -0800717 reset(clusterCommunicator);
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700718 clusterCommunicator.<AbstractEvent>multicast(
719 anyObject(AbstractEvent.class),
720 anyObject(MessageSubject.class),
721 anyObject(Function.class),
722 anyObject(Set.class));
723 expectLastCall().anyTimes();
Jonathan Hart584d2f32015-01-27 19:46:14 -0800724 replay(clusterCommunicator);
725 }
726
727 /**
728 * ClusterCommunicationService implementation that the map's addSubscriber
729 * call will delegate to. This means we can get a reference to the
730 * internal cluster message handler used by the map, so that we can simulate
731 * events coming in from other instances.
732 */
733 private final class TestClusterCommunicationService
734 implements ClusterCommunicationService {
735
736 @Override
Madan Jampani2af244a2015-02-22 13:12:01 -0800737 public void addSubscriber(MessageSubject subject,
738 ClusterMessageHandler subscriber,
739 ExecutorService executor) {
Brian O'Connoreeaea2c2015-03-05 16:24:34 -0800740 if (subject.equals(UPDATE_MESSAGE_SUBJECT)) {
741 updateHandler = subscriber;
Madan Jampani2af244a2015-02-22 13:12:01 -0800742 } else if (subject.equals(ANTI_ENTROPY_MESSAGE_SUBJECT)) {
743 antiEntropyHandler = subscriber;
744 } else {
745 throw new RuntimeException("Unexpected message subject " + subject.toString());
746 }
747 }
748
749 @Override
Jonathan Hart584d2f32015-01-27 19:46:14 -0800750 public void removeSubscriber(MessageSubject subject) {}
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700751
752 @Override
753 public <M> void broadcast(M message, MessageSubject subject,
754 Function<M, byte[]> encoder) {
755 }
756
757 @Override
758 public <M> void broadcastIncludeSelf(M message,
759 MessageSubject subject, Function<M, byte[]> encoder) {
760 }
761
762 @Override
Madan Jampani175e8fd2015-05-20 14:10:45 -0700763 public <M> CompletableFuture<Void> unicast(M message, MessageSubject subject,
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700764 Function<M, byte[]> encoder, NodeId toNodeId) {
Madan Jampani175e8fd2015-05-20 14:10:45 -0700765 return null;
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700766 }
767
768 @Override
769 public <M> void multicast(M message, MessageSubject subject,
770 Function<M, byte[]> encoder, Set<NodeId> nodes) {
771 }
772
773 @Override
774 public <M, R> CompletableFuture<R> sendAndReceive(M message,
775 MessageSubject subject, Function<M, byte[]> encoder,
776 Function<byte[], R> decoder, NodeId toNodeId) {
777 return null;
778 }
779
780 @Override
781 public <M, R> void addSubscriber(MessageSubject subject,
782 Function<byte[], M> decoder, Function<M, R> handler,
Madan Jampaniec5ae342015-04-13 15:43:10 -0700783 Function<R, byte[]> encoder, Executor executor) {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700784 }
785
786 @Override
Madan Jampani27b69c62015-05-15 15:49:02 -0700787 public <M, R> void addSubscriber(MessageSubject subject,
788 Function<byte[], M> decoder, Function<M, CompletableFuture<R>> handler,
789 Function<R, byte[]> encoder) {
790 }
791
792 @Override
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700793 public <M> void addSubscriber(MessageSubject subject,
794 Function<byte[], M> decoder, Consumer<M> handler,
Madan Jampaniec5ae342015-04-13 15:43:10 -0700795 Executor executor) {
Madan Jampani2bfa94c2015-04-11 05:03:49 -0700796 }
Jonathan Hart584d2f32015-01-27 19:46:14 -0800797 }
798
799 /**
800 * ClockService implementation that gives out timestamps based on a
801 * sequential counter. This clock service enables more control over the
802 * timestamps that are given out, including being able to "turn back time"
803 * to give out timestamps from the past.
804 *
805 * @param <T> Type that the clock service will give out timestamps for
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800806 * @param <U> Second type that the clock service will give out values for
Jonathan Hart584d2f32015-01-27 19:46:14 -0800807 */
Madan Jampanibcf1a482015-06-24 19:05:56 -0700808 private class SequentialClockService<T, U> {
Jonathan Hart584d2f32015-01-27 19:46:14 -0800809
810 private static final long INITIAL_VALUE = 1;
811 private final AtomicLong counter = new AtomicLong(INITIAL_VALUE);
812
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800813 public Timestamp getTimestamp(T object, U object2) {
Jonathan Hart584d2f32015-01-27 19:46:14 -0800814 return new TestTimestamp(counter.getAndIncrement());
815 }
816
817 /**
818 * Returns what the next timestamp will be without consuming the
819 * timestamp. This allows test code to set expectations correctly while
820 * still allowing the CUT to get the same timestamp.
821 *
822 * @return timestamp equal to the timestamp that will be returned by the
Jonathan Hart5ec32ba2015-02-05 13:33:58 -0800823 * next call to {@link #getTimestamp(T, U)}.
Jonathan Hart584d2f32015-01-27 19:46:14 -0800824 */
825 public Timestamp peekAtNextTimestamp() {
826 return peek(1);
827 }
828
829 /**
830 * Returns the ith timestamp to be given out in the future without
831 * consuming the timestamp. For example, i=1 returns the next timestamp,
832 * i=2 returns the timestamp after that, and so on.
833 *
834 * @param i number of the timestamp to peek at
835 * @return the ith timestamp that will be given out
836 */
837 public Timestamp peek(int i) {
838 checkArgument(i > 0, "i must be a positive integer");
839
840 return new TestTimestamp(counter.get() + i - 1);
841 }
842
843 /**
844 * Turns the clock back two ticks, so the next call to getTimestamp will
845 * return an older timestamp than the previous call to getTimestamp.
846 */
847 public void turnBackTime() {
848 // Not atomic, but should be OK for these tests.
849 counter.decrementAndGet();
850 counter.decrementAndGet();
851 }
852
853 }
854
855 /**
856 * Timestamp implementation where the value of the timestamp can be
857 * specified explicitly at creation time.
858 */
859 private class TestTimestamp implements Timestamp {
860
861 private final long timestamp;
862
863 /**
864 * Creates a new timestamp that has the specified value.
865 *
866 * @param timestamp value of the timestamp
867 */
868 public TestTimestamp(long timestamp) {
869 this.timestamp = timestamp;
870 }
871
872 @Override
873 public int compareTo(Timestamp o) {
874 checkArgument(o instanceof TestTimestamp);
875 TestTimestamp otherTimestamp = (TestTimestamp) o;
876 return ComparisonChain.start()
877 .compare(this.timestamp, otherTimestamp.timestamp)
878 .result();
879 }
880 }
881
882 /**
883 * EventuallyConsistentMapListener implementation which triggers a latch
884 * when it receives an event.
885 */
886 private class TestListener implements EventuallyConsistentMapListener<String, String> {
887 private CountDownLatch latch;
888
889 /**
890 * Creates a new listener that will trigger the specified latch when it
891 * receives and event.
892 *
893 * @param latch the latch to trigger on events
894 */
895 public TestListener(CountDownLatch latch) {
896 this.latch = latch;
897 }
898
899 @Override
900 public void event(EventuallyConsistentMapEvent<String, String> event) {
901 latch.countDown();
902 }
903 }
904}