blob: 2e334d6bf9e2789f94c6f86c70602b9303a8bbe5 [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani5e5b3d62016-02-01 16:03:33 -08003 *
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
Jordan Halterman2bf177c2017-06-29 01:49:08 -070018import io.atomix.protocols.raft.proxy.RaftProxy;
19import io.atomix.protocols.raft.service.RaftService;
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070020import org.junit.Test;
Madan Jampani5e5b3d62016-02-01 16:03:33 -080021
Aaron Kruglikovc0c27c02016-06-07 16:05:00 -070022import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertFalse;
24import static org.junit.Assert.assertTrue;
25
Jordan Halterman2bf177c2017-06-29 01:49:08 -070026/**
Madan Jampani5e5b3d62016-02-01 16:03:33 -080027 * Unit tests for {@link AtomixCounter}.
28 */
Jordan Halterman2bf177c2017-06-29 01:49:08 -070029public class AtomixCounterTest extends AtomixTestBase<AtomixCounter> {
30 @Override
31 protected RaftService createService() {
32 return new AtomixCounterService();
Aaron Kruglikovb5a41e52016-06-23 15:37:41 -070033 }
34
Madan Jampani5e5b3d62016-02-01 16:03:33 -080035 @Override
Jordan Halterman2bf177c2017-06-29 01:49:08 -070036 protected AtomixCounter createPrimitive(RaftProxy proxy) {
37 return new AtomixCounter(proxy);
Madan Jampani5e5b3d62016-02-01 16:03:33 -080038 }
39
40 @Test
41 public void testBasicOperations() throws Throwable {
Aaron Kruglikovc38dc7f2016-07-19 10:00:11 -070042 basicOperationsTest();
Madan Jampani5e5b3d62016-02-01 16:03:33 -080043 }
44
Aaron Kruglikovc38dc7f2016-07-19 10:00:11 -070045 protected void basicOperationsTest() throws Throwable {
Jordan Halterman2bf177c2017-06-29 01:49:08 -070046 AtomixCounter along = newPrimitive("test-counter-basic-operations");
Madan Jampani5e5b3d62016-02-01 16:03:33 -080047 assertEquals(0, along.get().join().longValue());
48 assertEquals(1, along.incrementAndGet().join().longValue());
49 along.set(100).join();
50 assertEquals(100, along.get().join().longValue());
51 assertEquals(100, along.getAndAdd(10).join().longValue());
52 assertEquals(110, along.get().join().longValue());
53 assertFalse(along.compareAndSet(109, 111).join());
54 assertTrue(along.compareAndSet(110, 111).join());
55 assertEquals(100, along.addAndGet(-11).join().longValue());
56 assertEquals(100, along.getAndIncrement().join().longValue());
57 assertEquals(101, along.get().join().longValue());
58 }
59}