/** * Block with wait/notifyAll semantics * @param spinTimeout the spin timeout * @param yieldTimeout the yield timeout * @param units the time unit * @return the wait strategy */ public static WaitStrategy phasedOffLiteLock(long spinTimeout, long yieldTimeout, TimeUnit units) { return phasedOff(spinTimeout, yieldTimeout, units, liteBlocking()); }
/** * Creates a new {@link WorkQueueProcessor} using the properties * of this builder. * @return a fresh processor */ public WorkQueueProcessor<T> build() { String name = this.name != null ? this.name : WorkQueueProcessor.class.getSimpleName(); WaitStrategy waitStrategy = this.waitStrategy != null ? this.waitStrategy : WaitStrategy.liteBlocking(); ThreadFactory threadFactory = this.executor != null ? null : new EventLoopFactory(name, autoCancel); ExecutorService requestTaskExecutor = this.requestTaskExecutor != null ? this.requestTaskExecutor : defaultRequestTaskExecutor(defaultName(threadFactory, WorkQueueProcessor.class)); return new WorkQueueProcessor<>( threadFactory, executor, requestTaskExecutor, bufferSize, waitStrategy, share, autoCancel); } }
@Test public void customRequestTaskThreadRejectsNull() { ExecutorService customTaskExecutor = null; Assertions.assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> new WorkQueueProcessor<>( Thread::new, Executors.newCachedThreadPool(), customTaskExecutor, 8, WaitStrategy.liteBlocking(), true, true) ); }
@Test public void customRequestTaskThreadRejectsNull() { ExecutorService customTaskExecutor = null; Assertions.assertThatExceptionOfType(NullPointerException.class) .isThrownBy(() -> new TopicProcessor<>( Thread::new, Executors.newCachedThreadPool(), customTaskExecutor, 8, WaitStrategy.liteBlocking(), true, true, Object::new) ); }
@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 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 testCustomRequestTaskThreadNameCreate() { String expectedName = "workQueueProcessorRequestTaskCreate"; //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)); WorkQueueProcessor<Object> processor = WorkQueueProcessor.builder() .executor(Executors.newCachedThreadPool()) .requestTaskExecutor(customTaskExecutor) .bufferSize(8) .waitStrategy(WaitStrategy.liteBlocking()) .autoCancel(true) .build(); processor.requestTask(Operators.cancelledSubscription()); processor.subscribe(); 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 testCustomRequestTaskThreadNameShare() { String expectedName = "workQueueProcessorRequestTaskShare"; //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)); WorkQueueProcessor<Object> processor = WorkQueueProcessor.builder() .executor(Executors.newCachedThreadPool()) .requestTaskExecutor(customTaskExecutor) .bufferSize(8) .waitStrategy(WaitStrategy.liteBlocking()) .autoCancel(true) .build(); processor.requestTask(Operators.cancelledSubscription()); processor.subscribe(); 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); }
private void assertProcessor(WorkQueueProcessor<Integer> processor, boolean shared, @Nullable String name, @Nullable Integer bufferSize, @Nullable WaitStrategy waitStrategy, @Nullable Boolean autoCancel, @Nullable ExecutorService executor, @Nullable ExecutorService requestTaskExecutor) { String expectedName = name != null ? name : WorkQueueProcessor.class.getSimpleName(); int expectedBufferSize = bufferSize != null ? bufferSize : Queues.SMALL_BUFFER_SIZE; boolean expectedAutoCancel = autoCancel != null ? autoCancel : true; WaitStrategy expectedWaitStrategy = waitStrategy != null ? waitStrategy : WaitStrategy.liteBlocking(); Class<?> sequencerClass = shared ? MultiProducerRingBuffer.class : SingleProducerSequencer.class; assertEquals(expectedName, processor.name); assertEquals(expectedBufferSize, processor.getBufferSize()); assertEquals(expectedAutoCancel, processor.autoCancel); assertEquals(expectedWaitStrategy.getClass(), processor.ringBuffer.getSequencer().waitStrategy.getClass()); assertEquals(sequencerClass, processor.ringBuffer.getSequencer().getClass()); if (executor != null) assertEquals(executor, processor.executor); if (requestTaskExecutor != null) assertEquals(requestTaskExecutor, processor.requestTaskExecutor); }
.name("Processor") .bufferSize(256) .waitStrategy(liteBlocking()) .build(); Scheduler timer = Schedulers.newSingle("Timer");
/** * Block with wait/notifyAll semantics * @param spinTimeout the spin timeout * @param yieldTimeout the yield timeout * @param units the time unit * @return the wait strategy */ public static WaitStrategy phasedOffLiteLock(long spinTimeout, long yieldTimeout, TimeUnit units) { return phasedOff(spinTimeout, yieldTimeout, units, liteBlocking()); }
/** * Creates a new {@link WorkQueueProcessor} using the properties * of this builder. * @return a fresh processor */ public WorkQueueProcessor<T> build() { String name = this.name != null ? this.name : WorkQueueProcessor.class.getSimpleName(); WaitStrategy waitStrategy = this.waitStrategy != null ? this.waitStrategy : WaitStrategy.liteBlocking(); ThreadFactory threadFactory = this.executor != null ? null : new EventLoopFactory(name, autoCancel); ExecutorService requestTaskExecutor = this.requestTaskExecutor != null ? this.requestTaskExecutor : defaultRequestTaskExecutor(defaultName(threadFactory, WorkQueueProcessor.class)); return new WorkQueueProcessor<>( threadFactory, executor, requestTaskExecutor, bufferSize, waitStrategy, share, autoCancel); } }