@Test
 public void testTransactionAtomicity_whenMultiMapGetIsUsed_withTransaction()
     throws InterruptedException {
   final HazelcastInstance hz = Hazelcast.newHazelcastInstance(createConfigWithDummyTxService());
   final String name = HazelcastTestSupport.generateRandomString(5);
   Thread producerThread = startProducerThread(hz, name);
   try {
     IQueue<String> q = hz.getQueue(name);
     for (int i = 0; i < 1000; i++) {
       String id = q.poll();
       if (id != null) {
         TransactionContext tx = hz.newTransactionContext();
         try {
           tx.beginTransaction();
           TransactionalMultiMap<Object, Object> multiMap = tx.getMultiMap(name);
           Collection<Object> values = multiMap.get(id);
           assertFalse(values.isEmpty());
           multiMap.remove(id);
           tx.commitTransaction();
         } catch (TransactionException e) {
           tx.rollbackTransaction();
           e.printStackTrace();
         }
       } else {
         LockSupport.parkNanos(100);
       }
     }
   } finally {
     stopProducerThread(producerThread);
   }
 }
예제 #2
0
  @Test
  public void testPutWithTTL() {
    final String mapName = randomString();
    final int ttlSeconds = 1;
    final String key = "key";
    final String value = "Value";
    final IMap map = client.getMap(mapName);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap<Object, Object> txnMap = context.getMap(mapName);

    txnMap.put(key, value, ttlSeconds, TimeUnit.SECONDS);
    Object resultFromClientWhileTxnInProgress = map.get(key);

    context.commitTransaction();

    assertNull(resultFromClientWhileTxnInProgress);
    assertEquals(value, map.get(key));

    // waite for ttl to expire
    sleepSeconds(ttlSeconds + 1);

    assertNull(map.get(key));
  }
예제 #3
0
  @Test
  public void testTnxMapReplaceKeyValue() throws Exception {
    final String mapName = randomString();
    final String key1 = "key1";
    final String oldValue1 = "old1";
    final String newValue1 = "new1";
    final String key2 = "key2";
    final String oldValue2 = "old2";

    IMap map = client.getMap(mapName);
    map.put(key1, oldValue1);
    map.put(key2, oldValue2);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);

    txMap.replace(key1, oldValue1, newValue1);
    txMap.replace(key2, "NOT_OLD_VALUE", "NEW_VALUE_CANT_BE_THIS");

    context.commitTransaction();

    assertEquals(newValue1, map.get(key1));
    assertEquals(oldValue2, map.get(key2));
  }
예제 #4
0
  @Test
  public void testTransactionalQueueGetsOfferedItems_whenBlockedOnPoll()
      throws InterruptedException {
    final String item = "offered1";
    final String queueName = randomString();
    final IQueue queue1 = client.getQueue(queueName);

    final CountDownLatch justBeforeBlocked = new CountDownLatch(1);

    new Thread() {
      public void run() {
        try {
          justBeforeBlocked.await();
          sleepSeconds(1);
          queue1.offer(item);
        } catch (InterruptedException e) {
          fail("failed" + e);
        }
      }
    }.start();

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue txnQueue1 = context.getQueue(queueName);

    justBeforeBlocked.countDown();
    Object result = txnQueue1.poll(5, TimeUnit.SECONDS);

    assertEquals(
        "TransactionalQueue while blocked in pol should get item offered from client queue",
        item,
        result);

    context.commitTransaction();
  }
예제 #5
0
  @Test
  public void testKeysetAndValuesWithPredicates() throws Exception {
    final String mapName = randomString();
    IMap map = client.getMap(mapName);

    final SampleObjects.Employee emp1 = new SampleObjects.Employee("abc-123-xvz", 34, true, 10D);
    final SampleObjects.Employee emp2 = new SampleObjects.Employee("abc-123-xvz", 20, true, 10D);

    map.put(emp1, emp1);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);

    assertNull(txMap.put(emp2, emp2));
    assertEquals(2, txMap.size());
    assertEquals(2, txMap.keySet().size());
    assertEquals(0, txMap.keySet(new SqlPredicate("age = 10")).size());
    assertEquals(0, txMap.values(new SqlPredicate("age = 10")).size());
    assertEquals(2, txMap.keySet(new SqlPredicate("age >= 10")).size());
    assertEquals(2, txMap.values(new SqlPredicate("age >= 10")).size());

    context.commitTransaction();

    assertEquals(2, map.size());
    assertEquals(2, map.values().size());
  }
예제 #6
0
  @Test(expected = NullPointerException.class)
  public void testKeyValuesPredicateNull() throws Exception {
    final String mapName = randomString();

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap<Object, Object> txMap = context.getMap(mapName);

    txMap.values(null);
  }
예제 #7
0
  @Test
  public void testTnxMapIsEmpty() throws Exception {
    final String mapName = randomString();
    IMap map = client.getMap(mapName);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);
    assertTrue(txMap.isEmpty());
    context.commitTransaction();
  }
