@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); }
@Test public void testQueueWithSizeLimit() { final IQueue q = client.getQueue(queueForTestQueueWithSizeLimit); for (int i = 0; i < maxSizeForQueue; i++) { q.offer(i); } assertFalse(q.offer(maxSizeForQueue)); }
@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()); }
@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()); }
@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()); }
@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()); } }
@Test public void testPutInterruption() throws InterruptedException { Config config = new Config(); config.setProperty(GroupProperties.PROP_OPERATION_CALL_TIMEOUT_MILLIS, "1000"); config.getQueueConfig("default").setMaxSize(1); HazelcastInstance instance = createHazelcastInstance(config); final IQueue<Object> queue = instance.getQueue(randomName()); final AtomicBoolean interrupted = new AtomicBoolean(); assertTrue(queue.offer("item")); Thread t = new Thread() { public void run() { try { queue.put("item"); } catch (InterruptedException e) { interrupted.set(true); } } }; t.start(); sleepSeconds(1); t.interrupt(); t.join(5000); assertTrue(interrupted.get()); }
@Test public void testRemove() throws IOException { final IQueue q = client.getQueue(randomString()); q.offer(1); assertTrue(q.remove(1)); assertFalse(q.remove(2)); }
@Test public void testContains() { final IQueue q = client.getQueue(randomString()); q.offer(1); assertTrue(q.contains(1)); assertFalse(q.contains(2)); }
@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()); }
@Test public void testRemainingCapacity() throws IOException { final IQueue q = client.getQueue(randomString()); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.offer("one"); assertEquals(Integer.MAX_VALUE - 1, q.remainingCapacity()); }
@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()); }
@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()); } }
@Test public void testIterator() 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 IteratorRequest(queueName)); PortableCollection result = (PortableCollection) client.receive(); Collection<Data> coll = result.getCollection(); int i = 1; for (Data data : coll) { assertEquals("item" + i, ss.toObject(data)); i++; } }
@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 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 testOffer() throws IOException, InterruptedException { final IQueue q = getQueue(); final SimpleClient client = getClient(); client.send(new OfferRequest(queueName, ss.toData("item1"))); Object result = client.receive(); assertTrue((Boolean) result); Object item = q.peek(); assertEquals(item, "item1"); q.offer("item2"); q.offer("item3"); q.offer("item4"); q.offer("item5"); q.offer("item6"); final CountDownLatch latch = new CountDownLatch(1); new Thread() { public void run() { try { latch.await(30, TimeUnit.SECONDS); } catch (InterruptedException e) { e.printStackTrace(); } q.poll(); } }.start(); client.send(new OfferRequest(queueName, 500, ss.toData("item7"))); result = client.receive(); assertFalse((Boolean) result); client.send(new OfferRequest(queueName, 10 * 1000, ss.toData("item7"))); Thread.sleep(1000); latch.countDown(); result = client.receive(); assertTrue((Boolean) result); }
@Test public void testToArray() { final int maxItems = 19; final IQueue q = client.getQueue(randomString()); Object[] offered = new Object[maxItems]; for (int i = 0; i < maxItems; i++) { q.offer(i); offered[i] = i; } Object[] result = q.toArray(); assertEquals(offered, result); }
@Test public void testIterator() { final int maxItems = 18; final IQueue q = client.getQueue(randomString()); for (int i = 0; i < maxItems; i++) { q.offer(i); } int i = 0; for (Object o : q) { assertEquals(i++, o); } }
@Test public void testContainsAll() { final int maxItems = 11; final IQueue q = client.getQueue(randomString()); List trueList = new ArrayList(); List falseList = new ArrayList(); for (int i = 0; i < maxItems; i++) { q.offer(i); trueList.add(i); falseList.add(i + 1); } assertTrue(q.containsAll(trueList)); assertFalse(q.containsAll(falseList)); }
@Test public void testContains() 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 ContainsRequest(queueName, list)); Boolean result = (Boolean) client.receive(); assertTrue(result); list.add(ss.toData("item0")); client.send(new ContainsRequest(queueName, list)); result = (Boolean) client.receive(); assertFalse(result); }
@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 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() { final int maxItems = 12; final IQueue q = client.getQueue(randomString()); List offeredList = new LinkedList(); for (int i = 0; i < maxItems; i++) { q.offer(i); offeredList.add(i); } List drainedList = new LinkedList(); int totalDrained = q.drainTo(drainedList); assertEquals(maxItems, totalDrained); assertEquals(offeredList, drainedList); }
/** @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 testTransactionalQueueSize() { final String item = "offered"; final String name = randomString(); final IQueue queue = client.getQueue(name); queue.offer(item); final TransactionContext context = client.newTransactionContext(); context.beginTransaction(); TransactionalQueue<String> txnQueue = context.getQueue(name); txnQueue.offer(item); assertEquals(2, txnQueue.size()); context.rollbackTransaction(); }
@Test public void testClearBackup() { HazelcastInstance[] instances = createHazelcastInstances(); HazelcastInstance instance1 = instances[0]; HazelcastInstance instance2 = instances[1]; String name = generateKeyOwnedBy(instance1); IQueue<Object> queue1 = instance1.getQueue(name); IQueue<Object> queue2 = instance2.getQueue(name); for (int i = 0; i < 4; i++) { queue1.offer("item" + i); } assertSizeEventually(4, queue2); assertIterableEquals(queue2, "item0", "item1", "item2", "item3"); queue1.clear(); instance1.shutdown(); assertSizeEventually(0, queue2); }
/* * (non-Javadoc) * * @see * com.hazelcast.core.MessageListener#onMessage(com.hazelcast.core.Message) */ @Override public void onMessage(Message<byte[]> hzMsg) { // We always queue the message even if we have a message listener. We'll // immediately pull it out of the queue and dispatch in a separate thread. // This is important to prevent slow message handlers from blocking topic // distribution in Hazelcast. if (!queue.offer(hzMsg.getMessageObject())) { log.warn( format( "In-memory message buffer full for topic [%s]. " + "Messages will be lost. Consider increaing the speed of " + "the consumer or the message buffer.", msgTopic.getName())); return; } if (messageListener != null) { hazelcastMQContext.onConsumerDispatchReady(id); } }
@Test public void testPartialDrain() { final int maxItems = 15; final int itemsToDrain = maxItems / 2; final IQueue q = client.getQueue(randomString()); List expectedList = new LinkedList(); for (int i = 0; i < maxItems; i++) { q.offer(i); if (i < itemsToDrain) { expectedList.add(i); } } List drainedList = new LinkedList(); int totalDrained = q.drainTo(drainedList, itemsToDrain); assertEquals(itemsToDrain, totalDrained); assertEquals(expectedList, drainedList); }
@Test public void testCompareAndRemoveBackup() { HazelcastInstance[] instances = createHazelcastInstances(); HazelcastInstance instance1 = instances[0]; HazelcastInstance instance2 = instances[1]; String name = generateKeyOwnedBy(instance1); IQueue<Object> queue1 = instance1.getQueue(name); IQueue<Object> queue2 = instance2.getQueue(name); for (int i = 0; i < 4; i++) { queue1.offer("item" + i); } assertSizeEventually(4, queue2); assertIterableEquals(queue2, "item0", "item1", "item2", "item3"); List<String> list = new ArrayList<String>(); list.add("item0"); list.add("item1"); list.add("item2"); assertTrue(queue1.removeAll(list)); instance1.shutdown(); assertSizeEventually(1, queue2); assertIterableEquals(queue2, "item3"); }