Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [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 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 18 | import io.atomix.Atomix; |
| 19 | import io.atomix.resource.ResourceType; |
| 20 | import io.atomix.variables.DistributedLong; |
Aaron Kruglikov | b5a41e5 | 2016-06-23 15:37:41 -0700 | [diff] [blame] | 21 | import org.junit.AfterClass; |
| 22 | import org.junit.BeforeClass; |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 24 | |
Aaron Kruglikov | c0c27c0 | 2016-06-07 16:05:00 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertTrue; |
| 28 | |
| 29 | /**git s |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 30 | * Unit tests for {@link AtomixCounter}. |
| 31 | */ |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 32 | public class AtomixLongTest extends AtomixTestBase { |
| 33 | |
Aaron Kruglikov | b5a41e5 | 2016-06-23 15:37:41 -0700 | [diff] [blame] | 34 | @BeforeClass |
| 35 | public static void preTestSetup() throws Throwable { |
| 36 | createCopycatServers(3); |
| 37 | } |
| 38 | |
| 39 | @AfterClass |
| 40 | public static void postTestCleanup() throws Exception { |
| 41 | clearTests(); |
| 42 | } |
| 43 | |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 44 | @Override |
| 45 | protected ResourceType resourceType() { |
| 46 | return new ResourceType(DistributedLong.class); |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testBasicOperations() throws Throwable { |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 51 | basicOperationsTest(); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 52 | } |
| 53 | |
Aaron Kruglikov | c38dc7f | 2016-07-19 10:00:11 -0700 | [diff] [blame] | 54 | protected void basicOperationsTest() throws Throwable { |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 55 | Atomix atomix = createAtomixClient(); |
Aaron Kruglikov | b5a41e5 | 2016-06-23 15:37:41 -0700 | [diff] [blame] | 56 | AtomixCounter along = new AtomixCounter("test-long-basic-operations", |
| 57 | atomix.getLong("test-long").join()); |
Madan Jampani | 5e5b3d6 | 2016-02-01 16:03:33 -0800 | [diff] [blame] | 58 | assertEquals(0, along.get().join().longValue()); |
| 59 | assertEquals(1, along.incrementAndGet().join().longValue()); |
| 60 | along.set(100).join(); |
| 61 | assertEquals(100, along.get().join().longValue()); |
| 62 | assertEquals(100, along.getAndAdd(10).join().longValue()); |
| 63 | assertEquals(110, along.get().join().longValue()); |
| 64 | assertFalse(along.compareAndSet(109, 111).join()); |
| 65 | assertTrue(along.compareAndSet(110, 111).join()); |
| 66 | assertEquals(100, along.addAndGet(-11).join().longValue()); |
| 67 | assertEquals(100, along.getAndIncrement().join().longValue()); |
| 68 | assertEquals(101, along.get().join().longValue()); |
| 69 | } |
| 70 | } |