Jordan Halterman | c955df7 | 2017-02-04 20:43:28 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.resources.impl; |
| 17 | |
| 18 | import io.atomix.resource.ResourceType; |
| 19 | import org.junit.AfterClass; |
| 20 | import org.junit.BeforeClass; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
| 26 | /** |
| 27 | * Unit test for {@code AtomixCounterMap}. |
| 28 | */ |
| 29 | public class AtomixAtomicCounterMapTest extends AtomixTestBase { |
| 30 | |
| 31 | @BeforeClass |
| 32 | public static void preTestSetup() throws Throwable { |
| 33 | createCopycatServers(3); |
| 34 | } |
| 35 | |
| 36 | @AfterClass |
| 37 | public static void postTestCleanup() throws Exception { |
| 38 | clearTests(); |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | protected ResourceType resourceType() { |
| 43 | return new ResourceType(AtomixAtomicCounterMap.class); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Tests basic counter map operations. |
| 48 | */ |
| 49 | @Test |
| 50 | public void testBasicCounterMapOperations() throws Throwable { |
| 51 | AtomixAtomicCounterMap map = createAtomixClient().getResource("testBasicCounterMapOperationMap", |
| 52 | AtomixAtomicCounterMap.class).join(); |
| 53 | |
| 54 | map.isEmpty().thenAccept(isEmpty -> { |
| 55 | assertTrue(isEmpty); |
| 56 | }).join(); |
| 57 | |
| 58 | map.size().thenAccept(size -> { |
| 59 | assertTrue(size == 0); |
| 60 | }).join(); |
| 61 | |
| 62 | map.put("foo", 2).thenAccept(value -> { |
| 63 | assertTrue(value == 0); |
| 64 | }).join(); |
| 65 | |
| 66 | map.incrementAndGet("foo").thenAccept(value -> { |
| 67 | assertTrue(value == 3); |
| 68 | }).join(); |
| 69 | |
| 70 | map.getAndIncrement("foo").thenAccept(value -> { |
| 71 | assertTrue(value == 3); |
| 72 | }).join(); |
| 73 | |
| 74 | map.get("foo").thenAccept(value -> { |
| 75 | assertTrue(value == 4); |
| 76 | }).join(); |
| 77 | |
| 78 | map.getAndDecrement("foo").thenAccept(value -> { |
| 79 | assertTrue(value == 4); |
| 80 | }).join(); |
| 81 | |
| 82 | map.decrementAndGet("foo").thenAccept(value -> { |
| 83 | assertTrue(value == 2); |
| 84 | }).join(); |
| 85 | |
| 86 | map.size().thenAccept(size -> { |
| 87 | assertTrue(size == 1); |
| 88 | }).join(); |
| 89 | |
| 90 | map.isEmpty().thenAccept(isEmpty -> { |
| 91 | assertFalse(isEmpty); |
| 92 | }).join(); |
| 93 | |
| 94 | map.clear().join(); |
| 95 | |
| 96 | map.isEmpty().thenAccept(isEmpty -> { |
| 97 | assertTrue(isEmpty); |
| 98 | }).join(); |
| 99 | |
| 100 | map.size().thenAccept(size -> { |
| 101 | assertTrue(size == 0); |
| 102 | }).join(); |
| 103 | |
| 104 | map.get("foo").thenAccept(value -> { |
| 105 | assertTrue(value == 0); |
| 106 | }).join(); |
| 107 | |
| 108 | map.incrementAndGet("bar").thenAccept(value -> { |
| 109 | assertTrue(value == 1); |
| 110 | }).join(); |
| 111 | |
| 112 | map.addAndGet("bar", 2).thenAccept(value -> { |
| 113 | assertTrue(value == 3); |
| 114 | }).join(); |
| 115 | |
| 116 | map.getAndAdd("bar", 3).thenAccept(value -> { |
| 117 | assertTrue(value == 3); |
| 118 | }).join(); |
| 119 | |
| 120 | map.get("bar").thenAccept(value -> { |
| 121 | assertTrue(value == 6); |
| 122 | }).join(); |
| 123 | |
| 124 | map.putIfAbsent("bar", 1).thenAccept(value -> { |
| 125 | assertTrue(value == 6); |
| 126 | }).join(); |
| 127 | |
| 128 | map.replace("bar", 6, 1).thenAccept(succeeded -> { |
| 129 | assertTrue(succeeded); |
| 130 | }).join(); |
| 131 | |
| 132 | map.replace("bar", 6, 1).thenAccept(succeeded -> { |
| 133 | assertFalse(succeeded); |
| 134 | }).join(); |
| 135 | |
| 136 | map.size().thenAccept(size -> { |
| 137 | assertTrue(size == 1); |
| 138 | }).join(); |
| 139 | |
| 140 | map.remove("bar").thenAccept(value -> { |
| 141 | assertTrue(value == 1); |
| 142 | }).join(); |
| 143 | |
| 144 | map.size().thenAccept(size -> { |
| 145 | assertTrue(size == 0); |
| 146 | }).join(); |
| 147 | |
| 148 | map.put("baz", 3).thenAccept(value -> { |
| 149 | assertTrue(value == 0); |
| 150 | }).join(); |
| 151 | |
| 152 | map.remove("baz", 2).thenAccept(removed -> { |
| 153 | assertFalse(removed); |
| 154 | }).join(); |
| 155 | |
| 156 | map.put("baz", 2).thenAccept(value -> { |
| 157 | assertTrue(value == 3); |
| 158 | }).join(); |
| 159 | |
| 160 | map.remove("baz", 2).thenAccept(removed -> { |
| 161 | assertTrue(removed); |
| 162 | }).join(); |
| 163 | |
| 164 | map.isEmpty().thenAccept(isEmpty -> { |
| 165 | assertTrue(isEmpty); |
| 166 | }).join(); |
| 167 | |
| 168 | map.replace("baz", 0, 5).thenAccept(replaced -> { |
| 169 | assertTrue(replaced); |
| 170 | }).join(); |
| 171 | |
| 172 | map.get("baz").thenAccept(value -> { |
| 173 | assertTrue(value == 5); |
| 174 | }).join(); |
| 175 | } |
| 176 | } |