forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BAEL-3463] - Big Queue (eugenp#8517)
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
data-structures/src/test/java/com/baeldung/bigqueue/BigQueueLiveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.baeldung.bigqueue; | ||
|
||
import com.leansoft.bigqueue.BigQueueImpl; | ||
import com.leansoft.bigqueue.IBigQueue; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@RunWith(JUnit4.class) | ||
public class BigQueueLiveTest { | ||
|
||
private IBigQueue bigQueue; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
String queueDir = System.getProperty("user.home"); | ||
String queueName = "baeldung-queue"; | ||
bigQueue = new BigQueueImpl(queueDir, queueName); | ||
} | ||
|
||
@After | ||
public void emptyQueue() throws IOException { | ||
bigQueue.removeAll(); | ||
bigQueue.gc(); | ||
bigQueue.close(); | ||
} | ||
|
||
@Test | ||
public void whenAddingRecords_ThenTheSizeIsCorrect() throws IOException { | ||
for (int i = 1; i <= 100; i++) { | ||
bigQueue.enqueue(String.valueOf(i).getBytes()); | ||
} | ||
|
||
assertEquals(100, bigQueue.size()); | ||
} | ||
|
||
@Test | ||
public void whenAddingRecords_ThenTheyCanBeRetrieved() throws IOException { | ||
bigQueue.enqueue(String.valueOf("new_record").getBytes()); | ||
|
||
String record = new String(bigQueue.dequeue()); | ||
assertEquals("new_record", record); | ||
} | ||
|
||
@Test | ||
public void whenDequeueingRecords_ThenTheyAreConsumed() throws IOException { | ||
for (int i = 1; i <= 100; i++) { | ||
bigQueue.enqueue(String.valueOf(i).getBytes()); | ||
} | ||
bigQueue.dequeue(); | ||
|
||
assertEquals(99, bigQueue.size()); | ||
} | ||
|
||
@Test | ||
public void whenPeekingRecords_ThenSizeDoesntChange() throws IOException { | ||
for (int i = 1; i <= 100; i++) { | ||
bigQueue.enqueue(String.valueOf(i).getBytes()); | ||
} | ||
String firstRecord = new String(bigQueue.peek()); | ||
|
||
assertEquals("1", firstRecord); | ||
assertEquals(100, bigQueue.size()); | ||
} | ||
|
||
@Test | ||
public void whenEmptyingTheQueue_ThenItSizeIs0() throws IOException { | ||
for (int i = 1; i <= 100; i++) { | ||
bigQueue.enqueue(String.valueOf(i).getBytes()); | ||
} | ||
bigQueue.removeAll(); | ||
|
||
assertEquals(0, bigQueue.size()); | ||
} | ||
|
||
} |