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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 158 lines · 128 code · 15 blank · 15 comment · 1 complexity · 3d7e2b8b1df398b8cb55d3efa9161612 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.core.HazelcastInstance;
  18. import com.hazelcast.core.IMap;
  19. import com.hazelcast.core.Transaction;
  20. import org.junit.AfterClass;
  21. import org.junit.Test;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Queue;
  25. import java.util.Set;
  26. import java.util.concurrent.CountDownLatch;
  27. import java.util.concurrent.TimeUnit;
  28. import static org.junit.Assert.*;
  29. public class HazelcastClientTransactionTest extends HazelcastClientTestBase {
  30. @Test
  31. public void rollbackTransactionMap() {
  32. HazelcastInstance hClient = getHazelcastClient();
  33. Transaction transaction = hClient.getTransaction();
  34. transaction.begin();
  35. Map<String, String> map = hClient.getMap("rollbackTransactionMap");
  36. map.put("1", "A");
  37. assertEquals("A", map.get("1"));
  38. transaction.rollback();
  39. assertNull(map.get("1"));
  40. }
  41. @Test
  42. public void commitTransactionMap() {
  43. HazelcastInstance hClient = getHazelcastClient();
  44. Transaction transaction = hClient.getTransaction();
  45. transaction.begin();
  46. Map<String, String> map = hClient.getMap("commitTransactionMap");
  47. map.put("1", "A");
  48. assertEquals("A", map.get("1"));
  49. transaction.commit();
  50. assertEquals("A", map.get("1"));
  51. }
  52. @Test
  53. public void testTransactionVisibilityFromDifferentThreads() throws InterruptedException {
  54. HazelcastInstance hClient = getHazelcastClient();
  55. final CountDownLatch latch = new CountDownLatch(1);
  56. final Object o = new Object();
  57. Transaction transaction = hClient.getTransaction();
  58. transaction.begin();
  59. final IMap<String, String> map = hClient.getMap("testTransactionVisibilityFromDifferentThreads");
  60. map.put("1", "A");
  61. assertEquals("A", map.get("1"));
  62. new Thread(new Runnable() {
  63. public void run() {
  64. assertNull(map.get("1"));
  65. if (!map.containsKey("1")) {
  66. latch.countDown();
  67. }
  68. synchronized (o) {
  69. o.notify();
  70. }
  71. }
  72. }).start();
  73. synchronized (o) {
  74. o.wait();
  75. }
  76. transaction.rollback();
  77. assertNull(map.get("1"));
  78. assertTrue(latch.await(1, TimeUnit.MICROSECONDS));
  79. }
  80. @Test
  81. public void rollbackTransactionList() {
  82. HazelcastInstance hClient = getHazelcastClient();
  83. Transaction transaction = hClient.getTransaction();
  84. transaction.begin();
  85. List<String> list = hClient.getList("rollbackTransactionList");
  86. list.add("Istanbul");
  87. transaction.rollback();
  88. assertTrue(list.isEmpty());
  89. }
  90. @Test
  91. public void commitTransactionList() {
  92. HazelcastInstance hClient = getHazelcastClient();
  93. Transaction transaction = hClient.getTransaction();
  94. transaction.begin();
  95. List<String> list = hClient.getList("commitTransactionList");
  96. list.add("Istanbul");
  97. transaction.commit();
  98. assertTrue(list.contains("Istanbul"));
  99. }
  100. @Test
  101. public void rollbackTransactionSet() {
  102. HazelcastInstance hClient = getHazelcastClient();
  103. Transaction transaction = hClient.getTransaction();
  104. transaction.begin();
  105. Set<String> set = hClient.getSet("rollbackTransactionSet");
  106. set.add("Istanbul");
  107. transaction.rollback();
  108. assertTrue(set.isEmpty());
  109. }
  110. @Test
  111. public void commitTransactionSet() {
  112. HazelcastInstance hClient = getHazelcastClient();
  113. Transaction transaction = hClient.getTransaction();
  114. transaction.begin();
  115. Set<String> set = hClient.getSet("commitTransactionSet");
  116. set.add("Istanbul");
  117. transaction.commit();
  118. assertTrue(set.contains("Istanbul"));
  119. }
  120. @Test
  121. public void rollbackTransactionQueue() {
  122. HazelcastInstance hClient = getHazelcastClient();
  123. Transaction transaction = hClient.getTransaction();
  124. transaction.begin();
  125. Queue<String> q = hClient.getQueue("rollbackTransactionQueue");
  126. q.offer("Istanbul");
  127. transaction.rollback();
  128. assertTrue(q.isEmpty());
  129. }
  130. @Test
  131. public void commitTransactionQueue() {
  132. HazelcastInstance hClient = getHazelcastClient();
  133. Transaction transaction = hClient.getTransaction();
  134. transaction.begin();
  135. Queue<String> q = hClient.getQueue("commitTransactionQueue");
  136. q.offer("Istanbul");
  137. transaction.commit();
  138. assertEquals("Istanbul", q.poll());
  139. }
  140. @AfterClass
  141. public static void shutdown() {
  142. }
  143. }