Search in sources :

Example 46 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project disruptor by LMAX-Exchange.

the class WorkerPoolTest method shouldProcessOnlyOnceItHasBeenPublished.

@SuppressWarnings("unchecked")
@Test
public void shouldProcessOnlyOnceItHasBeenPublished() throws Exception {
    Executor executor = Executors.newCachedThreadPool(DaemonThreadFactory.INSTANCE);
    WorkerPool<AtomicLong> pool = new WorkerPool<AtomicLong>(new AtomicLongEventFactory(), new FatalExceptionHandler(), new AtomicLongWorkHandler(), new AtomicLongWorkHandler());
    RingBuffer<AtomicLong> ringBuffer = pool.start(executor);
    ringBuffer.next();
    ringBuffer.next();
    Thread.sleep(1000);
    assertThat(ringBuffer.get(0).get(), is(0L));
    assertThat(ringBuffer.get(1).get(), is(0L));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Executor(java.util.concurrent.Executor) Test(org.junit.Test)

Example 47 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project RxJava by ReactiveX.

the class CompletableTimerTest method timer.

@Test
public void timer() {
    final TestScheduler testScheduler = new TestScheduler();
    final AtomicLong atomicLong = new AtomicLong();
    Completable.timer(2, TimeUnit.SECONDS, testScheduler).subscribe(new Action() {

        @Override
        public void run() throws Exception {
            atomicLong.incrementAndGet();
        }
    });
    assertEquals(0, atomicLong.get());
    testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertEquals(0, atomicLong.get());
    testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertEquals(1, atomicLong.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(io.reactivex.functions.Action) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 48 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project RxJava by ReactiveX.

the class FlowableAmbTest method testProducerRequestThroughAmb.

@SuppressWarnings("unchecked")
@Test
public void testProducerRequestThroughAmb() {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>(0L);
    ts.request(3);
    final AtomicLong requested1 = new AtomicLong();
    final AtomicLong requested2 = new AtomicLong();
    Flowable<Integer> o1 = Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> s) {
            s.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                    System.out.println("1-requested: " + n);
                    requested1.set(n);
                }

                @Override
                public void cancel() {
                }
            });
        }
    });
    Flowable<Integer> o2 = Flowable.unsafeCreate(new Publisher<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer> s) {
            s.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                    System.out.println("2-requested: " + n);
                    requested2.set(n);
                }

                @Override
                public void cancel() {
                }
            });
        }
    });
    Flowable.ambArray(o1, o2).subscribe(ts);
    assertEquals(3, requested1.get());
    assertEquals(3, requested2.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 49 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project RxJava by ReactiveX.

the class FlowableAmbTest method testSubscriptionOnlyHappensOnce.

@SuppressWarnings("unchecked")
@Test
public void testSubscriptionOnlyHappensOnce() throws InterruptedException {
    final AtomicLong count = new AtomicLong();
    Consumer<Subscription> incrementer = new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) {
            count.incrementAndGet();
        }
    };
    //this aync stream should emit first
    Flowable<Integer> o1 = Flowable.just(1).doOnSubscribe(incrementer).delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
    //this stream emits second
    Flowable<Integer> o2 = Flowable.just(1).doOnSubscribe(incrementer).delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    Flowable.ambArray(o1, o2).subscribe(ts);
    ts.request(1);
    ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
    ts.assertNoErrors();
    assertEquals(2, count.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 50 with AtomicLong

use of java.util.concurrent.atomic.AtomicLong in project RxJava by ReactiveX.

the class FlowableSkipTest method testBackpressureMultipleSmallAsyncRequests.

@Test
public void testBackpressureMultipleSmallAsyncRequests() throws InterruptedException {
    final AtomicLong requests = new AtomicLong(0);
    TestSubscriber<Long> ts = new TestSubscriber<Long>(0L);
    Flowable.interval(100, TimeUnit.MILLISECONDS).doOnRequest(new LongConsumer() {

        @Override
        public void accept(long n) {
            requests.addAndGet(n);
        }
    }).skip(4).subscribe(ts);
    Thread.sleep(100);
    ts.request(1);
    ts.request(1);
    Thread.sleep(100);
    ts.dispose();
    // FIXME not assertable anymore
    //        ts.assertUnsubscribed();
    ts.assertNoErrors();
    assertEquals(6, requests.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) LongConsumer(io.reactivex.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Aggregations

AtomicLong (java.util.concurrent.atomic.AtomicLong)2292 Test (org.junit.Test)986 ArrayList (java.util.ArrayList)300 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)273 IOException (java.io.IOException)254 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)250 List (java.util.List)222 HashMap (java.util.HashMap)212 Map (java.util.Map)209 CountDownLatch (java.util.concurrent.CountDownLatch)185 AtomicReference (java.util.concurrent.atomic.AtomicReference)174 HashSet (java.util.HashSet)106 Arrays (java.util.Arrays)101 File (java.io.File)99 Test (org.junit.jupiter.api.Test)98 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)95 Set (java.util.Set)94 TimeUnit (java.util.concurrent.TimeUnit)91 Collections (java.util.Collections)88 Random (java.util.Random)85