@SuppressWarnings("unchecked") ReplaySubscriber(ReplayBuffer<T> buffer) { this.buffer = buffer; this.management = new AtomicInteger(); this.subscribers = new AtomicReference<InnerSubscription<T>[]>(EMPTY); this.shouldConnect = new AtomicBoolean(); }
/** * If {@code key} is not already associated with a value or if {@code key} is associated with * zero, associate it with {@code newValue}. Returns the previous value associated with {@code * key}, or zero if there was no mapping for {@code key}. */ long putIfAbsent(K key, long newValue) { AtomicBoolean noValue = new AtomicBoolean(false); Long result = map.compute( key, (k, oldValue) -> { if (oldValue == null || oldValue == 0) { noValue.set(true); return newValue; } else { return oldValue; } }); return noValue.get() ? 0L : result.longValue(); }
@Test(timeout = 5000) public void subscribeActionNormal() { final AtomicBoolean run = new AtomicBoolean(); normal.completable.subscribe(new Action() { @Override public void run() { run.set(true); } }); Assert.assertTrue("Not completed", run.get()); }
@Test public void testJustAndRetry() throws Exception { final AtomicBoolean throwException = new AtomicBoolean(true); int value = Observable.just(1).map(new Function<Integer, Integer>() { @Override public Integer apply(Integer t1) { if (throwException.compareAndSet(true, false)) { throw new TestException(); } return t1; } }).retry(1).blockingSingle(); assertEquals(1, value); }
@Test public void createdExecutorShouldExecuteSubmittedTasks() throws Exception { AtomicBoolean exitCondition = new AtomicBoolean( false ); AtomicInteger threadCounter = new AtomicInteger( 0 ); executorService = factory.create( 0, 1, Duration.ZERO, 0, false, newThreadFactoryWithCounter( threadCounter ) ); assertNotNull( executorService ); assertEquals( 0, threadCounter.get() ); Future task1 = executorService.submit( newInfiniteWaitingRunnable( exitCondition ) ); assertEquals( 1, threadCounter.get() ); exitCondition.set( true ); assertNull( task1.get( 1, MINUTES ) ); assertTrue( task1.isDone() ); assertFalse( task1.isCancelled() ); }
@Test public void testEmitReentrancy() { AtomicInteger count = new AtomicInteger(2); AtomicBoolean emitting = new AtomicBoolean(); stream.pause(); stream.fetch(3); stream.handler(item -> { assertFalse(emitting.getAndSet(true)); emitted.add(item); stream.emit(count.getAndIncrement()); emitting.set(false); }); stream.emit(0, 1); assertEquals(Arrays.asList(0, 1, 2), emitted); }
@Test public void mustRetryCloseIfFailure() throws Exception { // GIVEN AtomicBoolean throwOnNext = new AtomicBoolean(); IOException exception = new IOException( "My failure" ); PageCache pageCache = pageCacheThatThrowExceptionWhenToldTo( exception, throwOnNext ); try ( GBPTree<MutableLong, MutableLong> ignored = index( pageCache ).build() ) { // WHEN throwOnNext.set( true ); } }
public void testCustomSchedule_startStop() throws Exception { final CyclicBarrier firstBarrier = new CyclicBarrier(2); final CyclicBarrier secondBarrier = new CyclicBarrier(2); final AtomicBoolean shouldWait = new AtomicBoolean(true); Runnable task = new Runnable() { @Override public void run() { try { if (shouldWait.get()) { firstBarrier.await(); secondBarrier.await(); } } catch (Exception e) { throw new RuntimeException(e); } } }; TestCustomScheduler scheduler = new TestCustomScheduler(); Future<?> future = scheduler.schedule(null, Executors.newScheduledThreadPool(10), task); firstBarrier.await(); assertEquals(1, scheduler.scheduleCounter.get()); secondBarrier.await(); firstBarrier.await(); assertEquals(2, scheduler.scheduleCounter.get()); shouldWait.set(false); secondBarrier.await(); future.cancel(false); }
@Test public void testDoOnCompleted() { final AtomicBoolean r = new AtomicBoolean(); String output = Observable.just("one").doOnComplete(new Action() { @Override public void run() { r.set(true); } }).blockingSingle(); assertEquals("one", output); assertTrue(r.get()); }
@Test public void testIssue3008RetryInfinite() { final List<Long> list = new CopyOnWriteArrayList<Long>(); final AtomicBoolean isFirst = new AtomicBoolean(true); Observable.<Long> just(1L, 2L, 3L).map(new Function<Long, Long>() { @Override public Long apply(Long x) { System.out.println("map " + x); if (x == 2 && isFirst.getAndSet(false)) { throw new RuntimeException("retryable error"); } return x; }}) .retry() .forEach(new Consumer<Long>() { @Override public void accept(Long t) { System.out.println(t); list.add(t); }}); assertEquals(Arrays.asList(1L, 1L, 2L, 3L), list); }
@Test public void testFetchReentrancy() { AtomicInteger count = new AtomicInteger(2); stream.pause(); AtomicBoolean emitting = new AtomicBoolean(); stream.handler(item -> { assertFalse(emitting.getAndSet(true)); emitted.add(item); stream.emit(count.getAndIncrement()); emitting.set(false); }); stream.emit(0, 1); stream.fetch(3); assertEquals(Arrays.asList(0, 1, 2), emitted); } }
WindowOverlapSubscriber(Subscriber<? super Flowable<T>> actual, long size, long skip, int bufferSize) { super(1); this.downstream = actual; this.size = size; this.skip = skip; this.queue = new SpscLinkedArrayQueue<UnicastProcessor<T>>(bufferSize); this.windows = new ArrayDeque<UnicastProcessor<T>>(); this.once = new AtomicBoolean(); this.firstRequest = new AtomicBoolean(); this.requested = new AtomicLong(); this.wip = new AtomicInteger(); this.bufferSize = bufferSize; }
public AsyncQueue<InternalHiveSplit> queueFor(OptionalInt bucketNumber) { checkArgument(bucketNumber.isPresent()); AtomicBoolean isNew = new AtomicBoolean(); AsyncQueue<InternalHiveSplit> queue = queues.computeIfAbsent(bucketNumber.getAsInt(), ignored -> { isNew.set(true); return new AsyncQueue<>(estimatedOutstandingSplitsPerBucket, executor); }); if (isNew.get() && finished.get()) { // Check `finished` and invoke `queue.finish` after the `queue` is added to the map. // Otherwise, `queue.finish` may not be invoked if `finished` is set while the lambda above is being evaluated. queue.finish(); } return queue; } },
private static AtomicBoolean addPage(ClientBuffer buffer, Page page) { AtomicBoolean dereferenced = new AtomicBoolean(true); SerializedPageReference serializedPageReference = new SerializedPageReference(PAGES_SERDE.serialize(page), 1, () -> dereferenced.set(false)); buffer.enqueuePages(ImmutableList.of(serializedPageReference)); serializedPageReference.dereferencePage(); return dereferenced; }
@Test public void doOnTerminateComplete() { final AtomicBoolean r = new AtomicBoolean(); String output = Observable.just("one").doOnTerminate(new Action() { @Override public void run() { r.set(true); } }).blockingSingle(); assertEquals("one", output); assertTrue(r.get()); }
@Test public void testIssue3008RetryWithPredicate() { final List<Long> list = new CopyOnWriteArrayList<Long>(); final AtomicBoolean isFirst = new AtomicBoolean(true); Observable.<Long> just(1L, 2L, 3L).map(new Function<Long, Long>() { @Override public Long apply(Long x) { System.out.println("map " + x); if (x == 2 && isFirst.getAndSet(false)) { throw new RuntimeException("retryable error"); } return x; }}) .retry(new BiPredicate<Integer, Throwable>() { @Override public boolean test(Integer t1, Throwable t2) { return true; }}) .forEach(new Consumer<Long>() { @Override public void accept(Long t) { System.out.println(t); list.add(t); }}); assertEquals(Arrays.asList(1L, 1L, 2L, 3L), list); }
@SuppressWarnings("unchecked") ReplaySubscriber(ReplayBuffer<T> buffer) { this.buffer = buffer; this.management = new AtomicInteger(); this.subscribers = new AtomicReference<InnerSubscription<T>[]>(EMPTY); this.shouldConnect = new AtomicBoolean(); }
@Test void streamShouldCloseSingleOnCompleted() { // Given AtomicBoolean closed = new AtomicBoolean( false ); ResourceIterator<Integer> resourceIterator = newResourceIterator( iterator( new Integer[]{1, 2, 3} ), () -> closed.set( true ) ); ResourceIterable<Integer> iterable = () -> resourceIterator; // When List<Integer> result = iterable.stream().collect( Collectors.toList() ); // Then assertEquals( asList(1,2,3), result ); assertTrue( closed.get() ); }
@Test public void onTerminateCalledWhenOnError() { final AtomicBoolean didRunOnTerminate = new AtomicBoolean(); UnicastProcessor<Integer> us = UnicastProcessor.create(Observable.bufferSize(), new Runnable() { @Override public void run() { didRunOnTerminate.set(true); } }); assertEquals(false, didRunOnTerminate.get()); us.onError(new RuntimeException("some error")); assertEquals(true, didRunOnTerminate.get()); }
@Test public void testIssue3008RetryInfinite() { final List<Long> list = new CopyOnWriteArrayList<Long>(); final AtomicBoolean isFirst = new AtomicBoolean(true); Flowable.<Long> just(1L, 2L, 3L).map(new Function<Long, Long>() { @Override public Long apply(Long x) { System.out.println("map " + x); if (x == 2 && isFirst.getAndSet(false)) { throw new RuntimeException("retryable error"); } return x; }}) .retry() .forEach(new Consumer<Long>() { @Override public void accept(Long t) { System.out.println(t); list.add(t); }}); assertEquals(Arrays.asList(1L, 1L, 2L, 3L), list); }