/** Test for issue #39 */
  @Test
  public void testIsMapKeyLocked() throws InterruptedException {
    HazelcastClient hClient = getHazelcastClient();
    final IMap map = hClient.getMap("testIsMapKeyLocked");
    assertFalse(map.isLocked("key"));
    map.lock("key");
    assertTrue(map.isLocked("key"));

    final CountDownLatch latch = new CountDownLatch(1);
    Thread thread =
        new Thread(
            new Runnable() {
              public void run() {
                assertTrue(map.isLocked("key"));
                try {
                  while (map.isLocked("key")) {
                    Thread.sleep(100);
                  }
                } catch (InterruptedException e) {
                  throw new RuntimeException(e);
                }
                latch.countDown();
              }
            });
    thread.start();
    Thread.sleep(100);
    map.unlock("key");
    assertTrue(latch.await(3, TimeUnit.SECONDS));
  }
 @Test
 public void lockMapKey() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, String> map = hClient.getMap("lockMapKey");
   final CountDownLatch latch = new CountDownLatch(1);
   map.put("a", "b");
   Thread.sleep(10);
   map.lock("a");
   new Thread(
           new Runnable() {
             public void run() {
               map.lock("a");
               latch.countDown();
             }
           })
       .start();
   Thread.sleep(10);
   assertEquals(1, latch.getCount());
   map.unlock("a");
   assertTrue(latch.await(10, TimeUnit.SECONDS));
 }
 @Test
 public void tryLock() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, String> map = hClient.getMap("tryLock");
   final CountDownLatch latch = new CountDownLatch(3);
   map.put("1", "A");
   map.lock("1");
   new Thread(
           new Runnable() {
             public void run() {
               if (!map.tryLock("1", 100, TimeUnit.MILLISECONDS)) {
                 latch.countDown();
               }
               if (!map.tryLock("1")) {
                 latch.countDown();
               }
               if (map.tryLock("2")) {
                 latch.countDown();
               }
             }
           })
       .start();
   assertTrue(latch.await(10, TimeUnit.SECONDS));
 }
Ejemplo n.º 4
0
 @Test
 public void testMigrationOfTTLAndLock() throws Exception {
   Config config = new Config();
   final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
   final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
   ConcurrentMapManager cmm1 = getConcurrentMapManager(h1);
   ConcurrentMapManager cmm2 = getConcurrentMapManager(h2);
   final IMap imap1 = h1.getMap("default");
   final IMap imap2 = h2.getMap("default");
   final Data dKey = toData("1");
   assertTrue(migrateKey("1", h1, h1, 0));
   assertTrue(migrateKey("1", h1, h2, 1));
   imap1.put("1", "value1", 60, TimeUnit.SECONDS);
   imap1.lock("1");
   Future put2 = imap2.putAsync("1", "value2");
   imap2.addEntryListener(
       new EntryAdapter() {
         @Override
         public void entryUpdated(EntryEvent entryEvent) {
           System.out.println(entryEvent);
         }
       },
       "1",
       true);
   if (put2 == null) fail();
   Thread.sleep(1000);
   CMap cmap1 = getCMap(h1, "default");
   CMap cmap2 = getCMap(h2, "default");
   Record record1 = cmap1.getRecord(dKey);
   assertEquals(1, record1.getScheduledActionCount());
   for (ScheduledAction scheduledAction : record1.getScheduledActions()) {
     assertTrue(scheduledAction.isValid());
   }
   assertNotNull(record1.getListeners());
   assertEquals(1, record1.getListeners().size());
   DistributedLock lock = cmap1.getRecord(dKey).getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size());
   assertTrue(migrateKey("1", h1, h2, 0));
   assertTrue(migrateKey("1", h1, h1, 1));
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size());
   assertTrue(cmap1.getRecord(dKey).getRemainingTTL() < 60000);
   assertTrue(cmap2.getRecord(dKey).getRemainingTTL() < 60000);
   Record record2 = cmap2.getRecord(dKey);
   lock = record2.getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   lock = cmap1.getRecord(dKey).getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   imap1.unlock("1");
   put2.get(10, TimeUnit.SECONDS);
   assertEquals("value2", imap1.get("1"));
   assertEquals("value2", imap2.get("1"));
   assertNotNull(record2.getListeners());
   assertEquals(1, record2.getListeners().size());
 }