Search in sources :

Example 46 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.

the class ObservableConcatTest method concatNonBlockingObservables.

@Test
public void concatNonBlockingObservables() {
    final CountDownLatch okToContinueW1 = new CountDownLatch(1);
    final CountDownLatch okToContinueW2 = new CountDownLatch(1);
    final TestObservable<String> w1 = new TestObservable<>(null, okToContinueW1, "one", "two", "three");
    final TestObservable<String> w2 = new TestObservable<>(null, okToContinueW2, "four", "five", "six");
    Observer<String> observer = TestHelper.mockObserver();
    Observable<Observable<String>> observableOfObservables = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {

        @Override
        public void subscribe(Observer<? super Observable<String>> observer) {
            observer.onSubscribe(Disposable.empty());
            // simulate what would happen in an Observable
            observer.onNext(Observable.unsafeCreate(w1));
            observer.onNext(Observable.unsafeCreate(w2));
            observer.onComplete();
        }
    });
    Observable<String> concat = Observable.concat(observableOfObservables);
    concat.subscribe(observer);
    verify(observer, times(0)).onComplete();
    try {
        // release both threads
        okToContinueW1.countDown();
        okToContinueW2.countDown();
        // wait for both to finish
        w1.t.join();
        w2.t.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("one");
    inOrder.verify(observer, times(1)).onNext("two");
    inOrder.verify(observer, times(1)).onNext("three");
    inOrder.verify(observer, times(1)).onNext("four");
    inOrder.verify(observer, times(1)).onNext("five");
    inOrder.verify(observer, times(1)).onNext("six");
    verify(observer, times(1)).onComplete();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable) Test(org.junit.Test)

Example 47 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project RxJava by ReactiveX.

the class ObservableConcatMapEagerTest method concatArrayEager.

@SuppressWarnings("unchecked")
@Test
public void concatArrayEager() throws Exception {
    for (int i = 2; i < 10; i++) {
        Observable<Integer>[] obs = new Observable[i];
        Arrays.fill(obs, Observable.just(1));
        Integer[] expected = new Integer[i];
        Arrays.fill(expected, 1);
        Method m = Observable.class.getMethod("concatArrayEager", ObservableSource[].class);
        TestObserver<Integer> to = TestObserver.create();
        ((Observable<Integer>) m.invoke(null, new Object[] { obs })).subscribe(to);
        to.assertValues(expected);
        to.assertNoErrors();
        to.assertComplete();
    }
}
Also used : Method(java.lang.reflect.Method) Observable(io.reactivex.rxjava3.core.Observable)

Example 48 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project AgileDev by LZ9.

the class DrawerTestActivity method refreshTitle.

/**
 * 刷新称号
 */
private void refreshTitle() {
    int random = new Random().nextInt(TITLES.length);
    Observable.just(TITLES[random]).flatMap(new Function<String, ObservableSource<String>>() {

        @Override
        public ObservableSource<String> apply(String s) throws Exception {
            Thread.sleep(1000);
            return Observable.just(s);
        }
    }).compose(RxUtils.<String>ioToMainObservable()).compose(this.<String>bindUntilEvent(ActivityEvent.DESTROY)).subscribe(new ProgressObserver<String>() {

        @Override
        public void onPgNext(String title) {
            StringBuilder stringBuilder = new StringBuilder();
            mTitleTv.setText(stringBuilder.append("称号:").append(title));
            ToastUtils.showShort(getContext(), "已为您刷新称号");
        }

        @Override
        public void onPgError(Throwable e, boolean isNetwork) {
            ToastUtils.showShort(getContext(), "刷新失败");
        }
    }.create(getContext(), "正在为您刷新称号", false));
}
Also used : Function(io.reactivex.rxjava3.functions.Function) ProgressObserver(com.lodz.android.component.rx.subscribe.observer.ProgressObserver) Random(java.util.Random)

Example 49 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project quran_android by quran.

the class AudioManagerUtils method shuyookhDownloadObservable.

@NonNull
public static Single<List<QariDownloadInfo>> shuyookhDownloadObservable(QuranInfo quranInfo, String basePath, List<QariItem> qariItems) {
    return Observable.fromIterable(qariItems).flatMap((Function<QariItem, ObservableSource<QariDownloadInfo>>) item -> {
        QariDownloadInfo cached = cache.get(item);
        if (cached != null) {
            return Observable.just(cached);
        }
        File baseFile = new File(basePath, item.getPath());
        return !baseFile.exists() ? Observable.just(new QariDownloadInfo(item)) : item.isGapless() ? getGaplessSheikhObservable(baseFile, item).toObservable() : getGappedSheikhObservable(quranInfo, baseFile, item).toObservable();
    }).doOnNext(qariDownloadInfo -> cache.put(qariDownloadInfo.qariItem, qariDownloadInfo)).toList().subscribeOn(Schedulers.io());
}
Also used : Function(io.reactivex.rxjava3.functions.Function) File(java.io.File) NonNull(androidx.annotation.NonNull)

Example 50 with ObservableSource

use of io.reactivex.rxjava3.core.ObservableSource in project Mycat2 by MyCATApache.

the class GroupByKeyPlan method execute.

