Added a compute method to ECMap to simplify map interactions following a read-modify-write template.
Change-Id: If8c791ce1f49a7b5b3d04941b6e03a10261c6f6f
diff --git a/core/store/dist/src/test/java/org/onosproject/store/ecmap/EventuallyConsistentMapImplTest.java b/core/store/dist/src/test/java/org/onosproject/store/ecmap/EventuallyConsistentMapImplTest.java
index 9a65630..c6ad750 100644
--- a/core/store/dist/src/test/java/org/onosproject/store/ecmap/EventuallyConsistentMapImplTest.java
+++ b/core/store/dist/src/test/java/org/onosproject/store/ecmap/EventuallyConsistentMapImplTest.java
@@ -382,6 +382,68 @@
}
@Test
+ public void testCompute() throws Exception {
+ // Set up expectations of external events to be sent to listeners during
+ // the test. These don't use timestamps so we can set them all up at once.
+ EventuallyConsistentMapListener<String, String> listener
+ = getListener();
+ listener.event(new EventuallyConsistentMapEvent<>(
+ EventuallyConsistentMapEvent.Type.PUT, KEY1, VALUE1));
+ listener.event(new EventuallyConsistentMapEvent<>(
+ EventuallyConsistentMapEvent.Type.REMOVE, KEY1, VALUE1));
+ listener.event(new EventuallyConsistentMapEvent<>(
+ EventuallyConsistentMapEvent.Type.PUT, KEY2, VALUE2));
+ replay(listener);
+
+ ecMap.addListener(listener);
+
+ // Put in an initial value
+ expectPeerMessage(clusterCommunicator);
+ ecMap.compute(KEY1, (k, v) -> VALUE1);
+ assertEquals(VALUE1, ecMap.get(KEY1));
+
+ // Remove the value and check the correct internal cluster messages
+ // are sent
+ expectSpecificMulticastMessage(generateRemoveMessage(KEY1, clockService.peekAtNextTimestamp()),
+ UPDATE_MESSAGE_SUBJECT, clusterCommunicator);
+
+ ecMap.compute(KEY1, (k, v) -> null);
+ assertNull(ecMap.get(KEY1));
+
+ verify(clusterCommunicator);
+
+ // Remove the same value again. Even though the value is no longer in
+ // the map, we expect that the tombstone is updated and another remove
+ // event is sent to the cluster and external listeners.
+ expectSpecificMulticastMessage(generateRemoveMessage(KEY1, clockService.peekAtNextTimestamp()),
+ UPDATE_MESSAGE_SUBJECT, clusterCommunicator);
+
+ ecMap.compute(KEY1, (k, v) -> null);
+ assertNull(ecMap.get(KEY1));
+
+ verify(clusterCommunicator);
+
+ // Put in a new value for us to try and remove
+ expectPeerMessage(clusterCommunicator);
+
+ ecMap.compute(KEY2, (k, v) -> VALUE2);
+
+ clockService.turnBackTime();
+
+ // Remove should have no effect, since it has an older timestamp than
+ // the put. Expect no notifications to be sent out
+ reset(clusterCommunicator);
+ replay(clusterCommunicator);
+
+ ecMap.compute(KEY2, (k, v) -> null);
+
+ verify(clusterCommunicator);
+
+ // Check that our listener received the correct events during the test
+ verify(listener);
+ }
+
+ @Test
public void testPutAll() throws Exception {
// putAll() with an empty map is a no-op - no messages will be sent
reset(clusterCommunicator);