예제 #8
0
 @Test
 public void testTransactionalOfferAndPollWithTimeout() throws InterruptedException {
   final String item = "offered";
   final String name = randomString();
   final TransactionContext context = client.newTransactionContext();
   context.beginTransaction();
   TransactionalQueue<String> txnQueue = context.getQueue(name);
   assertTrue(txnQueue.offer(item));
   assertEquals(1, txnQueue.size());
   assertEquals(item, txnQueue.poll(5, TimeUnit.SECONDS));
   context.commitTransaction();
 }
예제 #9
0
  @Test
  public void testUnlockAfterRollback() {
    final String mapName = randomString();
    final String key = "key";

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap<Object, Object> map = context.getMap(mapName);
    map.put(key, "value");
    context.rollbackTransaction();

    assertFalse(client.getMap(mapName).isLocked(key));
  }
예제 #10
0
  @Test
  public void testTransactionalOfferPoll() {
    final String item = "offered";
    final String queueName = randomString();
    final IQueue queue = client.getQueue(queueName);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue txnQueue = context.getQueue(queueName);
    txnQueue.offer(item);
    assertEquals(item, txnQueue.poll());
    context.commitTransaction();
  }
예제 #11
0
  @Test
  public void testTransactionalOfferRoleBack() {
    final String name = randomString();
    final IQueue queue = client.getQueue(name);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<String> qTxn = context.getQueue(name);
    qTxn.offer("ITEM");
    context.rollbackTransaction();

    assertEquals(0, queue.size());
  }
예제 #12
0
  @Test
  public void testDeadLockFromClientInstance() throws InterruptedException {
    final String mapName = randomString();
    final String key = "key";
    final AtomicBoolean running = new AtomicBoolean(true);
    Thread t =
        new Thread() {
          public void run() {
            while (running.get()) {
              client.getMap(mapName).get(key);
            }
          }
        };
    t.start();

    CBAuthorisation cb = new CBAuthorisation();
    cb.setAmount(15000);

    try {
      TransactionContext context = client.newTransactionContext();
      context.beginTransaction();

      TransactionalMap mapTransaction = context.getMap(mapName);
      // init data
      mapTransaction.put(key, cb);
      // start test deadlock, 3 set and concurrent, get deadlock

      cb.setAmount(12000);
      mapTransaction.set(key, cb);

      cb.setAmount(10000);
      mapTransaction.set(key, cb);

      cb.setAmount(900);
      mapTransaction.set(key, cb);

      cb.setAmount(800);
      mapTransaction.set(key, cb);

      cb.setAmount(700);
      mapTransaction.set(key, cb);

      context.commitTransaction();

    } catch (TransactionException e) {
      e.printStackTrace();
      fail();
    }
    running.set(false);
    t.join();
  }
예제 #13
0
  @Test
  public void testAddAndRoleBack() throws Exception {
    final String listName = randomString();
    final IList l = client.getList(listName);
    l.add("item1");

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalList<Object> list = context.getList(listName);
    list.add("item2");
    context.rollbackTransaction();

    assertEquals(1, l.size());
  }
예제 #14
0
  @Test
  public void testPutAndRoleBack() throws Exception {
    final String mapName = randomString();
    final String key = "key";
    final String value = "value";
    final IMap map = client.getMap(mapName);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap<Object, Object> mapTxn = context.getMap(mapName);
    mapTxn.put(key, value);
    context.rollbackTransaction();

    assertNull(map.get(key));
  }
예제 #15
0
  @Test
  public void testTxnMapPut_BeforeCommit() throws Exception {
    final String mapName = randomString();
    final String key = "key";
    final String value = "Value";
    final IMap map = client.getMap(mapName);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap<Object, Object> txnMap = context.getMap(mapName);

    assertNull(txnMap.put(key, value));

    context.commitTransaction();
  }
예제 #16
0
  @Test
  public void testTnxMapContainsKey() throws Exception {
    final String mapName = randomString();
    IMap map = client.getMap(mapName);
    map.put("key1", "value1");

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);
    txMap.put("key2", "value2");
    assertTrue(txMap.containsKey("key1"));
    assertTrue(txMap.containsKey("key2"));
    assertFalse(txMap.containsKey("key3"));

    context.commitTransaction();
  }
예제 #17
0
  @Test
  public void testPutAndRoleBack() throws Exception {
    final String mapName = randomString();
    final String key = "key";
    final String value = "value";
    final MultiMap multiMap = client.getMultiMap(mapName);

    TransactionContext tx = client.newTransactionContext();
    tx.beginTransaction();
    TransactionalMultiMap mulitMapTxn = tx.getMultiMap(mapName);
    mulitMapTxn.put(key, value);
    mulitMapTxn.put(key, value);
    tx.rollbackTransaction();

    assertEquals(Collections.EMPTY_LIST, multiMap.get(key));
  }
예제 #18
0
  @Test
  public void testTransactionalQueueSize() {
    final String item = "offered";
    final String name = randomString();
    final IQueue queue = client.getQueue(name);

    queue.offer(item);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<String> txnQueue = context.getQueue(name);

    txnQueue.offer(item);
    assertEquals(2, txnQueue.size());

    context.rollbackTransaction();
  }
