Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.util.Tools; |
| 22 | import org.onosproject.store.primitives.MapUpdate; |
| 23 | import org.onosproject.store.primitives.TransactionId; |
| 24 | import org.onosproject.store.serializers.KryoNamespaces; |
| 25 | import org.onosproject.store.service.AsyncConsistentMap; |
| 26 | import org.onosproject.store.service.MapEventListener; |
| 27 | import org.onosproject.store.service.Serializer; |
| 28 | import org.onosproject.store.service.TransactionLog; |
| 29 | import org.onosproject.store.service.Version; |
| 30 | import org.onosproject.store.service.Versioned; |
| 31 | import org.onosproject.utils.MeteringAgent; |
| 32 | |
| 33 | import java.util.Arrays; |
| 34 | import java.util.Collection; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.Map; |
| 37 | import java.util.Set; |
| 38 | import java.util.concurrent.CompletableFuture; |
| 39 | import java.util.concurrent.Executor; |
| 40 | import java.util.function.BiFunction; |
| 41 | import java.util.function.Predicate; |
| 42 | |
| 43 | import static org.hamcrest.core.Is.is; |
| 44 | import static org.junit.Assert.assertNull; |
| 45 | import static org.junit.Assert.assertThat; |
| 46 | |
| 47 | |
| 48 | public class DefaultAsyncAtomicValueTest { |
| 49 | DefaultAsyncAtomicValue defaultAsyncAtomicValue; |
| 50 | DefaultAsyncAtomicValue defaultAsyncAtomicValue1; |
| 51 | |
| 52 | |
| 53 | private AsyncConsistentMap<String, byte[]> asyncMap; |
| 54 | private Map<String, Versioned<byte[]>> map; |
| 55 | |
| 56 | private Serializer serializer; |
| 57 | private MeteringAgent meteringAgent; |
| 58 | |
| 59 | |
| 60 | private static final String NAME = "atomicValue"; |
| 61 | private static final String NAME1 = "atomicValue1"; |
| 62 | private static final String TEST = "foo"; |
| 63 | private static final String TEST1 = "bar"; |
| 64 | private static final int INTNAME = 20; |
| 65 | private static final long VERSION1 = 1; |
| 66 | |
| 67 | private final byte[] value1 = Tools.getBytesUtf8(NAME); |
| 68 | private final byte[] value2 = Tools.getBytesUtf8(NAME1); |
| 69 | private final byte[] value3 = Tools.getBytesUtf8("tester"); |
| 70 | private final byte[] defaultValue = Tools.getBytesUtf8("default"); |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() throws Exception { |
| 74 | map = new HashMap<>(); |
| 75 | asyncMap = new AsyncConsistentMap<String, byte[]>() { |
| 76 | @Override |
| 77 | public CompletableFuture<Integer> size() { |
| 78 | return CompletableFuture.completedFuture(map.size()); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public CompletableFuture<Boolean> containsKey(String key) { |
| 83 | return CompletableFuture.completedFuture(map.containsKey(key)); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public CompletableFuture<Boolean> containsValue(byte[] value) { |
| 88 | return CompletableFuture.completedFuture(map.containsValue(value)); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public CompletableFuture<Versioned<byte[]>> get(String key) { |
| 93 | return CompletableFuture.completedFuture(map.get(key)); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public CompletableFuture<Versioned<byte[]>> getOrDefault(String key, byte[] defaultValue) { |
| 98 | return CompletableFuture.completedFuture(map.getOrDefault(key, new Versioned<byte[]>(defaultValue, |
| 99 | VERSION1))); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public CompletableFuture<Versioned<byte[]>> computeIf(String key, Predicate<? super byte[]> condition, |
| 104 | BiFunction<? super String, ? super byte[], |
| 105 | ? extends byte[]> remappingFunction) { |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Versioned<byte[]>> put(String key, byte[] value) { |
| 111 | return CompletableFuture.completedFuture(map.put(key, new Versioned<byte[]>(value, VERSION1))); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | @Override |
| 116 | public CompletableFuture<Versioned<byte[]>> putAndGet(String key, byte[] value) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public CompletableFuture<Versioned<byte[]>> remove(String key) { |
| 122 | return CompletableFuture.completedFuture(map.remove(key)); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public CompletableFuture<Void> clear() { |
| 127 | return null; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public CompletableFuture<Set<String>> keySet() { |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public CompletableFuture<Collection<Versioned<byte[]>>> values() { |
| 137 | return CompletableFuture.completedFuture(map.values()); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public CompletableFuture<Set<Map.Entry<String, Versioned<byte[]>>>> entrySet() { |
| 142 | return CompletableFuture.completedFuture(map.entrySet()); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public CompletableFuture<Versioned<byte[]>> putIfAbsent(String key, byte[] value) { |
| 147 | return CompletableFuture.completedFuture(map.putIfAbsent(key, new Versioned<byte[]>(value, 2))); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public CompletableFuture<Boolean> remove(String key, byte[] value) { |
| 152 | return CompletableFuture.completedFuture(map.remove(key, value)); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public CompletableFuture<Boolean> remove(String key, long version) { |
| 157 | Versioned versioned = map.get(key); |
| 158 | if (versioned.version() == version) { |
| 159 | map.remove(key); |
| 160 | return CompletableFuture.completedFuture(true); |
| 161 | } |
| 162 | return CompletableFuture.completedFuture(false); |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public CompletableFuture<Versioned<byte[]>> replace(String key, byte[] value) { |
| 167 | return CompletableFuture.completedFuture(map.replace(key, new Versioned<byte[]>(value, VERSION1))); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public CompletableFuture<Boolean> replace(String key, byte[] oldValue, byte[] newValue) { |
| 172 | Versioned<byte[]> currentValue = map.get(key); |
| 173 | if (currentValue == null) { |
| 174 | return CompletableFuture.completedFuture(false); |
| 175 | } |
| 176 | |
| 177 | if (Arrays.equals(currentValue.value(), oldValue)) { |
| 178 | map.put(key, new Versioned<>(newValue, VERSION1)); |
| 179 | return CompletableFuture.completedFuture(true); |
| 180 | } |
| 181 | return CompletableFuture.completedFuture(false); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public CompletableFuture<Boolean> replace(String key, long oldVersion, byte[] newValue) { |
| 186 | Versioned versioned = map.get(key); |
| 187 | if (versioned != null && versioned.version() == oldVersion) { |
| 188 | map.put(key, new Versioned<byte[]>(newValue, VERSION1)); |
| 189 | return CompletableFuture.completedFuture(true); |
| 190 | } |
| 191 | return CompletableFuture.completedFuture(false); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public CompletableFuture<Void> addListener(MapEventListener<String, byte[]> listener, Executor executor) { |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public CompletableFuture<Void> removeListener(MapEventListener<String, byte[]> listener) { |
| 201 | return null; |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public String name() { |
| 206 | return null; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public CompletableFuture<Version> begin(TransactionId transactionId) { |
| 211 | return null; |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public CompletableFuture<Boolean> prepare(TransactionLog<MapUpdate<String, byte[]>> transactionLog) { |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public CompletableFuture<Boolean> prepareAndCommit(TransactionLog<MapUpdate<String, |
| 221 | byte[]>> transactionLog) { |
| 222 | return null; |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public CompletableFuture<Void> commit(TransactionId transactionId) { |
| 227 | return null; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public CompletableFuture<Void> rollback(TransactionId transactionId) { |
| 232 | return null; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | serializer = Serializer.using(KryoNamespaces.BASIC); |
| 237 | meteringAgent = new MeteringAgent(NAME, "*", false); |
| 238 | defaultAsyncAtomicValue = new DefaultAsyncAtomicValue(NAME, serializer, |
| 239 | asyncMap, meteringAgent); |
| 240 | defaultAsyncAtomicValue1 = new DefaultAsyncAtomicValue(NAME1, serializer, |
| 241 | asyncMap, meteringAgent); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | @After |
| 246 | public void tearDown() throws Exception { |
| 247 | defaultAsyncAtomicValue.destroy(); |
| 248 | } |
| 249 | |
| 250 | @Test |
| 251 | public void testAsyncMapping() { |
| 252 | assertThat(asyncMap.size().join(), is(0)); |
| 253 | asyncMap.put(TEST, value1); |
| 254 | asyncMap.put(TEST1, value2); |
| 255 | asyncMap.put("default", defaultValue); |
| 256 | |
| 257 | assertThat(asyncMap.getOrDefault("noMatch", defaultValue).join().value(), |
| 258 | is(asyncMap.get("default").join().value())); |
| 259 | |
| 260 | assertThat(asyncMap.size().join(), is(3)); |
| 261 | assertThat(asyncMap.get(TEST).join().value(), is(value1)); |
| 262 | |
| 263 | assertThat(asyncMap.getOrDefault(TEST, Tools.getBytesUtf8("newTest")).join().value(), |
| 264 | is(asyncMap.get(TEST).join().value())); |
| 265 | |
| 266 | assertThat(asyncMap.containsKey(TEST).join(), is(true)); |
| 267 | |
| 268 | asyncMap.put(TEST, value3); |
| 269 | assertThat(asyncMap.get(TEST).join().value(), is(value3)); |
| 270 | asyncMap.putIfAbsent(TEST, value3); |
| 271 | assertThat(asyncMap.size().join(), is(3)); |
| 272 | |
| 273 | asyncMap.replace(TEST, value3, value1); |
| 274 | assertThat(asyncMap.get(TEST).join().value(), is(value1)); |
| 275 | |
| 276 | asyncMap.replace(TEST, VERSION1, value3); |
| 277 | assertThat(asyncMap.get(TEST).join().value(), is(value3)); |
| 278 | |
| 279 | asyncMap.replace(TEST, value3, defaultValue); |
| 280 | assertThat(asyncMap.get(TEST).join().value(), is(defaultValue)); |
| 281 | asyncMap.replace(TEST, value1); |
| 282 | assertThat(asyncMap.get(TEST).join().value(), is(value1)); |
| 283 | |
| 284 | asyncMap.remove(TEST, value2); |
| 285 | |
| 286 | assertThat(asyncMap.size().join(), is(3)); |
| 287 | } |
| 288 | |
| 289 | @Test |
| 290 | public void testAsync() { |
| 291 | asyncMap.put(TEST, value1); |
| 292 | asyncMap.put(TEST1, value2); |
| 293 | |
| 294 | assertNull(defaultAsyncAtomicValue.get().join()); |
| 295 | defaultAsyncAtomicValue = new DefaultAsyncAtomicValue(NAME, serializer, |
| 296 | asyncMap, meteringAgent); |
| 297 | assertThat(defaultAsyncAtomicValue.name(), is(NAME)); |
| 298 | defaultAsyncAtomicValue.set(null); |
| 299 | assertNull(defaultAsyncAtomicValue.get().join()); |
| 300 | |
| 301 | defaultAsyncAtomicValue.set(INTNAME); |
| 302 | assertThat(defaultAsyncAtomicValue.get().join(), is(INTNAME)); |
| 303 | |
| 304 | defaultAsyncAtomicValue.set(value1); |
| 305 | assertThat(defaultAsyncAtomicValue.get().join(), is(value1)); |
| 306 | |
| 307 | defaultAsyncAtomicValue.compareAndSet(value1, value3).join(); |
| 308 | assertThat(defaultAsyncAtomicValue.get().join(), is(value3)); |
| 309 | |
| 310 | assertThat(defaultAsyncAtomicValue.compareAndSet(value3, value1).join(), |
| 311 | is(true)); |
| 312 | assertThat(defaultAsyncAtomicValue.get().join(), is(value1)); |
| 313 | |
| 314 | defaultAsyncAtomicValue.getAndSet(null); |
| 315 | assertNull(defaultAsyncAtomicValue.get().join()); |
| 316 | |
| 317 | defaultAsyncAtomicValue.getAndSet(value3); |
| 318 | assertThat(defaultAsyncAtomicValue.get().join(), is(value3)); |
| 319 | } |
| 320 | } |