示例#1
0
  @Test
  @Category(ProblematicTest.class) // TODO
  public void testLockInterruption() throws InterruptedException {
    Config config = new Config();
    config.setProperty(GroupProperties.PROP_OPERATION_CALL_TIMEOUT_MILLIS, "5000");
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1);
    final HazelcastInstance hz = nodeFactory.newHazelcastInstance(config);

    final Lock lock = hz.getLock("testLockInterruption2");
    final CountDownLatch latch = new CountDownLatch(1);
    Thread t =
        new Thread(
            new Runnable() {
              public void run() {
                try {
                  lock.tryLock(60, TimeUnit.SECONDS);
                } catch (InterruptedException ignored) {
                  latch.countDown();
                }
              }
            });
    lock.lock();
    t.start();
    Thread.sleep(2000);
    t.interrupt();
    assertTrue("tryLock() is not interrupted!", latch.await(30, TimeUnit.SECONDS));
    lock.unlock();
    assertTrue("Could not acquire lock!", lock.tryLock());
  }
示例#2
0
 private static <R> void verifyInvocationCannotBeInterrupted(final WSTester<R> inTester)
     throws Exception {
   resetServiceParameters();
   getMockSAService().setSleep(true);
   inTester.setReturnValue(false);
   final Semaphore sema = new Semaphore(0);
   final AtomicReference<Exception> interruptFailure = new AtomicReference<Exception>();
   Thread t =
       new Thread() {
         @Override
         public void run() {
           sema.release();
           try {
             inTester.invokeApi(false);
           } catch (Exception ex) {
             interruptFailure.set(ex);
           }
         }
       };
   t.start();
   // Wait for the thread to be started
   sema.acquire();
   // Interrupt it as soon as it is found started
   t.interrupt();
   // wait for it to end
   t.join();
   // verify that we are not able to interrupt it
   assertNull("API invocation got interrupted!", interruptFailure.get());
 }
示例#3
0
  @Test
  public void testMapRecordIdleEvictionOnMigration() throws InterruptedException {
    Config cfg = new Config();
    final String name = "testMapRecordIdleEvictionOnMigration";
    MapConfig mc = cfg.getMapConfig(name);
    int maxIdleSeconds = 10;
    int size = 100;
    final int nsize = size / 5;
    mc.setMaxIdleSeconds(maxIdleSeconds);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);

    HazelcastInstance instance1 = factory.newHazelcastInstance(cfg);
    final IMap map = instance1.getMap(name);
    final CountDownLatch latch = new CountDownLatch(size - nsize);
    map.addEntryListener(
        new EntryAdapter() {
          public void entryEvicted(EntryEvent event) {
            latch.countDown();
          }
        },
        false);

    for (int i = 0; i < size; i++) {
      map.put(i, i);
    }
    final Thread thread =
        new Thread(
            new Runnable() {
              public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                  try {
                    for (int i = 0; i < nsize; i++) {
                      map.get(i);
                    }
                    Thread.sleep(1000);
                  } catch (HazelcastInstanceNotActiveException e) {
                    return;
                  } catch (InterruptedException e) {
                    return;
                  }
                }
              }
            });
    thread.start();
    HazelcastInstance instance2 = factory.newHazelcastInstance(cfg);
    HazelcastInstance instance3 = factory.newHazelcastInstance(cfg);

    assertTrue(latch.await(1, TimeUnit.MINUTES));
    Assert.assertEquals(nsize, map.size());

    thread.interrupt();
    thread.join(5000);
  }
 /** @see java.lang.Runnable#run() */
 public void run() {
   final Thread thread = Thread.currentThread();
   while (!Thread.interrupted()) {
     m_set.add(m_touchable);
     m_list.add(m_touchable);
     try {
       synchronized (thread) {
         thread.wait();
       }
     } catch (InterruptedException e) {
       // propagate interrupt
       thread.interrupt();
     }
   }
 }