@Test
  @InSequence(2)
  @Ignore
  public void testTransactionCommit() throws Throwable {
    userTx.begin();
    HazelcastConnection c = getConnection();
    try {
      TransactionalMap<String, String> m = c.getTransactionalMap("testTransactionCommit");

      m.put("key", "value");

      doSql();

      assertEquals("value", m.get("key"));
    } finally {
      c.close();
    }
    userTx.commit();
    HazelcastConnection con2 = getConnection();

    try {
      assertEquals("value", con2.getMap("testTransactionCommit").get("key"));
      validateSQLdata(true);
    } finally {
      con2.close();
    }
  }
  @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));
  }
  @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));
  }
  @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);
  }
  @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();
  }
  @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));
  }
  @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());
  }
Exemple #8
0
 void transfer(int thief, UTS loot) {
   final UTS bag = this.bag.trim();
   final int me = this.me;
   final int wave = ResilientUTS.this.wave;
   hz.executeTransaction(
       (TransactionalTaskContext context) -> {
         final TransactionalMap<Integer, UTS> map = context.getMap("map" + wave);
         map.set(me, bag);
         final UTS old = map.getForUpdate(thief);
         loot.count = old == null ? 0 : old.count;
         map.set(thief, loot);
         return null;
       });
 }
  @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();
  }
Exemple #10
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));
  }
Exemple #11
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();
  }
Exemple #12
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));
  }
Exemple #13
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));
  }
Exemple #14
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());
  }
Exemple #15
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));
  }
Exemple #16
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));
  }
  /** {@inheritDoc} */
  @Override
  public boolean test(Map<Object, Object> ctx) throws Exception {
    int key = nextRandom(args.range());

    TransactionOptions txOpts = new TransactionOptions().setTransactionType(LOCAL);

    TransactionContext tCtx = hazelcast().newTransactionContext(txOpts);

    tCtx.beginTransaction();

    TransactionalMap<Object, Object> txMap = tCtx.getMap("map");

    try {
      txMap.put(key, new SampleValue(key));

      tCtx.commitTransaction();
    } catch (Exception e) {
      e.printStackTrace(cfg.error());

      tCtx.rollbackTransaction();
    }

    return true;
  }
Exemple #18
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();
  }