@Test public void testProduceConsumeMultiMessage() { PersistentQueue<Message> queue = new PersistentQueue<Message>("jk.store"); queue.init(); for (int i = 0; i < 10; i++) { assertTrue(queue.produce(new Message(i)), "always success"); } for (int i = 0; i < 10; i++) { Message expected = queue.consume(); Assert.assertEquals(i, expected.index(), "Comparing index value"); Assert.assertEquals( "My name is JK and I am a crap living in Singapore !@#$$%^^&&", expected.value(), "Comparing message value"); } Assert.assertNull(queue.consume(), "Already Popped, so it should not be available"); queue = new PersistentQueue<Message>("jk.store"); queue.init(); Assert.assertNull(queue.consume(), "Already Popped, so it should not be available"); //////////////// queue = new PersistentQueue<Message>("jk.store"); queue.init(); for (int i = 11; i <= 20; i++) { assertTrue(queue.produce(new Message(i)), "always success"); } for (int i = 11; i <= 20; i++) { Message expected = queue.consume(); Assert.assertEquals(i, expected.index(), "Comparing index value"); Assert.assertEquals( "My name is JK and I am a crap living in Singapore !@#$$%^^&&", expected.value(), "Comparing message value"); } }
@Test public void testProduceConsumeMultiMessageTwoFileOpens() { PersistentQueue<Message> queue = new PersistentQueue<Message>("jk.store"); queue.init(); for (int i = 0; i < 10; i++) { assertTrue(queue.produce(new Message(i)), "always success"); } queue.close(); queue = new PersistentQueue<Message>("jk.store"); queue.init(); for (int i = 0; i < 10; i++) { Message expected = queue.consume(); Assert.assertEquals(i, expected.index(), "Comparing index value"); Assert.assertEquals( "My name is JK and I am a crap living in Singapore !@#$$%^^&&", expected.value(), "Comparing message value"); } for (int i = 11; i <= 20; i++) { assertTrue(queue.produce(new Message(i)), "always success"); } for (int i = 11; i <= 20; i++) { Message expected = queue.consume(); Assert.assertEquals(i, expected.index(), "Comparing index value"); Assert.assertEquals( "My name is JK and I am a crap living in Singapore !@#$$%^^&&", expected.value(), "Comparing message value"); } }
@Test public void testLoad() { final PersistentQueue<Message> queue = new PersistentQueue<Message>("jk.store"); queue.init(); final Thread producer = new Thread() { @Override public void run() { for (int i = 0; i < 100000; i++) { queue.produce(new Message(i)); } } }; producer.setDaemon(false); producer.start(); final Thread consumer = new Thread() { @Override public void run() { for (int i = 0; i < 100000; i++) { final Object message = queue.consume(); if (message == null) { i--; continue; } Assert.assertEquals(((Message) message).index(), i, "Comparing the integer value"); } } }; consumer.setDaemon(false); consumer.start(); while (producer.isAlive() || consumer.isAlive()) { try { Thread.sleep(100); } catch (InterruptedException e) { // do nothing } } }
@Test public void testProduceConsumeSingleMessage() { PersistentQueue<Message> queue = new PersistentQueue<Message>("jk.store"); queue.init(); final Message actual = new Message(1); queue.produce(actual); Message expected = queue.consume(); Assert.assertEquals(actual.index(), expected.index(), "Comparing index value"); Assert.assertEquals(actual.value(), expected.value(), "Comparing message value"); Assert.assertNull(queue.consume(), "Already Popped, so it should not be available"); queue = new PersistentQueue<Message>("jk.store"); queue.init(); Assert.assertNull(queue.consume(), "Already Popped, so it should not be available"); }
public static void testPersistentQueue() { PersistentQueue<Integer> q0 = new PersistentQueue<Integer>(); assertThat(q0.size(), 0); int i = 0; try { q0.peek(); i = 1; } catch (NoSuchElementException e) { i = 2; } assertThat(i, 2); PersistentQueue<Integer> q1 = q0.enqueue(2); assertThat(q0.size(), 0); assertThat(q1.size(), 1); assertThat(q1.peek(), 2); PersistentQueue<Integer> q2 = q1.enqueue(1); assertThat(q2.size(), 2); assertThat(q2.peek(), 2); PersistentQueue<Integer> q3 = q2.dequeue(); assertThat(q3.size(), 1); assertThat(q3.peek(), 1); assertThat(q2.size(), 2); assertThat(q2.peek(), 2); assertThat(q1.size(), 1); assertThat(q1.peek(), 2); try { PersistentQueue<Integer> q5 = q3.enqueue(null); i = 3; } catch (IllegalArgumentException e) { i = 4; } assertThat(i, 4); }