blob: d38400ddfa1030125ffe311157b093f682e1add8 [file] [log] [blame]
Madan Jampani5e5b3d62016-02-01 16:03:33 -08001/*
2 * Copyright 2016 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 static org.junit.Assert.*;
19
20import org.junit.Test;
21
22import io.atomix.Atomix;
23import io.atomix.resource.ResourceType;
24import io.atomix.variables.DistributedLong;
25
26/**
27 * Unit tests for {@link AtomixCounter}.
28 */
29public class AtomixLongTest extends AtomixTestBase {
30
31 @Override
32 protected ResourceType resourceType() {
33 return new ResourceType(DistributedLong.class);
34 }
35
36 @Test
37 public void testBasicOperations() throws Throwable {
38 basicOperationsTest(1);
39 clearTests();
40 basicOperationsTest(2);
41 clearTests();
42 basicOperationsTest(3);
43 clearTests();
44 }
45
46 protected void basicOperationsTest(int clusterSize) throws Throwable {
47 createCopycatServers(clusterSize);
48 Atomix atomix = createAtomixClient();
49 AtomixCounter along = new AtomixCounter("test-long", atomix.getLong("test-long").join());
50 assertEquals(0, along.get().join().longValue());
51 assertEquals(1, along.incrementAndGet().join().longValue());
52 along.set(100).join();
53 assertEquals(100, along.get().join().longValue());
54 assertEquals(100, along.getAndAdd(10).join().longValue());
55 assertEquals(110, along.get().join().longValue());
56 assertFalse(along.compareAndSet(109, 111).join());
57 assertTrue(along.compareAndSet(110, 111).join());
58 assertEquals(100, along.addAndGet(-11).join().longValue());
59 assertEquals(100, along.getAndIncrement().join().longValue());
60 assertEquals(101, along.get().join().longValue());
61 }
62}