Jordan Halterman | 7cdb87f | 2018-06-26 23:41:22 -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.Collection; |
| 19 | import java.util.concurrent.CountDownLatch; |
| 20 | import java.util.concurrent.Executor; |
| 21 | import java.util.concurrent.Executors; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | |
| 25 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 26 | import io.atomix.protocols.raft.service.RaftService; |
| 27 | import org.junit.Test; |
| 28 | import org.onosproject.store.primitives.DefaultConsistentMultimap; |
| 29 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimap; |
| 30 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapService; |
| 31 | import org.onosproject.store.primitives.resources.impl.AtomixTestBase; |
| 32 | import org.onosproject.store.serializers.KryoNamespaces; |
| 33 | import org.onosproject.store.service.ConsistentMultimap; |
| 34 | import org.onosproject.store.service.Serializer; |
| 35 | |
| 36 | import static org.junit.Assert.assertEquals; |
| 37 | import static org.junit.Assert.fail; |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for cached {@link AtomixConsistentSetMultimap}. |
| 41 | */ |
| 42 | public class CachingAsyncConsistentMultimapTest extends AtomixTestBase<AtomixConsistentSetMultimap> { |
| 43 | |
| 44 | @Override |
| 45 | protected RaftService createService() { |
| 46 | return new AtomixConsistentSetMultimapService(); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | protected AtomixConsistentSetMultimap createPrimitive(RaftProxy proxy) { |
| 51 | return new AtomixConsistentSetMultimap(proxy); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests that reads following events are not stale when cached. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testCacheConsistency() throws Throwable { |
| 59 | Serializer serializer = Serializer.using(KryoNamespaces.BASIC); |
| 60 | |
| 61 | ConsistentMultimap<String, String> multimap1 = new DefaultConsistentMultimap<>( |
| 62 | new CachingAsyncConsistentMultimap<>( |
| 63 | new TranscodingAsyncConsistentMultimap<>( |
| 64 | newPrimitive("testCacheConsistency"), |
| 65 | k -> k, |
| 66 | k -> k, |
| 67 | v -> serializer.decode(v), |
| 68 | v -> serializer.encode(v))), 5000); |
| 69 | ConsistentMultimap<String, String> multimap2 = new DefaultConsistentMultimap<>( |
| 70 | new CachingAsyncConsistentMultimap<>( |
| 71 | new TranscodingAsyncConsistentMultimap<>( |
| 72 | newPrimitive("testCacheConsistency"), |
| 73 | k -> k, |
| 74 | k -> k, |
| 75 | v -> serializer.decode(v), |
| 76 | v -> serializer.encode(v))), 5000); |
| 77 | |
| 78 | CountDownLatch latch = new CountDownLatch(1); |
| 79 | AtomicBoolean failed = new AtomicBoolean(); |
| 80 | |
| 81 | Executor executor = Executors.newSingleThreadExecutor(); |
| 82 | multimap1.addListener(event -> { |
| 83 | if (event.newValue().equals("baz")) { |
| 84 | Collection<? extends String> values = multimap1.get("foo").value(); |
| 85 | try { |
| 86 | assertEquals(2, values.size()); |
| 87 | } catch (AssertionError e) { |
| 88 | failed.set(true); |
| 89 | } |
| 90 | latch.countDown(); |
| 91 | } |
| 92 | }, executor); |
| 93 | |
| 94 | multimap2.put("foo", "bar"); |
| 95 | multimap2.put("foo", "baz"); |
| 96 | |
| 97 | latch.await(10, TimeUnit.SECONDS); |
| 98 | if (latch.getCount() == 1 || failed.get()) { |
| 99 | fail(); |
| 100 | } |
| 101 | } |
| 102 | } |