Search in sources :

Example 81 with Action1

use of rx.functions.Action1 in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testRequestContextOnTimeout.

private RequestContextTestResults testRequestContextOnTimeout(ExecutionIsolationStrategy isolation, final Scheduler userScheduler) {
    final RequestContextTestResults results = new RequestContextTestResults();
    TestHystrixObservableCommand<Boolean> command = new TestHystrixObservableCommand<Boolean>(TestHystrixObservableCommand.testPropsBuilder(new TestCircuitBreaker()).setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter().withExecutionIsolationStrategy(isolation).withExecutionTimeoutInMilliseconds(50))) {

        @Override
        protected Observable<Boolean> construct() {
            return Observable.create(new OnSubscribe<Boolean>() {

                @Override
                public void call(Subscriber<? super Boolean> s) {
                    results.isContextInitialized.set(HystrixRequestContext.isCurrentThreadInitialized());
                    results.originThread.set(Thread.currentThread());
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    // ignore the interrupted exception
                    }
                }
            }).subscribeOn(userScheduler);
        }
    };
    results.command = command;
    command.toObservable().doOnEach(new Action1<Notification<? super Boolean>>() {

        @Override
        public void call(Notification<? super Boolean> n) {
            results.isContextInitializedObserveOn.set(HystrixRequestContext.isCurrentThreadInitialized());
            results.observeOnThread.set(Thread.currentThread());
        }
    }).subscribe(results.ts);
    results.ts.awaitTerminalEvent();
    System.out.println("Run => Initialized: " + results.isContextInitialized.get() + "  Thread: " + results.originThread.get());
    System.out.println("Observed => Initialized: " + results.isContextInitializedObserveOn.get() + "  Thread: " + results.observeOnThread.get());
    assertEquals(1, results.ts.getOnErrorEvents().size());
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertFalse(command.isSuccessfulExecution());
    assertTrue(command.isResponseTimedOut());
    assertCommandExecutionEvents(command, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
    return results;
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) Action1(rx.functions.Action1) TestSubscriber(rx.observers.TestSubscriber) OnSubscribe(rx.Observable.OnSubscribe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 82 with Action1

use of rx.functions.Action1 in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testRequestContextOnBadFailure.

private RequestContextTestResults testRequestContextOnBadFailure(ExecutionIsolationStrategy isolation, final Scheduler userScheduler) {
    final RequestContextTestResults results = new RequestContextTestResults();
    TestHystrixObservableCommand<Boolean> command = new TestHystrixObservableCommand<Boolean>(TestHystrixObservableCommand.testPropsBuilder(new TestCircuitBreaker()).setCommandPropertiesDefaults(HystrixCommandPropertiesTest.getUnitTestPropertiesSetter().withExecutionIsolationStrategy(isolation))) {

        @Override
        protected Observable<Boolean> construct() {
            return Observable.create(new OnSubscribe<Boolean>() {

                @Override
                public void call(Subscriber<? super Boolean> s) {
                    results.isContextInitialized.set(HystrixRequestContext.isCurrentThreadInitialized());
                    results.originThread.set(Thread.currentThread());
                    throw new RuntimeException("bad onError");
                }
            }).subscribeOn(userScheduler);
        }
    };
    results.command = command;
    command.toObservable().doOnEach(new Action1<Notification<? super Boolean>>() {

        @Override
        public void call(Notification<? super Boolean> n) {
            results.isContextInitializedObserveOn.set(HystrixRequestContext.isCurrentThreadInitialized());
            results.observeOnThread.set(Thread.currentThread());
        }
    }).subscribe(results.ts);
    results.ts.awaitTerminalEvent();
    System.out.println("Run => Initialized: " + results.isContextInitialized.get() + "  Thread: " + results.originThread.get());
    System.out.println("Observed => Initialized: " + results.isContextInitializedObserveOn.get() + "  Thread: " + results.observeOnThread.get());
    assertEquals(1, results.ts.getOnErrorEvents().size());
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertFalse(command.isSuccessfulExecution());
    assertTrue(command.isFailedExecution());
    assertCommandExecutionEvents(command, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
    return results;
}
Also used : TestCircuitBreaker(com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) Action1(rx.functions.Action1) TestSubscriber(rx.observers.TestSubscriber) OnSubscribe(rx.Observable.OnSubscribe) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 83 with Action1

use of rx.functions.Action1 in project MyJapanese by 54wall.

the class MainActivity method setViewPager.

@Override
public void setViewPager(final List<BannerItem> data) {
    if (bannerSubscription != null && bannerSubscription.isUnsubscribed()) {
        bannerSubscription.unsubscribe();
    }
    mBannerViewPager.setAdapter(new BannerPagerAdapter(getSupportFragmentManager(), data));
    mCircleIndicator.setViewPager(mBannerViewPager);
    bannerSubscription = Observable.timer(10, 10, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Action1<Long>() {

        @Override
        public void call(Long aLong) {
            int next = (mBannerViewPager.getCurrentItem() + 1) % data.size();
            mBannerViewPager.setCurrentItem(next);
        }
    });
}
Also used : BannerPagerAdapter(pri.weiqiang.myjapanese.ui.adapter.BannerPagerAdapter) Action1(rx.functions.Action1)

Example 84 with Action1

use of rx.functions.Action1 in project frodo by android10.

the class LogEverythingObservable method get.

@Override
<T> Observable<T> get(T type) throws Throwable {
    final StopWatch stopWatch = new StopWatch();
    final Counter emittedItems = new Counter(joinPoint.getMethodName());
    return ((Observable<T>) joinPoint.proceed()).doOnSubscribe(new Action0() {

        @Override
        public void call() {
            stopWatch.start();
            messageManager.printObservableOnSubscribe(observableInfo);
        }
    }).doOnEach(new Action1<Notification<? super T>>() {

        @Override
        public void call(Notification<? super T> notification) {
            if (!observableInfo.getSubscribeOnThread().isPresent() && (notification.isOnNext() || notification.isOnError())) {
                observableInfo.setSubscribeOnThread(Thread.currentThread().getName());
            }
        }
    }).doOnNext(new Action1<T>() {

        @Override
        public void call(T value) {
            emittedItems.increment();
            messageManager.printObservableOnNextWithValue(observableInfo, value);
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            messageManager.printObservableOnError(observableInfo, throwable);
        }
    }).doOnCompleted(new Action0() {

        @Override
        public void call() {
            messageManager.printObservableOnCompleted(observableInfo);
        }
    }).doOnTerminate(new Action0() {

        @Override
        public void call() {
            stopWatch.stop();
            observableInfo.setTotalExecutionTime(stopWatch.getTotalTimeMillis());
            observableInfo.setTotalEmittedItems(emittedItems.tally());
            messageManager.printObservableOnTerminate(observableInfo);
            messageManager.printObservableItemTimeInfo(observableInfo);
        }
    }).doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            if (!observableInfo.getObserveOnThread().isPresent()) {
                observableInfo.setObserveOnThread(Thread.currentThread().getName());
            }
            messageManager.printObservableThreadInfo(observableInfo);
            messageManager.printObservableOnUnsubscribe(observableInfo);
        }
    });
}
Also used : Action0(rx.functions.Action0) Counter(com.fernandocejas.frodo.internal.Counter) Action1(rx.functions.Action1) Observable(rx.Observable) Notification(rx.Notification) StopWatch(com.fernandocejas.frodo.internal.StopWatch)

