blob: 0ee4812c43ebd97e3ff977d4e95c8a7d60e8fdeb [file] [log] [blame]
pierfa48c6e2019-10-11 18:19:59 +02001/*
2 * Copyright 2019-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 com.google.common.collect.Lists;
19import com.google.common.collect.Maps;
20import com.google.common.hash.Hashing;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.cluster.PartitionId;
24import org.onosproject.store.serializers.KryoNamespaces;
25import org.onosproject.store.service.AsyncConsistentMultimap;
26import org.onosproject.store.service.Serializer;
27import org.onosproject.store.service.Versioned;
28
29import java.util.Collection;
30import java.util.HashMap;
31import java.util.List;
32import java.util.Map;
33
34import static org.hamcrest.Matchers.*;
35import static org.hamcrest.Matchers.containsInAnyOrder;
36import static org.junit.Assert.assertThat;
37
38public class PartitionedAsyncConsistentMultimapTest {
39
40 private static PartitionedAsyncConsistentMultimap<String, String> partitionedAsyncConsistentMap;
41 private static Map<PartitionId, AsyncConsistentMultimap<String, String>> partitions;
42 private static List<PartitionId> sortedMemberPartitionIds;
43 private static Serializer serializer;
44
45 private static final String PARTITION_NAME = "PartitionManager";
46
47 private static final String KEY1 = "AAA";
48 private static final String VALUE1 = "one";
49 private static final String KEY2 = "BBB";
50 private static final String VALUE2 = "two";
51 private static final String KEY3 = "CCC";
52 private static final String VALUE3 = "three";
53 private static final String KEY4 = "DDD";
54 private static final String VALUE4 = "four";
55
56 private final List<String> allKeys = Lists.newArrayList(KEY1, KEY2,
57 KEY3, KEY4);
58 private final List<String> allValues = Lists.newArrayList(VALUE1, VALUE2,
59 VALUE3, VALUE4);
60
61 @Before
62 public void setUp() throws Exception {
63 // Init maps and partitions
64 AsyncConsistentMultimap<String, String> asyncMap1 = new AsyncConsistentMultimapMock<>();
65 AsyncConsistentMultimap<String, String> asyncMap2 = new AsyncConsistentMultimapMock<>();
66 AsyncConsistentMultimap<String, String> asyncMap3 = new AsyncConsistentMultimapMock<>();
67 AsyncConsistentMultimap<String, String> asyncMap4 = new AsyncConsistentMultimapMock<>();
68 PartitionId pid1 = PartitionId.from(1);
69 PartitionId pid2 = PartitionId.from(2);
70 PartitionId pid3 = PartitionId.from(3);
71 PartitionId pid4 = PartitionId.from(4);
72 partitions = new HashMap<>();
73 serializer = Serializer.using(KryoNamespaces.BASIC);
74 partitions.put(pid1, asyncMap1);
75 partitions.put(pid2, asyncMap2);
76 partitions.put(pid3, asyncMap3);
77 partitions.put(pid4, asyncMap4);
78 sortedMemberPartitionIds = Lists.newArrayList(partitions.keySet());
79 Hasher<String> hasher = key -> {
80 int hashCode = Hashing.sha256().hashBytes(serializer.encode(key)).asInt();
81 return sortedMemberPartitionIds.get(Math.abs(hashCode) % partitions.size());
82 };
83 partitionedAsyncConsistentMap = new PartitionedAsyncConsistentMultimap<>(PARTITION_NAME,
84 partitions,
85 hasher);
86 }
87
88 @Test
89 public void testPutAllRemoveAll() {
90 // Init phase
91 assertThat(partitionedAsyncConsistentMap.isEmpty().join(), is(true));
92 assertThat(partitionedAsyncConsistentMap.size().join(), is(0));
93 assertThat(partitionedAsyncConsistentMap.name(), is("PartitionManager"));
94 // Test multi put
95 Map<String, Collection<? extends String>> mapping = Maps.newHashMap();
96 // First build the mappings having each key a different mapping
97 allKeys.forEach(key -> {
98 switch (key) {
99 case KEY1:
100 mapping.put(key, Lists.newArrayList(allValues.subList(0, 1)));
101 break;
102 case KEY2:
103 mapping.put(key, Lists.newArrayList(allValues.subList(0, 2)));
104 break;
105 case KEY3:
106 mapping.put(key, Lists.newArrayList(allValues.subList(0, 3)));
107 break;
108 default:
109 mapping.put(key, Lists.newArrayList(allValues.subList(0, 4)));
110 break;
111 }
112 });
113 // Success
114 assertThat(partitionedAsyncConsistentMap.putAll(mapping).join(), is(true));
115 // Failure
116 assertThat(partitionedAsyncConsistentMap.putAll(mapping).join(), is(false));
117 // Verify operation
118 assertThat(partitionedAsyncConsistentMap.size().join(), is(10));
119 assertThat(partitionedAsyncConsistentMap.isEmpty().join(), is(false));
120 // verify mapping is ok
121 allKeys.forEach(key -> {
122 switch (key) {
123 case KEY1:
124 assertThat(Lists.newArrayList(Versioned.valueOrNull(partitionedAsyncConsistentMap.get(key).join())),
125 containsInAnyOrder(allValues.subList(0, 1).toArray()));
126 break;
127 case KEY2:
128 assertThat(Lists.newArrayList(Versioned.valueOrNull(partitionedAsyncConsistentMap.get(key).join())),
129 containsInAnyOrder(allValues.subList(0, 2).toArray()));
130 break;
131 case KEY3:
132 assertThat(Lists.newArrayList(Versioned.valueOrNull(partitionedAsyncConsistentMap.get(key).join())),
133 containsInAnyOrder(allValues.subList(0, 3).toArray()));
134 break;
135 default:
136 assertThat(Lists.newArrayList(Versioned.valueOrNull(partitionedAsyncConsistentMap.get(key).join())),
137 containsInAnyOrder(allValues.subList(0, 4).toArray()));
138 break;
139 }
140 });
141 // Success
142 assertThat(partitionedAsyncConsistentMap.removeAll(mapping).join(), is(true));
143 // Failure
144 assertThat(partitionedAsyncConsistentMap.removeAll(mapping).join(), is(false));
145 // Verify operation
146 assertThat(partitionedAsyncConsistentMap.size().join(), is(0));
147 assertThat(partitionedAsyncConsistentMap.isEmpty().join(), is(true));
148 }
149}