Search in sources :

Example 26 with Scheduler

use of rx.Scheduler in project rxrabbit by meltwater.

the class RxRabbitTests method consumer_closes_internal_subscriber_on_error_during_connection.

@Test
public void consumer_closes_internal_subscriber_on_error_during_connection() throws Exception {
    MonitoringTestThreadFactory threadFactory = new MonitoringTestThreadFactory();
    Scheduler threadPoolScheduler = new CachedThreadScheduler(threadFactory);
    RxJavaHooks.setOnIOScheduler((ioScheduler) -> threadPoolScheduler);
    CountDownLatch retries = new CountDownLatch(10);
    ConsumerSettings consumerSettings = new ConsumerSettings().withRetryCount(ConsumerSettings.RETRY_FOREVER).withNumChannels(1).withPreFetchCount(1024).withBackoffAlgorithm(integer -> {
        retries.countDown();
        return 1;
    });
    Observable<Message> consumer = new DefaultConsumerFactory(channelFactory, consumerSettings).createConsumer("non-existent-queue");
    Subscription subscribe = consumer.subscribe();
    retries.await();
    subscribe.unsubscribe();
    assertThat(threadFactory.getAliveThreads(), lessThan(10));
}
Also used : CachedThreadScheduler(rx.internal.schedulers.CachedThreadScheduler) Scheduler(rx.Scheduler) CachedThreadScheduler(rx.internal.schedulers.CachedThreadScheduler) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) MonitoringTestThreadFactory(com.meltwater.rxrabbit.util.MonitoringTestThreadFactory) Test(org.junit.Test)

Example 27 with Scheduler

use of rx.Scheduler in project AnDevCon-RxPatterns by colintheshots.

the class Example16Test method using_testscheduler_to_simulate_network_events.

@Test
public void using_testscheduler_to_simulate_network_events() {
    // TestScheduler lets you advance time by hand
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<NetworkResponse> subscriber = new TestSubscriber<>();
    // Scheduler.Worker lets you schedule events in time
    Scheduler.Worker worker = scheduler.createWorker();
    // Subjects allow both input and output, so they can be swapped in for
    // Retrofit calls to unit test your code.
    final PublishSubject<NetworkResponse> networkSubject = PublishSubject.create();
    // schedule a first observable event to occur at 1000 ms
    worker.schedule(new Action0() {

        @Override
        public void call() {
            // explicitly calling onNext in a worker allows one to
            // create a very specific test of timed events
            networkSubject.onNext(new NetworkResponse(401));
        }
    }, 1000, TimeUnit.MILLISECONDS);
    // schedule a second observable event to occur at 2000 ms
    worker.schedule(new Action0() {

        @Override
        public void call() {
            networkSubject.onNext(new NetworkResponse(200));
            networkSubject.onCompleted();
        }
    }, 2000, TimeUnit.MILLISECONDS);
    // we must subscribe before anticipating results
    networkSubject.subscribeOn(scheduler).subscribe(subscriber);
    // we can manually advance time using the scheduler and check assertions
    scheduler.advanceTimeBy(1500, TimeUnit.MILLISECONDS);
    subscriber.assertReceivedOnNext(Arrays.asList(new NetworkResponse(401)));
    // awaitTerminalEvent will wait forever if we don't advance time enough
    scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS);
    subscriber.awaitTerminalEvent();
    subscriber.assertReceivedOnNext(Arrays.asList(new NetworkResponse(401), new NetworkResponse(200)));
    // TestSubscriber provides many useful methods
    subscriber.assertNoErrors();
    subscriber.assertValueCount(2);
    subscriber.assertUnsubscribed();
}
Also used : Action0(rx.functions.Action0) TestScheduler(rx.schedulers.TestScheduler) Scheduler(rx.Scheduler) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Override(java.lang.Override) Test(org.junit.Test)

Aggregations

Scheduler (rx.Scheduler)27 Test (org.junit.Test)13 TestScheduler (rx.schedulers.TestScheduler)9 Observable (rx.Observable)7 Schedulers (rx.schedulers.Schedulers)7 Executors (java.util.concurrent.Executors)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Logger (org.slf4j.Logger)5 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)4 Member (io.scalecube.cluster.Member)4 Message (io.scalecube.transport.Message)4 Transport (io.scalecube.transport.Transport)4 Collections (java.util.Collections)4 ExecutorService (java.util.concurrent.ExecutorService)4 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)4 TimeUnit (java.util.concurrent.TimeUnit)4 LoggerFactory (org.slf4j.LoggerFactory)4 Collection (java.util.Collection)3 ScheduledFuture (java.util.concurrent.ScheduledFuture)3