|
| 1 | +package com.baeldung.bigqueue; |
| 2 | + |
| 3 | +import com.leansoft.bigqueue.BigQueueImpl; |
| 4 | +import com.leansoft.bigqueue.IBigQueue; |
| 5 | +import org.junit.After; |
| 6 | +import org.junit.Before; |
| 7 | +import org.junit.Test; |
| 8 | +import org.junit.runner.RunWith; |
| 9 | +import org.junit.runners.JUnit4; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | + |
| 15 | +@RunWith(JUnit4.class) |
| 16 | +public class BigQueueLiveTest { |
| 17 | + |
| 18 | + private IBigQueue bigQueue; |
| 19 | + |
| 20 | + @Before |
| 21 | + public void setup() throws IOException { |
| 22 | + String queueDir = System.getProperty("user.home"); |
| 23 | + String queueName = "baeldung-queue"; |
| 24 | + bigQueue = new BigQueueImpl(queueDir, queueName); |
| 25 | + } |
| 26 | + |
| 27 | + @After |
| 28 | + public void emptyQueue() throws IOException { |
| 29 | + bigQueue.removeAll(); |
| 30 | + bigQueue.gc(); |
| 31 | + bigQueue.close(); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void whenAddingRecords_ThenTheSizeIsCorrect() throws IOException { |
| 36 | + for (int i = 1; i <= 100; i++) { |
| 37 | + bigQueue.enqueue(String.valueOf(i).getBytes()); |
| 38 | + } |
| 39 | + |
| 40 | + assertEquals(100, bigQueue.size()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void whenAddingRecords_ThenTheyCanBeRetrieved() throws IOException { |
| 45 | + bigQueue.enqueue(String.valueOf("new_record").getBytes()); |
| 46 | + |
| 47 | + String record = new String(bigQueue.dequeue()); |
| 48 | + assertEquals("new_record", record); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void whenDequeueingRecords_ThenTheyAreConsumed() throws IOException { |
| 53 | + for (int i = 1; i <= 100; i++) { |
| 54 | + bigQueue.enqueue(String.valueOf(i).getBytes()); |
| 55 | + } |
| 56 | + bigQueue.dequeue(); |
| 57 | + |
| 58 | + assertEquals(99, bigQueue.size()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenPeekingRecords_ThenSizeDoesntChange() throws IOException { |
| 63 | + for (int i = 1; i <= 100; i++) { |
| 64 | + bigQueue.enqueue(String.valueOf(i).getBytes()); |
| 65 | + } |
| 66 | + String firstRecord = new String(bigQueue.peek()); |
| 67 | + |
| 68 | + assertEquals("1", firstRecord); |
| 69 | + assertEquals(100, bigQueue.size()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void whenEmptyingTheQueue_ThenItSizeIs0() throws IOException { |
| 74 | + for (int i = 1; i <= 100; i++) { |
| 75 | + bigQueue.enqueue(String.valueOf(i).getBytes()); |
| 76 | + } |
| 77 | + bigQueue.removeAll(); |
| 78 | + |
| 79 | + assertEquals(0, bigQueue.size()); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments