Exemplo n.º 1
0
  @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();
    }
  }
Exemplo n.º 2
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());
  }
Exemplo n.º 3
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));
  }
Exemplo n.º 4
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));
  }
Exemplo n.º 5
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();
  }
Exemplo n.º 6
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));
  }
Exemplo n.º 7
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();
  }
Exemplo n.º 8
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();
  }
Exemplo n.º 9
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());
  }
  /** {@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;
  }