@Test
  public void testPoll() throws IOException {
    final IQueue q = getQueue();
    final SimpleClient client = getClient();
    client.send(new PollRequest(queueName));
    Object result = client.receive();
    assertNull(result);

    q.offer("item1");
    client.send(new PollRequest(queueName));
    result = client.receive();
    assertEquals("item1", result);
    assertEquals(0, q.size());

    new Thread() {
      public void run() {
        try {
          Thread.sleep(3000);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        q.offer("item2");
      }
    }.start();
    client.send(new PollRequest(queueName, 10 * 1000));
    result = client.receive();
    assertEquals("item2", result);
    assertEquals(0, q.size());
  }
示例#2
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);
  }
示例#3
0
  @Test
  public void testClear() {
    final int maxItems = 123;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }

    assertEquals(maxItems, q.size());
    q.clear();
    assertEquals(0, q.size());
  }
示例#4
0
 @Test
 public void testPeak() throws InterruptedException {
   final IQueue q = client.getQueue(randomString());
   q.offer(1);
   assertEquals(1, q.peek());
   assertEquals(1, q.peek());
   assertEquals(1, q.size());
 }
 /** @throws Exception */
 @Test
 public void testQueueAfterShutdown2() throws Exception {
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
   final HazelcastInstance[] instances = factory.newInstances();
   final HazelcastInstance h1 = instances[0];
   final HazelcastInstance h2 = instances[1];
   IQueue q1 = h1.getQueue("default");
   IQueue q2 = h2.getQueue("default");
   q1.offer("item");
   assertEquals(1, q1.size());
   assertEquals(1, q2.size());
   assertEquals("item", q2.take());
   assertEquals(0, q1.size());
   assertEquals(0, q2.size());
   h2.getLifecycleService().shutdown();
   assertEquals(0, q1.size());
 }
  @Test
  public void testRemove() throws IOException {

    final IQueue q = getQueue();
    q.offer("item1");
    q.offer("item2");
    q.offer("item3");

    final SimpleClient client = getClient();
    client.send(new RemoveRequest(queueName, ss.toData("item2")));
    Boolean result = (Boolean) client.receive();
    assertTrue(result);
    assertEquals(2, q.size());

    client.send(new RemoveRequest(queueName, ss.toData("item2")));
    result = (Boolean) client.receive();
    assertFalse(result);
    assertEquals(2, q.size());
  }
  @Test
  public void testSize() throws IOException {
    final IQueue q = getQueue();
    q.offer("item1");
    q.offer("item2");
    q.offer("item3");

    final SimpleClient client = getClient();
    client.send(new SizeRequest(queueName));
    int result = (Integer) client.receive();
    assertEquals(result, q.size());
  }
示例#8
0
  @Test
  public void testRetainEmptyList() throws IOException {
    final int maxItems = 131;
    final IQueue q = client.getQueue(randomString());

    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }

    List retain = new LinkedList();
    assertTrue(q.retainAll(retain));
    assertEquals(0, q.size());
  }
  @Test
  public void testTransactionalOfferRoleBack() {
    final String name = randomString();
    final IQueue queue = client.getQueue(name);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue<String> qTxn = context.getQueue(name);
    qTxn.offer("ITEM");
    context.rollbackTransaction();

    assertEquals(0, queue.size());
  }
  @Test
  public void testClear() throws Exception {
    IQueue q = getQueue();
    q.offer("item1");
    q.offer("item2");
    q.offer("item3");

    final SimpleClient client = getClient();
    client.send(new ClearRequest(queueName));
    Object result = client.receive();
    assertTrue((Boolean) result);
    assertEquals(0, q.size());
  }
示例#11
0
  @Test
  public void testAddAll() throws IOException {
    final int maxItems = 13;
    final IQueue q = client.getQueue(randomString());

    Collection coll = new ArrayList(maxItems);

    for (int i = 0; i < maxItems; i++) {
      coll.add(i);
    }

    assertTrue(q.addAll(coll));
    assertEquals(coll.size(), q.size());
  }
