@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);
   }
 }
Exemplo n.º 2
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();
  }