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