PageRenderTime 22ms CodeModel.GetById 7ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast-client/src/test/java/com/hazelcast/client/HazelcastClientAtomicNumberTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 93 lines | 70 code | 8 blank | 15 comment | 0 complexity | 8ff25599c1f7033b56d3478deaaae8e8 MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 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
17package com.hazelcast.client;
18
19import com.hazelcast.config.Config;
20import com.hazelcast.core.AtomicNumber;
21import com.hazelcast.core.Hazelcast;
22import com.hazelcast.core.HazelcastInstance;
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27
28import static com.hazelcast.client.TestUtility.newHazelcastClient;
29import static junit.framework.Assert.assertTrue;
30import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.assertFalse;
32
33@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
34public class HazelcastClientAtomicNumberTest {
35
36    @Before
37    @After
38    public void after() throws Exception {
39        HazelcastClient.shutdownAll();
40        Hazelcast.shutdownAll();
41    }
42
43    @Test
44    public void testAtomicLong() {
45        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
46        HazelcastClient client = newHazelcastClient(h1);
47        AtomicNumber an = client.getAtomicNumber("testAtomicLong");
48        assertEquals(0, an.get());
49        assertEquals(-1, an.decrementAndGet());
50        assertEquals(0, an.incrementAndGet());
51        assertEquals(1, an.incrementAndGet());
52        assertEquals(2, an.incrementAndGet());
53        assertEquals(1, an.decrementAndGet());
54        assertEquals(1, an.getAndSet(23));
55        assertEquals(28, an.addAndGet(5));
56        assertEquals(28, an.get());
57        assertEquals(28, an.getAndAdd(-3));
58        assertEquals(24, an.decrementAndGet());
59        assertFalse(an.compareAndSet(23, 50));
60        assertTrue(an.compareAndSet(24, 50));
61        assertTrue(an.compareAndSet(50, 0));
62    }
63
64    @Test
65    public void testSimple() throws Exception {
66        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(new Config());
67        HazelcastClient client = newHazelcastClient(h1);
68        final String name = "simple";
69        final AtomicNumber nodeAtomicLong = h1.getAtomicNumber(name);
70        final AtomicNumber clientAtomicLong = client.getAtomicNumber(name);
71        check(nodeAtomicLong, clientAtomicLong, 0L);
72        assertEquals(1L, clientAtomicLong.incrementAndGet());
73        check(nodeAtomicLong, clientAtomicLong, 1L);
74        assertEquals(1L, clientAtomicLong.getAndAdd(1));
75        check(nodeAtomicLong, clientAtomicLong, 2L);
76        assertEquals(1L, nodeAtomicLong.decrementAndGet());
77        check(nodeAtomicLong, clientAtomicLong, 1L);
78        assertEquals(2L, clientAtomicLong.addAndGet(1L));
79        check(nodeAtomicLong, clientAtomicLong, 2L);
80        clientAtomicLong.set(3L);
81        check(nodeAtomicLong, clientAtomicLong, 3L);
82        assertFalse(nodeAtomicLong.compareAndSet(4L, 1L));
83        check(nodeAtomicLong, clientAtomicLong, 3L);
84        assertTrue(clientAtomicLong.compareAndSet(3L, 1L));
85        check(nodeAtomicLong, clientAtomicLong, 1L);
86    }
87
88    private void check(final AtomicNumber nodeAtomicLong,
89                       final AtomicNumber clientAtomicLong, final long expectedValue) {
90        assertEquals(expectedValue, nodeAtomicLong.get());
91        assertEquals(expectedValue, clientAtomicLong.get());
92    }
93}