Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [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 | 5a1053e | 2017-05-19 18:03: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.resources.impl; |
| 17 | |
| 18 | import java.util.concurrent.CompletableFuture; |
| 19 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 20 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 21 | import io.atomix.protocols.raft.service.RaftService; |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | |
| 26 | /** |
| 27 | * Unit test for {@code AtomixIdGenerator}. |
| 28 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 29 | public class AtomixIdGeneratorTest extends AtomixTestBase<AtomixCounter> { |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 30 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 31 | @Override |
| 32 | protected RaftService createService() { |
| 33 | return new AtomixCounterService(); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 37 | protected AtomixCounter createPrimitive(RaftProxy proxy) { |
| 38 | return new AtomixCounter(proxy); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Tests generating IDs. |
| 43 | */ |
| 44 | @Test |
| 45 | public void testNextId() throws Throwable { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 46 | AtomixIdGenerator idGenerator1 = new AtomixIdGenerator(newPrimitive("testNextId")); |
| 47 | AtomixIdGenerator idGenerator2 = new AtomixIdGenerator(newPrimitive("testNextId")); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 48 | |
| 49 | CompletableFuture<Long> future11 = idGenerator1.nextId(); |
| 50 | CompletableFuture<Long> future12 = idGenerator1.nextId(); |
| 51 | CompletableFuture<Long> future13 = idGenerator1.nextId(); |
| 52 | assertEquals(Long.valueOf(1), future11.join()); |
| 53 | assertEquals(Long.valueOf(2), future12.join()); |
| 54 | assertEquals(Long.valueOf(3), future13.join()); |
| 55 | |
| 56 | CompletableFuture<Long> future21 = idGenerator1.nextId(); |
| 57 | CompletableFuture<Long> future22 = idGenerator1.nextId(); |
| 58 | CompletableFuture<Long> future23 = idGenerator1.nextId(); |
| 59 | assertEquals(Long.valueOf(6), future23.join()); |
| 60 | assertEquals(Long.valueOf(5), future22.join()); |
| 61 | assertEquals(Long.valueOf(4), future21.join()); |
| 62 | |
| 63 | CompletableFuture<Long> future31 = idGenerator2.nextId(); |
| 64 | CompletableFuture<Long> future32 = idGenerator2.nextId(); |
| 65 | CompletableFuture<Long> future33 = idGenerator2.nextId(); |
| 66 | assertEquals(Long.valueOf(1001), future31.join()); |
| 67 | assertEquals(Long.valueOf(1002), future32.join()); |
| 68 | assertEquals(Long.valueOf(1003), future33.join()); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Tests generating IDs. |
| 73 | */ |
| 74 | @Test |
| 75 | public void testNextIdBatchRollover() throws Throwable { |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 76 | AtomixIdGenerator idGenerator1 = new AtomixIdGenerator(newPrimitive("testNextIdBatchRollover"), 2); |
| 77 | AtomixIdGenerator idGenerator2 = new AtomixIdGenerator(newPrimitive("testNextIdBatchRollover"), 2); |
Jordan Halterman | 5a1053e | 2017-05-19 18:03:47 -0700 | [diff] [blame] | 78 | |
| 79 | CompletableFuture<Long> future11 = idGenerator1.nextId(); |
| 80 | CompletableFuture<Long> future12 = idGenerator1.nextId(); |
| 81 | CompletableFuture<Long> future13 = idGenerator1.nextId(); |
| 82 | assertEquals(Long.valueOf(1), future11.join()); |
| 83 | assertEquals(Long.valueOf(2), future12.join()); |
| 84 | assertEquals(Long.valueOf(3), future13.join()); |
| 85 | |
| 86 | CompletableFuture<Long> future21 = idGenerator2.nextId(); |
| 87 | CompletableFuture<Long> future22 = idGenerator2.nextId(); |
| 88 | CompletableFuture<Long> future23 = idGenerator2.nextId(); |
| 89 | assertEquals(Long.valueOf(5), future21.join()); |
| 90 | assertEquals(Long.valueOf(6), future22.join()); |
| 91 | assertEquals(Long.valueOf(7), future23.join()); |
| 92 | |
| 93 | CompletableFuture<Long> future14 = idGenerator1.nextId(); |
| 94 | CompletableFuture<Long> future15 = idGenerator1.nextId(); |
| 95 | CompletableFuture<Long> future16 = idGenerator1.nextId(); |
| 96 | assertEquals(Long.valueOf(4), future14.join()); |
| 97 | assertEquals(Long.valueOf(9), future15.join()); |
| 98 | assertEquals(Long.valueOf(10), future16.join()); |
| 99 | } |
| 100 | } |