Example 85 with Action1

use of rx.functions.Action1 in project xDrip by NightscoutFoundation.

the class ShareTest method attemptRead.

public void attemptRead() {
    final ReadDataShare readData = new ReadDataShare(this);
    final Action1<Long> systemTimeListener = new Action1<Long>() {

        @Override
        public void call(Long s) {
            Log.d(TAG, "Made the full round trip, got " + s + " as the system time");
            Log.d("SYSTTIME", "Made the full round trip, got " + s + " as the system time");
            final long addativeSystemTimeOffset = new Date().getTime() - s;
            Log.d(TAG, "Made the full round trip, got " + addativeSystemTimeOffset + " offset");
            Log.d("SYSTTIME", "Made the full round trip, got " + addativeSystemTimeOffset + " offset");
            final Action1<CalRecord[]> calRecordListener = new Action1<CalRecord[]>() {

                @Override
                public void call(CalRecord[] calRecords) {
                    Log.d(TAG, "Made the full round trip, got " + calRecords.length + " Cal Records");
                    Calibration.create(calRecords, addativeSystemTimeOffset, getApplicationContext());
                    final Action1<SensorRecord[]> sensorRecordListener = new Action1<SensorRecord[]>() {

                        @Override
                        public void call(SensorRecord[] sensorRecords) {
                            Log.d(TAG, "Made the full round trip, got " + sensorRecords.length + " Sensor Records");
                            BgReading.create(sensorRecords, addativeSystemTimeOffset, getApplicationContext());
                            final Action1<EGVRecord[]> evgRecordListener = new Action1<EGVRecord[]>() {

                                @Override
                                public void call(EGVRecord[] egvRecords) {
                                    Log.d(TAG, "Made the full round trip, got " + egvRecords.length + " EVG Records");
                                    BgReading.create(egvRecords, addativeSystemTimeOffset, getApplicationContext());
                                }
                            };
                            readData.getRecentEGVs(evgRecordListener);
                        }
                    };
                    readData.getRecentSensorRecords(sensorRecordListener);
                }
            };
            readData.getRecentCalRecords(calRecordListener);
        }
    };
    readData.readSystemTime(systemTimeListener);
}
Also used : Action1(rx.functions.Action1) CalRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.CalRecord) SensorRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.SensorRecord) EGVRecord(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.records.EGVRecord) ReadDataShare(com.eveningoutpost.dexdrip.ImportedLibraries.dexcom.ReadDataShare) Date(java.util.Date)

Aggregations

Action1 (rx.functions.Action1)106 Test (org.junit.Test)33 Action0 (rx.functions.Action0)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 UiThreadTest (android.support.test.annotation.UiThreadTest)20 Observable (rx.Observable)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)11 ArrayList (java.util.ArrayList)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 TestSubscriber (rx.observers.TestSubscriber)10 AllTypes (io.realm.entities.AllTypes)9 List (java.util.List)9 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 OnSubscribe (rx.Observable.OnSubscribe)7 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 IOException (java.io.IOException)6 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)5 Method (java.lang.reflect.Method)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5