Jordan Halterman | 3b01fec | 2018-06-27 14:00:23 -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.DefaultDocumentTree; |
| 28 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTree; |
| 29 | import org.onosproject.store.primitives.resources.impl.AtomixDocumentTreeService; |
| 30 | import org.onosproject.store.primitives.resources.impl.AtomixTestBase; |
| 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.DocumentPath; |
| 33 | import org.onosproject.store.service.DocumentTree; |
| 34 | import org.onosproject.store.service.Ordering; |
| 35 | import org.onosproject.store.service.Serializer; |
| 36 | |
| 37 | import static org.junit.Assert.assertEquals; |
| 38 | import static org.junit.Assert.fail; |
| 39 | |
| 40 | /** |
| 41 | * Unit tests for cached {@link AtomixDocumentTree}. |
| 42 | */ |
| 43 | public class CachingAsyncDocumentTreeTest extends AtomixTestBase<AtomixDocumentTree> { |
| 44 | |
| 45 | @Override |
| 46 | protected RaftService createService() { |
| 47 | return new AtomixDocumentTreeService(Ordering.NATURAL); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected AtomixDocumentTree createPrimitive(RaftProxy proxy) { |
| 52 | return new AtomixDocumentTree(proxy); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Tests that reads following events are not stale when cached. |
| 57 | */ |
| 58 | @Test |
| 59 | public void testCacheConsistency() throws Throwable { |
| 60 | Serializer serializer = Serializer.using(KryoNamespaces.BASIC); |
| 61 | |
| 62 | DocumentTree<String> tree1 = new DefaultDocumentTree<>( |
| 63 | new CachingAsyncDocumentTree<>( |
| 64 | new DefaultDistributedDocumentTree<>( |
| 65 | "testCacheConsistency", |
| 66 | newPrimitive("testCacheConsistency"), |
| 67 | serializer)), 5000); |
| 68 | DocumentTree<String> tree2 = new DefaultDocumentTree<>( |
| 69 | new CachingAsyncDocumentTree<>( |
| 70 | new DefaultDistributedDocumentTree<>( |
| 71 | "testCacheConsistency", |
| 72 | newPrimitive("testCacheConsistency"), |
| 73 | serializer)), 5000); |
| 74 | |
| 75 | CountDownLatch latch = new CountDownLatch(1); |
| 76 | AtomicBoolean failed = new AtomicBoolean(); |
| 77 | |
| 78 | Executor executor = Executors.newSingleThreadExecutor(); |
| 79 | DocumentPath path = DocumentPath.from("root|foo"); |
| 80 | tree1.addListener(path, event -> executor.execute(() -> { |
| 81 | // Check only the "baz" value since it's the last one written. If we check for "bar" on the "bar" event, |
| 82 | // there's a race in the test wherein the cache can legitimately be updated to "baz" before the next read. |
| 83 | if (event.newValue().get().value().equals("baz")) { |
| 84 | try { |
| 85 | assertEquals(event.newValue().get().value(), tree1.get(path).value()); |
| 86 | } catch (AssertionError e) { |
| 87 | failed.set(true); |
| 88 | } |
| 89 | latch.countDown(); |
| 90 | } |
| 91 | })); |
| 92 | |
| 93 | tree2.set(path, "bar"); |
| 94 | tree2.set(path, "baz"); |
| 95 | |
| 96 | latch.await(10, TimeUnit.SECONDS); |
| 97 | if (latch.getCount() == 1 || failed.get()) { |
| 98 | fail(); |
| 99 | } |
| 100 | } |
| 101 | } |