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