@Test public void testCustomRequestTaskThreadName() { String expectedName = "topicProcessorRequestTaskCreate"; //NOTE: the below single executor should not be used usually as requestTask assumes it immediately gets executed ExecutorService customTaskExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, expectedName)); TopicProcessor<Object> processor = TopicProcessor.builder() .executor(Executors.newCachedThreadPool()) .requestTaskExecutor(customTaskExecutor) .bufferSize(8) .waitStrategy(WaitStrategy.liteBlocking()) .autoCancel(true) .build(); processor.requestTask(Operators.cancelledSubscription()); Thread[] threads = new Thread[Thread.activeCount()]; Thread.enumerate(threads); //cleanup to avoid visibility in other tests customTaskExecutor.shutdownNow(); processor.forceShutdown(); Condition<Thread> customRequestTaskThread = new Condition<>( thread -> thread != null && expectedName.equals(thread.getName()), "a thread named \"%s\"", expectedName); Assertions.assertThat(threads) .haveExactly(1, customRequestTaskThread); }
@Test public void testDefaultRequestTaskThreadName() { String mainName = "topicProcessorRequestTask"; String expectedName = mainName + "[request-task]"; TopicProcessor<Object> processor = TopicProcessor.builder().name(mainName).bufferSize(8).build(); processor.requestTask(Operators.cancelledSubscription()); Thread[] threads = new Thread[Thread.activeCount()]; Thread.enumerate(threads); //cleanup to avoid visibility in other tests processor.forceShutdown(); Condition<Thread> defaultRequestTaskThread = new Condition<>( thread -> thread != null && expectedName.equals(thread.getName()), "a thread named \"%s\"", expectedName); Assertions.assertThat(threads) .haveExactly(1, defaultRequestTaskThread); }
@Test public void testForceShutdownWhileWaitingForRequest() throws InterruptedException { TopicProcessor<String> processor = TopicProcessor.<String>builder().name("processor").bufferSize(4).build(); Publisher<String> publisher = Flux.fromArray(new String[] { "1", "2", "3", "4", "5" }); publisher.subscribe(processor); AssertSubscriber<String> subscriber = AssertSubscriber.create(0); processor.subscribe(subscriber); subscriber.request(1); Thread.sleep(250); processor.forceShutdown(); assertTrue(processor.awaitAndShutdown(Duration.ofSeconds(1))); }
@Test public void testCustomRequestTaskThreadShare() { String expectedName = "topicProcessorRequestTaskShare"; //NOTE: the below single executor should not be used usually as requestTask assumes it immediately gets executed ExecutorService customTaskExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, expectedName)); TopicProcessor<Object> processor = TopicProcessor.builder().share(true) .executor(Executors.newCachedThreadPool()) .requestTaskExecutor(customTaskExecutor) .bufferSize(8) .waitStrategy(WaitStrategy.liteBlocking()) .autoCancel(true) .build(); processor.requestTask(Operators.cancelledSubscription()); Thread[] threads = new Thread[Thread.activeCount()]; Thread.enumerate(threads); //cleanup to avoid visibility in other tests customTaskExecutor.shutdownNow(); processor.forceShutdown(); Condition<Thread> customRequestTaskThread = new Condition<>( thread -> thread != null && expectedName.equals(thread.getName()), "a thread named \"%s\"", expectedName); Assertions.assertThat(threads) .haveExactly(1, customRequestTaskThread); }
@Test public void testForceShutdownWhileWaitingForMoreData() throws InterruptedException { TopicProcessor<String> processor = TopicProcessor.<String>builder().name("processor").bufferSize(4).build(); Publisher<String> publisher = new CappedPublisher(2); publisher.subscribe(processor); AssertSubscriber<String> subscriber = AssertSubscriber.create(0); processor.subscribe(subscriber); subscriber.request(3); Thread.sleep(250); processor.forceShutdown(); assertTrue(processor.awaitAndShutdown(Duration.ofSeconds(5))); }
@Test public void testForceShutdownAfterShutdown() throws InterruptedException { TopicProcessor<String> processor = TopicProcessor.<String>builder().name("processor").bufferSize(4).build(); Publisher<String> publisher = Flux.fromArray(new String[] { "1", "2", "3", "4", "5" }); publisher.subscribe(processor); AssertSubscriber<String> subscriber = AssertSubscriber.create(0); processor.subscribe(subscriber); subscriber.request(1); Thread.sleep(250); processor.shutdown(); assertFalse(processor.awaitAndShutdown(Duration.ofMillis(400))); processor.forceShutdown(); assertTrue(processor.awaitAndShutdown(Duration.ofMillis(400))); }
@Test public void testForceShutdownWhileWaitingForInitialRequest() throws InterruptedException { TopicProcessor<String> processor = TopicProcessor.<String>builder().name("processor").bufferSize(4).build(); Publisher<String> publisher = new CappedPublisher(2); publisher.subscribe(processor); AssertSubscriber<String> subscriber = AssertSubscriber.create(0); processor.subscribe(subscriber); processor.forceShutdown(); assertTrue(processor.awaitAndShutdown(Duration.ofSeconds(5))); }
@Test public void drainTest() throws Exception { final TopicProcessor<Integer> sink = TopicProcessor.<Integer>builder().name("topic").build(); sink.onNext(1); sink.onNext(2); sink.onNext(3); sink.forceShutdown() .subscribeWith(AssertSubscriber.create()) .assertComplete() .assertValues(1, 2, 3); }