blob: 84f7595421693e65de07193669b09b836894f99b [file] [log] [blame]
Jordan Halterman7cdb87f2018-06-26 23:41:22 -07001/*
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 */
16package org.onosproject.store.primitives.impl;
17
18import java.util.Collection;
19import java.util.concurrent.CountDownLatch;
20import java.util.concurrent.Executor;
21import java.util.concurrent.Executors;
22import java.util.concurrent.TimeUnit;
23import java.util.concurrent.atomic.AtomicBoolean;
24
25import io.atomix.protocols.raft.proxy.RaftProxy;
26import io.atomix.protocols.raft.service.RaftService;
27import org.junit.Test;
28import org.onosproject.store.primitives.DefaultConsistentMultimap;
29import org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimap;
30import org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapService;
31import org.onosproject.store.primitives.resources.impl.AtomixTestBase;
32import org.onosproject.store.serializers.KryoNamespaces;
33import org.onosproject.store.service.ConsistentMultimap;
34import org.onosproject.store.service.Serializer;
35
36import static org.junit.Assert.assertEquals;
37import static org.junit.Assert.fail;
38
39/**
40 * Unit tests for cached {@link AtomixConsistentSetMultimap}.
41 */
42public 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}