示例#12
0
  @Test
  public void testRemoveList_whereNotFound() throws IOException {
    final int maxItems = 131;
    final IQueue q = client.getQueue(randomString());

    List removeList = new LinkedList();
    for (int i = 0; i < maxItems; i++) {
      q.add(i);
    }
    removeList.add(maxItems + 1);
    removeList.add(maxItems + 2);

    assertFalse(q.removeAll(removeList));
    assertEquals(maxItems, q.size());
  }
  @Test
  public void testPeek() throws IOException {
    IQueue q = getQueue();

    final SimpleClient client = getClient();
    client.send(new PeekRequest(queueName));
    Object result = client.receive();
    assertNull(result);

    q.offer("item1");
    client.send(new PeekRequest(queueName));
    result = client.receive();
    assertEquals("item1", result);
    assertEquals(1, q.size());
  }
  @Test
  public void test() {
    List<Object> expectedItems = new LinkedList<Object>();
    for (int k = 0; k < 100; k++) {
      queue.add(k);
      expectedItems.add(k);
    }

    remote1.shutdown();
    remote2.shutdown();

    assertEquals(expectedItems.size(), queue.size());
    List actualItems = Arrays.asList(queue.toArray());
    assertEquals(expectedItems, actualItems);
  }
  @Test
  public void testQueueSizeAfterTxnOfferPoll() {
    final String item = "offered";
    final String queueName = randomString();
    final IQueue queue = client.getQueue(queueName);

    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    TransactionalQueue txnQueue = context.getQueue(queueName);
    txnQueue.offer(item);
    txnQueue.poll();
    context.commitTransaction();

    assertEquals(0, queue.size());
  }
  @Test
  public void testCompareAndRemove() throws IOException {
    IQueue q = getQueue();
    q.offer("item1");
    q.offer("item2");
    q.offer("item3");
    q.offer("item4");
    q.offer("item5");

    List<Data> list = new ArrayList<Data>();
    list.add(ss.toData("item1"));
    list.add(ss.toData("item2"));

    final SimpleClient client = getClient();
    client.send(new CompareAndRemoveRequest(queueName, list, true));
    Boolean result = (Boolean) client.receive();
    assertTrue(result);
    assertEquals(2, q.size());

    client.send(new CompareAndRemoveRequest(queueName, list, false));
    result = (Boolean) client.receive();
    assertTrue(result);
    assertEquals(0, q.size());
  }
  @Test
  public void testDrain() throws IOException {
    IQueue q = getQueue();
    q.offer("item1");
    q.offer("item2");
    q.offer("item3");
    q.offer("item4");
    q.offer("item5");

    final SimpleClient client = getClient();
    client.send(new DrainRequest(queueName, 1));
    PortableCollection result = (PortableCollection) client.receive();
    Collection<Data> coll = result.getCollection();
    assertEquals(1, coll.size());
    assertEquals("item1", ss.toObject(coll.iterator().next()));
    assertEquals(4, q.size());
  }
示例#18
0
 @Test
 public void shouldCreateQueue() throws InterruptedException {
   // Given
   int numberOfEvents = 5;
   Config config = new Config();
   QueueStoreConfig jdbcBackedQueueConfig =
       QueueStoreConfigFactory.getJdbcBackedQueueConfig(dataSource(), queueName);
   QueueConfig messageQueue = config.getQueueConfig(queueName);
   messageQueue.setQueueStoreConfig(jdbcBackedQueueConfig);
   HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
   // When
   QueueStore storeImplementation = jdbcBackedQueueConfig.getStoreImplementation();
   for (long i = 0; i < numberOfEvents; i++) {
     storeImplementation.store(i, String.valueOf(i));
   }
   IQueue<String> iQueue = hazelcastInstance.getQueue(queueName);
   MatcherAssert.assertThat(iQueue.size(), CoreMatchers.equalTo(numberOfEvents));
   String actual = iQueue.take();
   MatcherAssert.assertThat(actual, CoreMatchers.equalTo("0"));
   hazelcastInstance.shutdown();
 }
 @Verify
 public void verify() {
   long expected = workQueue.size() + consumed.get();
   long actual = produced.get();
   assertEquals(expected, actual);
 }
  /** 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());
  }
示例#21
0
 @Test
 public void testadd() {
   final IQueue q = client.getQueue(randomString());
   assertTrue(q.add(1));
   assertEquals(1, q.size());
 }