From 02e7129ff59c855b835f8eb915563d6b18644cd6 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Mon, 15 Jun 2020 18:44:24 +0530 Subject: [PATCH] simple producer consumer test --- .../CircularBufferUnitTest.java | 3 +- .../ProducerConsumerLiveTest.java | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 data-structures/src/test/java/com/baeldung/circularbuffer/ProducerConsumerLiveTest.java diff --git a/data-structures/src/test/java/com/baeldung/circularbuffer/CircularBufferUnitTest.java b/data-structures/src/test/java/com/baeldung/circularbuffer/CircularBufferUnitTest.java index 34658b9251..0809444901 100644 --- a/data-structures/src/test/java/com/baeldung/circularbuffer/CircularBufferUnitTest.java +++ b/data-structures/src/test/java/com/baeldung/circularbuffer/CircularBufferUnitTest.java @@ -2,7 +2,7 @@ package com.baeldung.circularbuffer; import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class CircularBufferUnitTest { @@ -40,5 +40,4 @@ public class CircularBufferUnitTest { assertEquals(true, buffer.isFull()); } - } diff --git a/data-structures/src/test/java/com/baeldung/circularbuffer/ProducerConsumerLiveTest.java b/data-structures/src/test/java/com/baeldung/circularbuffer/ProducerConsumerLiveTest.java new file mode 100644 index 0000000000..06b1c5e4cf --- /dev/null +++ b/data-structures/src/test/java/com/baeldung/circularbuffer/ProducerConsumerLiveTest.java @@ -0,0 +1,77 @@ +package com.baeldung.circularbuffer; + +import static org.junit.Assert.assertArrayEquals; + +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 InterruptedException { + CircularBuffer buffer = new CircularBuffer(shapes.length); + String[] consumedShapes = new String[shapes.length]; + + Thread producer = new Thread(new Producer(shapes, buffer)); + Thread consumer = new Thread(new Consumer(consumedShapes, buffer)); + + producer.start(); + consumer.start(); + + producer.join(); + consumer.join(); + + assertArrayEquals(shapes, consumedShapes); + } + + static class Producer implements Runnable { + + private String[] producerShapes; + private CircularBuffer buffer; + + public Producer(String[] producerShapes, CircularBuffer buffer) { + this.producerShapes = producerShapes; + this.buffer = buffer; + } + + @Override + public void run() { + + for (int i = 0; i < producerShapes.length;) { + if (buffer.offer(producerShapes[i])) { + System.out.println("Produced: " + producerShapes[i]); + i++; + LockSupport.parkNanos(5); + } + } + } + } + + static class Consumer implements Runnable { + + private CircularBuffer buffer; + private String[] consumedShapes; + + public Consumer(String[] consumedShapes, CircularBuffer buffer) { + this.consumedShapes = consumedShapes; + this.buffer = buffer; + } + + @Override + public void run() { + + for (int i = 0; i < consumedShapes.length;) { + String shape = buffer.poll(); + if (shape != null) { + consumedShapes[i++] = shape; + + LockSupport.parkNanos(5); + System.out.println("Consumed: " + shape); + } + } + } + } +}