blob: 2a9ca026324aa64a40cd299aedd060a01511193c [file] [log] [blame]
Jordan Halterman5a1053e2017-05-19 18:03:47 -07001/*
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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import java.util.concurrent.CompletableFuture;
19
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020import io.atomix.protocols.raft.proxy.RaftProxy;
21import io.atomix.protocols.raft.service.RaftService;
Jordan Halterman5a1053e2017-05-19 18:03:47 -070022import org.junit.Test;
23
24import static org.junit.Assert.assertEquals;
25
26/**
27 * Unit test for {@code AtomixIdGenerator}.
28 */
Jordan Halterman2bf177c2017-06-29 01:49:08 -070029public class AtomixIdGeneratorTest extends AtomixTestBase<AtomixCounter> {
Jordan Halterman5a1053e2017-05-19 18:03:47 -070030
Jordan Halterman2bf177c2017-06-29 01:49:08 -070031 @Override
32 protected RaftService createService() {
33 return new AtomixCounterService();
Jordan Halterman5a1053e2017-05-19 18:03:47 -070034 }
35
36 @Override
Jordan Halterman2bf177c2017-06-29 01:49:08 -070037 protected AtomixCounter createPrimitive(RaftProxy proxy) {
38 return new AtomixCounter(proxy);
Jordan Halterman5a1053e2017-05-19 18:03:47 -070039 }
40
41 /**
42 * Tests generating IDs.
43 */
44 @Test
45 public void testNextId() throws Throwable {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070046 AtomixIdGenerator idGenerator1 = new AtomixIdGenerator(newPrimitive("testNextId"));
47 AtomixIdGenerator idGenerator2 = new AtomixIdGenerator(newPrimitive("testNextId"));
Jordan Halterman5a1053e2017-05-19 18:03:47 -070048
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 Halterman2bf177c2017-06-29 01:49:08 -070076 AtomixIdGenerator idGenerator1 = new AtomixIdGenerator(newPrimitive("testNextIdBatchRollover"), 2);
77 AtomixIdGenerator idGenerator2 = new AtomixIdGenerator(newPrimitive("testNextIdBatchRollover"), 2);
Jordan Halterman5a1053e2017-05-19 18:03:47 -070078
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}