Ray Milkey | 2eb9167 | 2018-04-24 10:07:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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 | */ |
| 16 | |
| 17 | package org.onosproject.store.primitives; |
| 18 | |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 22 | import org.onosproject.store.service.StorageException; |
| 23 | |
| 24 | import java.util.concurrent.CompletableFuture; |
| 25 | |
| 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.hamcrest.Matchers.notNullValue; |
| 29 | import static org.onosproject.store.primitives.TestingCompletableFutures.ErrorState.NONE; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Unit tests for the DefaultAtomicCounter class. |
| 34 | */ |
| 35 | |
| 36 | public class DefaultAtomicCounterTest { |
| 37 | |
| 38 | private static final long INITIAL_VALUE = 33L; |
| 39 | private static final long ADDED_VALUE = 44L; |
| 40 | |
| 41 | private DefaultAtomicCounter atomicCounter; |
| 42 | |
| 43 | |
| 44 | private DefaultAtomicCounter create() { |
| 45 | AsyncAtomicCounter atomicCounter = AsyncAtomicCounterAdapter.builder().build(); |
| 46 | DefaultAtomicCounter counter = new DefaultAtomicCounter(atomicCounter, 1000); |
| 47 | counter.set(INITIAL_VALUE); |
| 48 | assertThat(counter.get(), is(INITIAL_VALUE)); |
| 49 | return counter; |
| 50 | } |
| 51 | |
| 52 | @Before |
| 53 | public void setUpCounter() { |
| 54 | atomicCounter = create(); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testConstruction() { |
| 59 | assertThat(atomicCounter, notNullValue()); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testIncrements() { |
| 64 | long beforeIncrement = atomicCounter.getAndIncrement(); |
| 65 | assertThat(beforeIncrement, is(INITIAL_VALUE)); |
| 66 | assertThat(atomicCounter.get(), is(INITIAL_VALUE + 1)); |
| 67 | |
| 68 | atomicCounter.set(INITIAL_VALUE); |
| 69 | long afterIncrement = atomicCounter.incrementAndGet(); |
| 70 | assertThat(afterIncrement, is(INITIAL_VALUE + 1)); |
| 71 | assertThat(atomicCounter.get(), is(INITIAL_VALUE + 1)); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testAdds() { |
| 76 | long beforeIncrement = atomicCounter.getAndAdd(ADDED_VALUE); |
| 77 | assertThat(beforeIncrement, is(INITIAL_VALUE)); |
| 78 | assertThat(atomicCounter.get(), is(INITIAL_VALUE + ADDED_VALUE)); |
| 79 | |
| 80 | atomicCounter.set(INITIAL_VALUE); |
| 81 | long afterIncrement = atomicCounter.addAndGet(ADDED_VALUE); |
| 82 | assertThat(afterIncrement, is(INITIAL_VALUE + ADDED_VALUE)); |
| 83 | assertThat(atomicCounter.get(), is(INITIAL_VALUE + ADDED_VALUE)); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testCompareAndSet() { |
| 88 | boolean compareTrue = atomicCounter.compareAndSet(INITIAL_VALUE, ADDED_VALUE); |
| 89 | assertThat(compareTrue, is(true)); |
| 90 | |
| 91 | boolean compareFalse = atomicCounter.compareAndSet(INITIAL_VALUE, ADDED_VALUE); |
| 92 | assertThat(compareFalse, is(false)); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | class AtomicCounterWithErrors extends AsyncAtomicCounterAdapter { |
| 98 | |
| 99 | TestingCompletableFutures.ErrorState errorState = NONE; |
| 100 | |
| 101 | void setErrorState(TestingCompletableFutures.ErrorState errorState) { |
| 102 | this.errorState = errorState; |
| 103 | } |
| 104 | |
| 105 | AtomicCounterWithErrors() { |
| 106 | super(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public CompletableFuture<Long> get() { |
| 111 | return TestingCompletableFutures.createFuture(errorState); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Test(expected = StorageException.Timeout.class) |
| 116 | public void testTimeout() { |
| 117 | AtomicCounterWithErrors atomicCounter = new AtomicCounterWithErrors(); |
| 118 | atomicCounter.setErrorState(TestingCompletableFutures.ErrorState.TIMEOUT_EXCEPTION); |
| 119 | DefaultAtomicCounter counter = new DefaultAtomicCounter(atomicCounter, 1000); |
| 120 | |
| 121 | counter.get(); |
| 122 | } |
| 123 | |
| 124 | @Test(expected = StorageException.Interrupted.class) |
| 125 | public void testInterrupted() { |
| 126 | AtomicCounterWithErrors atomicCounter = new AtomicCounterWithErrors(); |
| 127 | atomicCounter.setErrorState(TestingCompletableFutures.ErrorState.INTERRUPTED_EXCEPTION); |
| 128 | DefaultAtomicCounter counter = new DefaultAtomicCounter(atomicCounter, 1000); |
| 129 | |
| 130 | counter.get(); |
| 131 | } |
| 132 | |
| 133 | @Test(expected = StorageException.class) |
| 134 | public void testExecutionError() { |
| 135 | AtomicCounterWithErrors atomicCounter = new AtomicCounterWithErrors(); |
| 136 | atomicCounter.setErrorState(TestingCompletableFutures.ErrorState.EXECUTION_EXCEPTION); |
| 137 | DefaultAtomicCounter counter = new DefaultAtomicCounter(atomicCounter, 1000); |
| 138 | |
| 139 | counter.get(); |
| 140 | } |
| 141 | } |