|
| 1 | +package com.baeldung.java9.reactive; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import java.util.concurrent.ForkJoinPool; |
| 7 | +import java.util.concurrent.SubmissionPublisher; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.stream.IntStream; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Rule; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.rules.Stopwatch; |
| 15 | + |
| 16 | +public class BaeldungSubscriberImplTest { |
| 17 | + |
| 18 | + private static final int ITEM_SIZE = 10; |
| 19 | + private SubmissionPublisher<String> publisher; |
| 20 | + private BaeldungSubscriberImpl<String> subscriber; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void initialize() { |
| 24 | + // create Publisher with max buffer capacity 3. |
| 25 | + this.publisher = new SubmissionPublisher<String>(ForkJoinPool.commonPool(), 3); |
| 26 | + this.subscriber = new BaeldungSubscriberImpl<String>(); |
| 27 | + publisher.subscribe(subscriber); |
| 28 | + } |
| 29 | + |
| 30 | + @Rule |
| 31 | + public Stopwatch stopwatch = new Stopwatch() { |
| 32 | + |
| 33 | + }; |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testReactiveStreamCount() { |
| 37 | + IntStream.range(0, ITEM_SIZE) |
| 38 | + .forEach(item -> publisher.submit(item + "")); |
| 39 | + publisher.close(); |
| 40 | + |
| 41 | + do { |
| 42 | + // wait for subscribers to complete all processing. |
| 43 | + try { |
| 44 | + Thread.sleep(100); |
| 45 | + } catch (InterruptedException e) { |
| 46 | + // TODO Auto-generated catch block |
| 47 | + e.printStackTrace(); |
| 48 | + } |
| 49 | + } while (!subscriber.isCompleted()); |
| 50 | + |
| 51 | + int count = subscriber.getCounter(); |
| 52 | + |
| 53 | + assertEquals(ITEM_SIZE, count); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testReactiveStreamTime() { |
| 58 | + IntStream.range(0, ITEM_SIZE) |
| 59 | + .forEach(item -> publisher.submit(item + "")); |
| 60 | + publisher.close(); |
| 61 | + |
| 62 | + do { |
| 63 | + // wait for subscribers to complete all processing. |
| 64 | + try { |
| 65 | + Thread.sleep(100); |
| 66 | + } catch (InterruptedException e) { |
| 67 | + // TODO Auto-generated catch block |
| 68 | + e.printStackTrace(); |
| 69 | + } |
| 70 | + } while (!subscriber.isCompleted()); |
| 71 | + |
| 72 | + // The runtime in seconds should be equal to the number of items. |
| 73 | + assertTrue(stopwatch.runtime(TimeUnit.SECONDS) >= ITEM_SIZE); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testReactiveStreamOffer() { |
| 78 | + IntStream.range(0, ITEM_SIZE) |
| 79 | + .forEach(item -> publisher.offer(item + "", (subscriber, string) -> { |
| 80 | + // Returning false means this item will be dropped (no retry), if blocked. |
| 81 | + return false; |
| 82 | + })); |
| 83 | + publisher.close(); |
| 84 | + |
| 85 | + do { |
| 86 | + // wait for subscribers to complete all processing. |
| 87 | + try { |
| 88 | + Thread.sleep(100); |
| 89 | + } catch (InterruptedException e) { |
| 90 | + // TODO Auto-generated catch block |
| 91 | + e.printStackTrace(); |
| 92 | + } |
| 93 | + } while (!subscriber.isCompleted()); |
| 94 | + |
| 95 | + int count = subscriber.getCounter(); |
| 96 | + // Because 10 items were offered and the buffer capacity was 3, few items will not be processed. |
| 97 | + assertTrue(ITEM_SIZE > count); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments