Exemplo n.º 1
0
  @Test
  public void testOfferPoll() throws IOException, InterruptedException {

    final IQueue q = client.getQueue(queueForTestOfferPoll);

    for (int i = 0; i < 10; i++) {
      boolean result = q.offer("item");
      if (i < maxSizeForQueue) {
        assertTrue(result);
      } else {
        assertFalse(result);
      }
    }
    assertEquals(maxSizeForQueue, q.size());

    final Thread t1 =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            q.poll();
          }
        };
    t1.start();

    boolean result = q.offer("item", 5, TimeUnit.SECONDS);
    assertTrue(result);

    for (int i = 0; i < 10; i++) {
      Object o = q.poll();
      if (i < maxSizeForQueue) {
        assertNotNull(o);
      } else {
        assertNull(o);
      }
    }
    assertEquals(0, q.size());

    final Thread t2 =
        new Thread() {
          public void run() {
            try {
              Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            q.offer("item1");
          }
        };
    t2.start();

    Object o = q.poll(5, TimeUnit.SECONDS);
    assertEquals("item1", o);
    t1.join(10000);
    t2.join(10000);
  }
Exemplo n.º 2
0
 @Test
 public void testPollNull() throws Exception {
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance[] instances = factory.newInstances();
   final HazelcastInstance h1 = instances[0];
   final HazelcastInstance h2 = instances[1];
   final IQueue q1 = h1.getQueue("default");
   final IQueue q2 = h2.getQueue("default");
   for (int i = 0; i < 100; i++) {
     assertNull(q1.poll());
     assertNull(q2.poll());
   }
   assertNull(q1.poll(2, TimeUnit.SECONDS));
   assertNull(q2.poll(2, TimeUnit.SECONDS));
 }
 @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);
   }
 }
    /*
     * (non-Javadoc)
     *
     * @see
     * org.mpilone.hazelcastmq.core.DefaultHazelcastMQConsumer.ReceiveStrategy
     * #receive(com.hazelcast.core.IQueue)
     */
    @Override
    public byte[] receive(IQueue<byte[]> queue) {
      if (closed || !active) {
        return null;
      }

      return queue.poll();
    }
Exemplo n.º 5
0
 @Test
 public void testOffer() throws Exception {
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance[] instances = factory.newInstances();
   HazelcastInstance h1 = instances[0];
   HazelcastInstance h2 = instances[1];
   final IQueue q1 = h1.getQueue("default");
   final IQueue q2 = h2.getQueue("default");
   for (int i = 0; i < 100; i++) {
     assertTrue(q1.offer("item" + i, 100, TimeUnit.SECONDS));
     assertTrue(q2.offer("item" + i, 100, TimeUnit.SECONDS));
   }
   assertEquals("item0", q1.peek());
   assertEquals("item0", q2.peek());
   for (int i = 0; i < 100; i++) {
     assertEquals("item" + i, q1.poll());
     assertEquals("item" + i, q2.poll());
   }
 }
    /*
     * (non-Javadoc)
     *
     * @see
     * org.mpilone.hazelcastmq.core.DefaultHazelcastMQConsumer.ReceiveStrategy
     * #receive(com.hazelcast.core.IQueue)
     */
    @Override
    public byte[] receive(IQueue<byte[]> queue) {
      if (closed) {
        return null;
      }

      try {
        if (!active) {
          receiveCondition.await(500L, TimeUnit.MILLISECONDS);
        } else {
          return queue.poll(500L, TimeUnit.MILLISECONDS);
        }
      } catch (InterruptedException ex) {
        // Ignore for now
      }

      return null;
    }
    /*
     * (non-Javadoc)
     *
     * @see
     * org.mpilone.hazelcastmq.core.DefaultHazelcastMQConsumer.ReceiveStrategy
     * #receive(com.hazelcast.core.IQueue)
     */
    @Override
    public byte[] receive(IQueue<byte[]> queue) {
      if (closed) {
        return null;
      }

      long t = Math.min(timeout, 500L);
      timeout -= 500L;

      try {
        if (!active) {
          receiveCondition.await(t, TimeUnit.MILLISECONDS);
        } else {
          return queue.poll(t, TimeUnit.MILLISECONDS);
        }
      } catch (InterruptedException ex) {
        // Ignore for now
      }

      return null;
    }
Exemplo n.º 8
0
  @Test
  public void testShutdown() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances();
    final HazelcastInstance h1 = instances[0];
    final HazelcastInstance h2 = instances[1];
    warmUpPartitions(h2, h1);

    final IQueue q1 = h1.getQueue("default");
    final IQueue q2 = h2.getQueue("default");
    for (int i = 0; i < 40; i++) {
      assertTrue(q1.offer("item" + i, 100, TimeUnit.SECONDS));
    }
    h1.getLifecycleService().shutdown();
    for (int i = 40; i < 100; i++) {
      assertTrue(q2.offer("item" + i, 100, TimeUnit.SECONDS));
    }
    for (int i = 0; i < 100; i++) {
      assertEquals("item" + i, q2.poll());
    }
  }
 @Test
 public void testTransactionAtomicity_whenMultiMapValueCountIsUsed_withoutTransaction()
     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) {
         MultiMap<Object, Object> multiMap = hz.getMultiMap(name);
         assertEquals(1, multiMap.valueCount(id));
         multiMap.remove(id);
       } else {
         LockSupport.parkNanos(100);
       }
     }
   } finally {
     stopProducerThread(producerThread);
   }
 }
Exemplo n.º 10
0
 @Test
 public void testPoll() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertEquals(1, q.poll());
 }
Exemplo n.º 11
0
  /** Test for issue 730. (google) */
  @Test
  public void testDeadTaker() throws Exception {
    Config config = new Config();
    final CountDownLatch shutdownLatch = new CountDownLatch(1);
    config.addListenerConfig(
        new ListenerConfig()
            .setImplementation(
                new MembershipListener() {
                  public void memberAdded(MembershipEvent membershipEvent) {}

                  public void memberRemoved(MembershipEvent membershipEvent) {
                    shutdownLatch.countDown();
                  }

                  public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {}
                }));

    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances(config);
    final HazelcastInstance h1 = instances[0];
    final HazelcastInstance h2 = instances[1];
    warmUpPartitions(h1, h2);

    final IQueue q1 = h1.getQueue("default");
    final IQueue q2 = h2.getQueue("default");

    final CountDownLatch startLatch = new CountDownLatch(1);
    new Thread(
            new Runnable() {
              public void run() {
                try {
                  assertTrue(
                      startLatch.await(10, TimeUnit.SECONDS)); // fail shutdown if await fails.
                  Thread.sleep(5000);
                  h2.getLifecycleService().terminate();
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
              }
            })
        .start();

    new Thread(
            new Runnable() {
              public void run() {
                try {
                  startLatch.countDown();
                  final Object o = q2.take();
                  fail("Should not be able to take: " + o);
                } catch (HazelcastInstanceNotActiveException ignored) {
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
              }
            })
        .start();

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

    q1.offer("item");
    assertEquals(1, q1.size()); // 0
    assertEquals("item", q1.poll());
  }