Java Code Examples for rx.Scheduler.Worker#unsubscribe()

The following examples show how to use rx.Scheduler.Worker#unsubscribe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ObsTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testCachedScheduledReset() {
    TestScheduler scheduler = new TestScheduler();
    Worker worker = scheduler.createWorker();
    try {
        final AtomicInteger count = new AtomicInteger(0);
        Observable<Integer> source = Observable.defer(new Func0<Observable<Integer>>() {
            @Override
            public Observable<Integer> call() {
                return Observable.just(count.incrementAndGet());
            }
        })
                // cache
                .compose(Transformers.<Integer> cache(5, TimeUnit.MINUTES, worker));
        assertEquals(1, (int) source.toBlocking().single());
        scheduler.advanceTimeBy(1, TimeUnit.MINUTES);
        assertEquals(1, (int) source.toBlocking().single());
        scheduler.advanceTimeBy(1, TimeUnit.MINUTES);
        assertEquals(1, (int) source.toBlocking().single());
        scheduler.advanceTimeBy(3, TimeUnit.MINUTES);
        assertEquals(2, (int) source.toBlocking().single());
        assertEquals(2, (int) source.toBlocking().single());
    } finally {
        worker.unsubscribe();
    }
}
 
Example 2
Source File: RxJavaComputationSchedulerSensor.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private List<WorkerTaskScheduler> findComputationSchedulerWorkers() {
    List<WorkerTaskScheduler> workerTaskSchedulers = new ArrayList<>();
    try {
        int cpuCount = Runtime.getRuntime().availableProcessors();
        Set<Thread> threads = new HashSet<>();
        int iterCount = 0;
        while (workerTaskSchedulers.size() < cpuCount && iterCount < MAX_LOOP_COUNT) {
            iterCount++;

            Worker worker = Schedulers.computation().createWorker();
            AtomicReference<Thread> workerThreadRef = new AtomicReference<>();
            CountDownLatch latch = new CountDownLatch(1);
            worker.schedule(() -> {
                workerThreadRef.set(Thread.currentThread());
                latch.countDown();
            });
            if (!latch.await(MAX_INIT_PROCESSING_WAIT_TIME_MS, TimeUnit.MILLISECONDS)) {
                logger.warn("RxJava computation scheduler sensor initialization error");
                return Collections.emptyList();
            }
            if (threads.add(workerThreadRef.get())) {
                workerTaskSchedulers.add(new WorkerTaskScheduler(workerThreadRef.get(), worker));
            } else {
                worker.unsubscribe();
            }
        }
        return workerTaskSchedulers;
    } catch (Exception e) {
        logger.warn("RxJava computation scheduler sensor initialization error", e);
        workerTaskSchedulers.forEach(WorkerTaskScheduler::shutdown);
        return Collections.emptyList();
    }
}
 
Example 3
Source File: PublishSubjectRaceTest.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Test
public void racy() throws Exception {
    Worker worker = Schedulers.computation().createWorker();
    try {
        for (int i = 0; i < 1000; i++) {
            AtomicInteger wip = new AtomicInteger(2);
    
            PublishSubject<Integer> ps = PublishSubject.create();
            
            AssertableSubscriber<Integer> as = ps.test(1);
            
            CountDownLatch cdl = new CountDownLatch(1);
            
            worker.schedule(() -> {
                if (wip.decrementAndGet() != 0) {
                    while (wip.get() != 0) ;
                }
                ps.onNext(1);
                
                cdl.countDown();
            });
            if (wip.decrementAndGet() != 0) {
                while (wip.get() != 0) ;
            }
            ps.onNext(1);
            
            cdl.await();
            
            as.assertFailure(MissingBackpressureException.class, 1);
        }
    } finally {
        worker.unsubscribe();
    }
}
 
Example 4
Source File: PublishSubjectRaceTest.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Test
public void nonRacy() throws Exception {
    Worker worker = Schedulers.computation().createWorker();
    try {
        for (int i = 0; i < 1000; i++) {
            AtomicInteger wip = new AtomicInteger(2);
    
            Subject<Integer, Integer> ps = PublishSubject.<Integer>create().toSerialized();
            
            AssertableSubscriber<Integer> as = ps.test(1);
            
            CountDownLatch cdl = new CountDownLatch(1);
            
            worker.schedule(() -> {
                if (wip.decrementAndGet() != 0) {
                    while (wip.get() != 0) ;
                }
                ps.onNext(1);
                
                cdl.countDown();
            });
            if (wip.decrementAndGet() != 0) {
                while (wip.get() != 0) ;
            }
            ps.onNext(1);
            
            cdl.await();
            
            as.assertFailure(MissingBackpressureException.class, 1);
        }
    } finally {
        worker.unsubscribe();
    }
}