예제 #19
0
  @Test
  public void testTxnMultiValueCount() throws Exception {
    final String mapName = randomString();
    final String key = "key";
    final String value = "value";
    final MultiMap multiMap = client.getMultiMap(mapName);

    multiMap.put(key, value);

    TransactionContext tx = client.newTransactionContext();
    tx.beginTransaction();
    TransactionalMultiMap mulitMapTxn = tx.getMultiMap(mapName);
    mulitMapTxn.put(key, "newValue");

    assertEquals(2, mulitMapTxn.valueCount(key));

    tx.commitTransaction();
  }
예제 #20
0
  @Test
  public void testTnxMapDelete() throws Exception {
    final String mapName = randomString();
    final String key = "key1";
    final String value = "old1";

    IMap map = client.getMap(mapName);
    map.put(key, value);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);

    txMap.delete(key);

    context.commitTransaction();

    assertNull(map.get(key));
  }
예제 #21
0
  @Test
  public void testRemoveAll() throws Exception {
    final String mapName = randomString();
    final String key = "key";

    MultiMap multiMap = client.getMultiMap(mapName);
    for (int i = 0; i < 10; i++) {
      multiMap.put(key, i);
    }

    TransactionContext tx = client.newTransactionContext();
    tx.beginTransaction();

    TransactionalMultiMap txnMultiMap = tx.getMultiMap(mapName);
    txnMultiMap.remove(key);
    tx.commitTransaction();

    assertEquals(Collections.EMPTY_LIST, multiMap.get(key));
  }
예제 #22
0
  @Test
  public void testAddRemove() throws Exception {
    String listName = randomString();
    final IList l = client.getList(listName);
    l.add("item1");

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalList<Object> list = context.getList(listName);
    assertTrue(list.add("item2"));
    assertEquals(2, list.size());
    assertEquals(1, l.size());
    assertFalse(list.remove("item3"));
    assertTrue(list.remove("item1"));

    context.commitTransaction();

    assertEquals(1, l.size());
  }
예제 #23
0
  @Test
  public void testRemove() throws Exception {
    final String mapName = randomString();
    final String key = "key";
    final String val = "value";

    MultiMap multiMap = client.getMultiMap(mapName);
    multiMap.put(key, val);

    TransactionContext tx = client.newTransactionContext();
    tx.beginTransaction();

    TransactionalMultiMap txnMultiMap = tx.getMultiMap(mapName);
    txnMultiMap.remove(key, val);

    tx.commitTransaction();

    assertEquals(Collections.EMPTY_LIST, client.getMultiMap(mapName).get(key));
  }
예제 #24
0
  @Test
  public void testTnxMapPutIfAbsent() throws Exception {
    final String mapName = randomString();
    IMap map = client.getMap(mapName);
    final String keyValue1 = "keyValue1";
    final String keyValue2 = "keyValue2";
    map.put(keyValue1, keyValue1);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);

    txMap.putIfAbsent(keyValue1, "NOT_THIS");
    txMap.putIfAbsent(keyValue2, keyValue2);

    context.commitTransaction();

    assertEquals(keyValue1, map.get(keyValue1));
    assertEquals(keyValue2, map.get(keyValue2));
  }
예제 #25
0
  @Test
  public void testKeySetValues() throws Exception {
    final String mapName = randomString();
    IMap map = client.getMap(mapName);
    map.put("key1", "value1");

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap<Object, Object> txMap = context.getMap(mapName);

    assertNull(txMap.put("key2", "value2"));
    assertEquals(2, txMap.size());
    assertEquals(2, txMap.keySet().size());
    assertEquals(2, txMap.values().size());

    context.commitTransaction();

    assertEquals(2, map.size());
    assertEquals(2, map.keySet().size());
    assertEquals(2, map.values().size());
  }
예제 #26
0
  @Test
  public void testTnxMapReplace() throws Exception {
    final String mapName = randomString();
    IMap map = client.getMap(mapName);
    final String key1 = "key1";
    final String key2 = "key2";
    final String replaceValue = "replaceValue";
    map.put(key1, "OLD_VALUE");

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);

    txMap.replace(key1, replaceValue);
    txMap.replace(key2, "NOT_POSSIBLE");

    context.commitTransaction();

    assertEquals(replaceValue, map.get(key1));
    assertNull(map.get(key2));
  }
예제 #27
0
  @Test
  public void testTnxMapRemoveKeyValue() throws Exception {
    final String mapName = randomString();
    final String key1 = "key1";
    final String oldValue1 = "old1";
    final String key2 = "key2";
    final String oldValue2 = "old2";

    IMap map = client.getMap(mapName);
    map.put(key1, oldValue1);
    map.put(key2, oldValue2);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);

    txMap.remove(key1, oldValue1);
    txMap.remove(key2, "NO_REMOVE_AS_NOT_VALUE");

    context.commitTransaction();

    assertNull(map.get(key1));
    assertEquals(oldValue2, map.get(key2));
  }