Jordan Halterman | 74d76b1 | 2018-06-27 13:38:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
| 16 | package org.onosproject.store.primitives.impl; |
| 17 | |
| 18 | import java.util.concurrent.CountDownLatch; |
| 19 | import java.util.concurrent.Executor; |
| 20 | import java.util.concurrent.Executors; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | import java.util.concurrent.atomic.AtomicBoolean; |
| 23 | |
| 24 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 25 | import io.atomix.protocols.raft.service.RaftService; |
| 26 | import org.junit.Test; |
| 27 | import org.onosproject.store.primitives.DefaultConsistentMap; |
| 28 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; |
| 29 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMapService; |
| 30 | import org.onosproject.store.primitives.resources.impl.AtomixTestBase; |
| 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.ConsistentMap; |
| 33 | import org.onosproject.store.service.Serializer; |
| 34 | |
| 35 | import static org.junit.Assert.assertEquals; |
| 36 | import static org.junit.Assert.fail; |
| 37 | |
| 38 | /** |
| 39 | * Unit tests for cached {@link AtomixConsistentMap}. |
| 40 | */ |
| 41 | public class CachingAsyncConsistentMapTest extends AtomixTestBase<AtomixConsistentMap> { |
| 42 | |
| 43 | @Override |
| 44 | protected RaftService createService() { |
| 45 | return new AtomixConsistentMapService(); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | protected AtomixConsistentMap createPrimitive(RaftProxy proxy) { |
| 50 | return new AtomixConsistentMap(proxy); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Tests that reads following events are not stale when cached. |
| 55 | */ |
| 56 | @Test |
| 57 | public void testCacheConsistency() throws Throwable { |
| 58 | Serializer serializer = Serializer.using(KryoNamespaces.BASIC); |
| 59 | |
| 60 | ConsistentMap<String, String> map1 = new DefaultConsistentMap<>( |
| 61 | new CachingAsyncConsistentMap<>( |
| 62 | new TranscodingAsyncConsistentMap<>( |
| 63 | newPrimitive("testCacheConsistency"), |
| 64 | k -> k, |
| 65 | k -> k, |
| 66 | v -> serializer.encode(v), |
| 67 | v -> serializer.decode(v))), 5000); |
| 68 | ConsistentMap<String, String> map2 = new DefaultConsistentMap<>( |
| 69 | new CachingAsyncConsistentMap<>( |
| 70 | new TranscodingAsyncConsistentMap<>( |
| 71 | newPrimitive("testCacheConsistency"), |
| 72 | k -> k, |
| 73 | k -> k, |
| 74 | v -> serializer.encode(v), |
| 75 | v -> serializer.decode(v))), 5000); |
| 76 | |
| 77 | CountDownLatch latch = new CountDownLatch(1); |
| 78 | AtomicBoolean failed = new AtomicBoolean(); |
| 79 | |
| 80 | Executor executor = Executors.newSingleThreadExecutor(); |
| 81 | map1.addListener(event -> { |
| 82 | // Check only the "baz" value since it's the last one written. If we check for "bar" on the "bar" event, |
| 83 | // there's a race in the test wherein the cache can legitimately be updated to "baz" before the next read. |
| 84 | if (event.newValue().value().equals("baz")) { |
| 85 | try { |
| 86 | assertEquals(event.newValue().value(), map1.get("foo").value()); |
| 87 | } catch (AssertionError e) { |
| 88 | failed.set(true); |
| 89 | } |
| 90 | latch.countDown(); |
| 91 | } |
| 92 | }, executor); |
| 93 | |
| 94 | map2.put("foo", "bar"); |
| 95 | map2.put("foo", "baz"); |
| 96 | |
| 97 | latch.await(10, TimeUnit.SECONDS); |
| 98 | if (latch.getCount() == 1 || failed.get()) { |
| 99 | fail(); |
| 100 | } |
| 101 | } |
| 102 | } |