PageRenderTime 19ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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