Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 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; |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 22 | import org.onosproject.store.serializers.KryoNamespaces; |
| 23 | import org.onosproject.store.service.AsyncConsistentMap; |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 24 | import org.onosproject.store.service.Serializer; |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 25 | import org.onosproject.utils.MeteringAgent; |
| 26 | |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 27 | import static org.hamcrest.core.Is.is; |
| 28 | import static org.junit.Assert.assertNull; |
| 29 | import static org.junit.Assert.assertThat; |
| 30 | |
| 31 | |
| 32 | public class DefaultAsyncAtomicValueTest { |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 33 | DefaultAsyncAtomicValue<String> defaultAsyncAtomicValue; |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 34 | |
| 35 | private AsyncConsistentMap<String, byte[]> asyncMap; |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 36 | |
| 37 | private Serializer serializer; |
| 38 | private MeteringAgent meteringAgent; |
| 39 | |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 40 | private static final String MAPNAME = "map1"; |
| 41 | |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 42 | |
| 43 | private static final String NAME = "atomicValue"; |
| 44 | private static final String NAME1 = "atomicValue1"; |
| 45 | private static final String TEST = "foo"; |
| 46 | private static final String TEST1 = "bar"; |
| 47 | private static final int INTNAME = 20; |
| 48 | private static final long VERSION1 = 1; |
| 49 | |
| 50 | private final byte[] value1 = Tools.getBytesUtf8(NAME); |
| 51 | private final byte[] value2 = Tools.getBytesUtf8(NAME1); |
| 52 | private final byte[] value3 = Tools.getBytesUtf8("tester"); |
| 53 | private final byte[] defaultValue = Tools.getBytesUtf8("default"); |
| 54 | |
| 55 | @Before |
| 56 | public void setUp() throws Exception { |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 57 | asyncMap = new AsyncConsistentMapMock<>(); |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 58 | serializer = Serializer.using(KryoNamespaces.BASIC); |
| 59 | meteringAgent = new MeteringAgent(NAME, "*", false); |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 60 | defaultAsyncAtomicValue = new DefaultAsyncAtomicValue(MAPNAME, serializer, |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 61 | asyncMap, meteringAgent); |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @After |
| 65 | public void tearDown() throws Exception { |
| 66 | defaultAsyncAtomicValue.destroy(); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testAsyncMapping() { |
| 71 | assertThat(asyncMap.size().join(), is(0)); |
| 72 | asyncMap.put(TEST, value1); |
| 73 | asyncMap.put(TEST1, value2); |
| 74 | asyncMap.put("default", defaultValue); |
| 75 | |
| 76 | assertThat(asyncMap.getOrDefault("noMatch", defaultValue).join().value(), |
| 77 | is(asyncMap.get("default").join().value())); |
| 78 | |
| 79 | assertThat(asyncMap.size().join(), is(3)); |
| 80 | assertThat(asyncMap.get(TEST).join().value(), is(value1)); |
| 81 | |
| 82 | assertThat(asyncMap.getOrDefault(TEST, Tools.getBytesUtf8("newTest")).join().value(), |
| 83 | is(asyncMap.get(TEST).join().value())); |
| 84 | |
| 85 | assertThat(asyncMap.containsKey(TEST).join(), is(true)); |
| 86 | |
| 87 | asyncMap.put(TEST, value3); |
| 88 | assertThat(asyncMap.get(TEST).join().value(), is(value3)); |
| 89 | asyncMap.putIfAbsent(TEST, value3); |
| 90 | assertThat(asyncMap.size().join(), is(3)); |
| 91 | |
| 92 | asyncMap.replace(TEST, value3, value1); |
| 93 | assertThat(asyncMap.get(TEST).join().value(), is(value1)); |
| 94 | |
| 95 | asyncMap.replace(TEST, VERSION1, value3); |
| 96 | assertThat(asyncMap.get(TEST).join().value(), is(value3)); |
| 97 | |
| 98 | asyncMap.replace(TEST, value3, defaultValue); |
| 99 | assertThat(asyncMap.get(TEST).join().value(), is(defaultValue)); |
| 100 | asyncMap.replace(TEST, value1); |
| 101 | assertThat(asyncMap.get(TEST).join().value(), is(value1)); |
| 102 | |
| 103 | asyncMap.remove(TEST, value2); |
| 104 | |
| 105 | assertThat(asyncMap.size().join(), is(3)); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testAsync() { |
| 110 | asyncMap.put(TEST, value1); |
| 111 | asyncMap.put(TEST1, value2); |
| 112 | |
| 113 | assertNull(defaultAsyncAtomicValue.get().join()); |
| 114 | defaultAsyncAtomicValue = new DefaultAsyncAtomicValue(NAME, serializer, |
| 115 | asyncMap, meteringAgent); |
| 116 | assertThat(defaultAsyncAtomicValue.name(), is(NAME)); |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 117 | defaultAsyncAtomicValue.set(null).join(); |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 118 | assertNull(defaultAsyncAtomicValue.get().join()); |
| 119 | |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 120 | defaultAsyncAtomicValue.set(NAME).join(); |
| 121 | assertThat(defaultAsyncAtomicValue.get().join(), is(NAME)); |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 122 | |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 123 | defaultAsyncAtomicValue.set(NAME1).join(); |
| 124 | assertThat(defaultAsyncAtomicValue.get().join(), is(NAME1)); |
| 125 | defaultAsyncAtomicValue.compareAndSet(NAME1, NAME).join(); |
| 126 | assertThat(defaultAsyncAtomicValue.get().join(), is(NAME)); |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 127 | |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 128 | |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 129 | defaultAsyncAtomicValue.getAndSet(null).join(); |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 130 | assertNull(defaultAsyncAtomicValue.get().join()); |
| 131 | |
Sbhat35 | ffe0234 | 2017-08-09 16:31:17 -0700 | [diff] [blame] | 132 | defaultAsyncAtomicValue.set(NAME1).join(); |
| 133 | assertThat(defaultAsyncAtomicValue.getAndSet(NAME).join(), is(NAME1)); |
| 134 | assertThat(defaultAsyncAtomicValue.getAndSet("new").join(), is(NAME)); |
| 135 | |
| 136 | assertThat(defaultAsyncAtomicValue.get().join(), is("new")); |
Sbhat35 | 7c90e4b | 2017-07-24 16:49:47 -0700 | [diff] [blame] | 137 | } |
| 138 | } |