Revert "BAEL-4134"

This commit is contained in:
Loredana Crusoveanu
2020-07-07 14:18:10 +03:00
committed by GitHub
parent dffa1f64e6
commit 485b4e3e99
2477 changed files with 9477 additions and 547819 deletions

View File

@@ -0,0 +1,72 @@
package com.baeldung.circularbuffer;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CircularBufferUnitTest {
private final String[] shapes = { "Circle", "Triangle", "Rectangle", "Square", "Rhombus", "Trapezoid", "Pentagon", "Pentagram", "Hexagon", "Hexagram" };
private final int defaultCapacity = shapes.length;
@Test
public void givenCircularBuffer_whenAnElementIsEnqueued_thenSizeIsOne() {
CircularBuffer<String> buffer = new CircularBuffer<>(defaultCapacity);
assertTrue(buffer.offer("Square"));
assertEquals(1, buffer.size());
}
@Test
public void givenCircularBuffer_whenAnElementIsDequeued_thenElementMatchesEnqueuedElement() {
CircularBuffer<String> buffer = new CircularBuffer<>(defaultCapacity);
buffer.offer("Triangle");
String shape = buffer.poll();
assertEquals("Triangle", shape);
}
@Test
public void givenCircularBuffer_whenAnElementIsEnqueuedAndDeququed_thenBufferIsEmpty() {
CircularBuffer<String> buffer = new CircularBuffer<>(defaultCapacity);
buffer.offer("Rectangle");
assertFalse(buffer.isEmpty());
assertEquals(1, buffer.size());
buffer.poll();
assertTrue(buffer.isEmpty());
}
@Test
public void givenCircularBuffer_whenFilledToCapacity_thenNoMoreElementsCanBeEnqueued() {
int capacity = shapes.length;
CircularBuffer<String> buffer = new CircularBuffer<>(capacity);
assertTrue(buffer.isEmpty());
for (String shape : shapes) {
buffer.offer(shape);
}
assertTrue(buffer.isFull());
assertFalse(buffer.offer("Octagon"));
}
@Test
public void givenCircularBuffer_whenBufferIsEmpty_thenReturnsNull() {
CircularBuffer<String> buffer = new CircularBuffer<>(1);
assertTrue(buffer.isEmpty());
assertNull(buffer.poll());
}
}

View File

@@ -0,0 +1,80 @@
package com.baeldung.circularbuffer;
import static org.junit.Assert.assertArrayEquals;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import org.junit.jupiter.api.Test;
public class ProducerConsumerLiveTest {
private final String[] shapes = { "Circle", "Triangle", "Rectangle", "Square", "Rhombus", "Trapezoid", "Pentagon", "Pentagram", "Hexagon", "Hexagram" };
@Test
public void givenACircularBuffer_whenInterleavingProducerConsumer_thenElementsMatch() throws Exception {
CircularBuffer<String> buffer = new CircularBuffer<String>(shapes.length);
ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.submit(new Producer<String>(buffer, shapes));
Future<String[]> consumed = executorService.submit(new Consumer<String>(buffer, shapes.length));
String[] shapesConsumed = consumed.get(5L, TimeUnit.SECONDS);
assertArrayEquals(shapes, shapesConsumed);
}
static class Producer<T> implements Runnable {
private CircularBuffer<T> buffer;
private T[] items;
public Producer(CircularBuffer<T> buffer, T[] items) {
this.buffer = buffer;
this.items = items;
}
@Override
public void run() {
for (int i = 0; i < items.length;) {
if (buffer.offer(items[i])) {
System.out.println("Produced: " + items[i]);
i++;
LockSupport.parkNanos(5);
}
}
}
}
@SuppressWarnings("unchecked")
static class Consumer<T> implements Callable<T[]> {
private CircularBuffer<T> buffer;
private int expectedCount;
public Consumer(CircularBuffer<T> buffer, int expectedCount) {
this.buffer = buffer;
this.expectedCount = expectedCount;
}
@Override
public T[] call() throws Exception {
T[] items = (T[]) new Object[expectedCount];
for (int i = 0; i < items.length;) {
T item = buffer.poll();
if (item != null) {
items[i++] = item;
LockSupport.parkNanos(5);
System.out.println("Consumed: " + item);
}
}
return items;
}
}
}