Ejemplo n.º 1
0
  @Test
  public void testInterruptionDuringBlockingOp2() throws InterruptedException {
    HazelcastInstance hz = createHazelcastInstance();
    final ILock lock = hz.getLock("lock");
    lock.lock();
    assertTrue(lock.isLockedByCurrentThread());

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean interruptedFlag = new AtomicBoolean(false);

    final OpThread thread =
        new OpThread("Lock-Thread", latch, interruptedFlag) {
          protected void doOp() throws InterruptedException {
            assertTrue(lock.tryLock(1, TimeUnit.MINUTES));
          }
        };
    thread.start();

    Thread.sleep(5000);
    thread.interrupt();
    lock.unlock();

    assertTrue(latch.await(1, TimeUnit.MINUTES));

    if (thread.isInterruptionCaught()) {
      assertFalse("Thread interrupted flag should not be set!", interruptedFlag.get());
      assertFalse("Lock should not be in 'locked' state!", lock.isLocked());
    } else {
      assertTrue("Thread interrupted flag should be set! " + thread, interruptedFlag.get());
      assertTrue("Lock should be 'locked' state!", lock.isLocked());
    }
  }
Ejemplo n.º 2
0
  @Test
  public void testInterruptionDuringBlockingOp1() throws InterruptedException {
    HazelcastInstance hz = createHazelcastInstance();
    final IQueue<Object> q = hz.getQueue("queue");

    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean interruptedFlag = new AtomicBoolean(false);

    OpThread thread =
        new OpThread("Queue Thread", latch, interruptedFlag) {
          protected void doOp() throws InterruptedException {
            q.poll(1, TimeUnit.MINUTES);
          }
        };
    thread.start();

    Thread.sleep(5000);
    thread.interrupt();
    q.offer("new item!");

    assertTrue(latch.await(1, TimeUnit.MINUTES));

    if (thread.isInterruptionCaught()) {
      assertFalse("Thread interrupted flag should not be set!", interruptedFlag.get());
      assertFalse("Queue should not be empty!", q.isEmpty());
    } else {
      assertTrue("Thread interrupted flag should be set!", interruptedFlag.get());
      assertTrue("Queue should be empty!", q.isEmpty());
    }
  }