@Override
public Observable<VectorSchemaRoot> execute(RootContext rootContext) {
    List<Field> fields = schema().getFields();
    InnerType[] innerTypes = schema().getFields().stream().map(i -> InnerType.from(i.getType())).toArray(n -> new InnerType[n]);
    Map map = MapFactory.createMap(innerTypes);
    RecordSink[] recordSinks = new RecordSink[groupByKeys.length];
    int groupIndex = 0;
    for (GroupKeys groupByKey : groupByKeys) {
        IntInnerType[] intPairs = new IntInnerType[groupByKey.getKeys().length];
        int[] keys = groupByKey.getKeys();
        int index = 0;
        for (int key : keys) {
            Field field = fields.get(key);
            intPairs[index] = IntInnerType.of(index, InnerType.from(field.getType()));
            index++;
        }
        recordSinks[groupIndex] = RecordSinkFactory.INSTANCE.buildRecordSink(intPairs);
        groupIndex++;
    }
    return inputPlan.execute(rootContext).flatMap(new Function<VectorSchemaRoot, ObservableSource<? extends VectorSchemaRoot>>() {

        @Override
        public ObservableSource<? extends VectorSchemaRoot> apply(VectorSchemaRoot input) throws Throwable {
            int rowCount = input.getRowCount();
            VectorBatchRecord record = new VectorBatchRecord(input);
            VectorSchemaRoot output = rootContext.getVectorSchemaRoot(schema, rowCount * recordSinks.length);
            int outputRowId = 0;
            for (int i = 0; i < recordSinks.length; i++) {
                RecordSink recordSink = recordSinks[i];
                for (int rowId = 0; rowId < rowCount; rowId++) {
                    record.setPosition(rowId);
                    MapKey key = map.withKey();
                    RecordSetter recordSinkSPI = RecordSinkFactory.INSTANCE.getRecordSinkSPI(key);
                    recordSink.copy(record, recordSinkSPI);
                    if (key.create()) {
                        recordSink.copy(record, outputRowId, output);
                        outputRowId++;
                    // output
                    } else {
                    // skip
                    }
                }
            }
            if (outputRowId == 0) {
                output.close();
                return Observable.empty();
            }
            output.setRowCount(outputRowId);
            inputPlan.eachFree(input);
            return Observable.fromArray(output);
        }
    }).doOnComplete(new Action() {

        @Override
        public void run() throws Throwable {
            map.close();
        }
    });
}
Also used : MapFactory(io.ordinate.engine.structure.MapFactory) ObservableSource(io.reactivex.rxjava3.core.ObservableSource) Schema(org.apache.arrow.vector.types.pojo.Schema) InnerType(io.ordinate.engine.schema.InnerType) Logger(org.slf4j.Logger) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) LoggerFactory(org.slf4j.LoggerFactory) RecordSinkFactory(io.ordinate.engine.record.RecordSinkFactory) RecordSetter(io.ordinate.engine.record.RecordSetter) Field(org.apache.arrow.vector.types.pojo.Field) RootContext(io.ordinate.engine.record.RootContext) MapKey(io.questdb.cairo.map.MapKey) Action(io.reactivex.rxjava3.functions.Action) Map(io.questdb.cairo.map.Map) GroupKeys(io.ordinate.engine.builder.GroupKeys) List(java.util.List) IntInnerType(io.ordinate.engine.schema.IntInnerType) Function(io.reactivex.rxjava3.functions.Function) Observable(io.reactivex.rxjava3.core.Observable) VectorBatchRecord(io.ordinate.engine.record.VectorBatchRecord) Collections(java.util.Collections) RecordSink(io.ordinate.engine.record.RecordSink) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) RecordSetter(io.ordinate.engine.record.RecordSetter) Action(io.reactivex.rxjava3.functions.Action) InnerType(io.ordinate.engine.schema.InnerType) IntInnerType(io.ordinate.engine.schema.IntInnerType) IntInnerType(io.ordinate.engine.schema.IntInnerType) Field(org.apache.arrow.vector.types.pojo.Field) Function(io.reactivex.rxjava3.functions.Function) MapKey(io.questdb.cairo.map.MapKey) GroupKeys(io.ordinate.engine.builder.GroupKeys) RecordSink(io.ordinate.engine.record.RecordSink) Map(io.questdb.cairo.map.Map) VectorBatchRecord(io.ordinate.engine.record.VectorBatchRecord)

Aggregations

Test (org.junit.Test)21 Observable (io.reactivex.rxjava3.core.Observable)20 TestException (io.reactivex.rxjava3.exceptions.TestException)12 InOrder (org.mockito.InOrder)12 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 IOException (java.io.IOException)8 Observer (io.reactivex.rxjava3.core.Observer)5 RxMethod (io.reactivex.rxjava3.validators.BaseTypeParser.RxMethod)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ObservableSource (io.reactivex.rxjava3.core.ObservableSource)4 Function (io.reactivex.rxjava3.functions.Function)4 Intent (android.content.Intent)3 Disposable (io.reactivex.rxjava3.disposables.Disposable)3 File (java.io.File)3 View (android.view.View)2 RxPermissionTransformer (com.xxf.permission.transformer.RxPermissionTransformer)2 Supplier (io.reactivex.rxjava3.functions.Supplier)2 GroupedObservable (io.reactivex.rxjava3.observables.GroupedObservable)2 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2