Search in sources :

Example 51 with Supplier

use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.

the class ObservableDelayTest method delayWithObservableSubscriptionNormal.

@Test
public void delayWithObservableSubscriptionNormal() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> delay = PublishSubject.create();
    Supplier<Observable<Integer>> subFunc = new Supplier<Observable<Integer>>() {

        @Override
        public Observable<Integer> get() {
            return delay;
        }
    };
    Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return delay;
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.delay(Observable.defer(subFunc), delayFunc).subscribe(o);
    source.onNext(1);
    delay.onNext(1);
    source.onNext(2);
    delay.onNext(2);
    inOrder.verify(o).onNext(2);
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onError(any(Throwable.class));
    verify(o, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) Observable(io.reactivex.rxjava3.core.Observable)

Example 52 with Supplier

use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.

the class ObservableDelayTest method delayWithObservableSubscriptionThrows.

@Test
public void delayWithObservableSubscriptionThrows() {
    PublishSubject<Integer> source = PublishSubject.create();
    final PublishSubject<Integer> delay = PublishSubject.create();
    Supplier<Observable<Integer>> subFunc = new Supplier<Observable<Integer>>() {

        @Override
        public Observable<Integer> get() {
            return delay;
        }
    };
    Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer t1) {
            return delay;
        }
    };
    Observer<Object> o = TestHelper.mockObserver();
    InOrder inOrder = inOrder(o);
    source.delay(Observable.defer(subFunc), delayFunc).subscribe(o);
    source.onNext(1);
    delay.onError(new TestException());
    source.onNext(2);
    inOrder.verify(o).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(o, never()).onNext(any());
    verify(o, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) Observable(io.reactivex.rxjava3.core.Observable)

Example 53 with Supplier

use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.

the class ObservableFromSupplierTest method shouldAllowToThrowCheckedException.

@Test
public void shouldAllowToThrowCheckedException() {
    final Exception checkedException = new Exception("test exception");
    Observable<Object> fromSupplierObservable = Observable.fromSupplier(new Supplier<Object>() {

        @Override
        public Object get() throws Exception {
            throw checkedException;
        }
    });
    Observer<Object> observer = TestHelper.mockObserver();
    fromSupplierObservable.subscribe(observer);
    verify(observer).onSubscribe(any(Disposable.class));
    verify(observer).onError(checkedException);
    verifyNoMoreInteractions(observer);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 54 with Supplier

use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.

the class ObservableGenerateTest method disposerThrows.

@Test
public void disposerThrows() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Observable.generate(new Supplier<Object>() {

            @Override
            public Object get() throws Exception {
                return 1;
            }
        }, new BiConsumer<Object, Emitter<Object>>() {

            @Override
            public void accept(Object s, Emitter<Object> e) throws Exception {
                e.onComplete();
            }
        }, new Consumer<Object>() {

            @Override
            public void accept(Object d) throws Exception {
                throw new TestException();
            }
        }).test().assertResult();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 55 with Supplier

use of io.reactivex.rxjava3.functions.Supplier in project bookkeeper by apache.

the class Main method buildBookieServer.

/**
 * Build the bookie server.
 *
 * <p>The sequence of the components is:
 *
 * <pre>
 * - stats provider
 * - bookie server
 * - autorecovery daemon
 * - http service
 * </pre>
 *
 * @param conf bookie server configuration
 * @return lifecycle stack
 */
public static LifecycleComponentStack buildBookieServer(BookieConfiguration conf) throws Exception {
    final ComponentInfoPublisher componentInfoPublisher = new ComponentInfoPublisher();
    final Supplier<BookieServiceInfo> bookieServiceInfoProvider = () -> buildBookieServiceInfo(componentInfoPublisher);
    LifecycleComponentStack.Builder serverBuilder = LifecycleComponentStack.newBuilder().withComponentInfoPublisher(componentInfoPublisher).withName("bookie-server");
    // 1. build stats provider
    StatsProviderService statsProviderService = new StatsProviderService(conf);
    StatsLogger rootStatsLogger = statsProviderService.getStatsProvider().getStatsLogger("");
    serverBuilder.addComponent(statsProviderService);
    log.info("Load lifecycle component : {}", StatsProviderService.class.getName());
    // 2. Build metadata driver
    MetadataBookieDriver metadataDriver = BookieResources.createMetadataDriver(conf.getServerConf(), rootStatsLogger);
    serverBuilder.addComponent(new AutoCloseableLifecycleComponent("metadataDriver", metadataDriver));
    RegistrationManager rm = metadataDriver.createRegistrationManager();
    serverBuilder.addComponent(new AutoCloseableLifecycleComponent("registrationManager", rm));
    // 3. Build ledger manager
    LedgerManagerFactory lmFactory = metadataDriver.getLedgerManagerFactory();
    serverBuilder.addComponent(new AutoCloseableLifecycleComponent("lmFactory", lmFactory));
    LedgerManager ledgerManager = lmFactory.newLedgerManager();
    serverBuilder.addComponent(new AutoCloseableLifecycleComponent("ledgerManager", ledgerManager));
    // 4. Build bookie
    StatsLogger bookieStats = rootStatsLogger.scope(BOOKIE_SCOPE);
    DiskChecker diskChecker = BookieResources.createDiskChecker(conf.getServerConf());
    LedgerDirsManager ledgerDirsManager = BookieResources.createLedgerDirsManager(conf.getServerConf(), diskChecker, bookieStats.scope(LD_LEDGER_SCOPE));
    LedgerDirsManager indexDirsManager = BookieResources.createIndexDirsManager(conf.getServerConf(), diskChecker, bookieStats.scope(LD_INDEX_SCOPE), ledgerDirsManager);
    ByteBufAllocatorWithOomHandler allocator = BookieResources.createAllocator(conf.getServerConf());
    UncleanShutdownDetection uncleanShutdownDetection = new UncleanShutdownDetectionImpl(ledgerDirsManager);
    if (uncleanShutdownDetection.lastShutdownWasUnclean()) {
        log.info("Unclean shutdown detected. The bookie did not register a graceful shutdown prior to this boot.");
    }
    // bookie takes ownership of storage, so shuts it down
    LedgerStorage storage = null;
    DataIntegrityCheck integCheck = null;
    if (conf.getServerConf().isDataIntegrityCheckingEnabled()) {
        StatsLogger clientStats = bookieStats.scope(CLIENT_SCOPE);
        ClientConfiguration clientConfiguration = new ClientConfiguration(conf.getServerConf());
        clientConfiguration.setClientRole(ClientConfiguration.CLIENT_ROLE_SYSTEM);
        BookKeeper bkc = BookKeeper.forConfig(clientConfiguration).statsLogger(clientStats).build();
        serverBuilder.addComponent(new AutoCloseableLifecycleComponent("bkc", bkc));
        BookieId bookieId = BookieImpl.getBookieId(conf.getServerConf());
        ExecutorService rxExecutor = Executors.newFixedThreadPool(2, new ThreadFactoryBuilder().setNameFormat("rx-schedule-%d").setUncaughtExceptionHandler((t, ex) -> log.error("Uncaught exception on thread {}", t.getName(), ex)).build());
        Scheduler rxScheduler = Schedulers.from(rxExecutor);
        serverBuilder.addComponent(new RxSchedulerLifecycleComponent("rx-scheduler", conf, bookieStats, rxScheduler, rxExecutor));
        storage = BookieResources.createLedgerStorage(conf.getServerConf(), ledgerManager, ledgerDirsManager, indexDirsManager, bookieStats, allocator);
        EntryCopier copier = new EntryCopierImpl(bookieId, ((org.apache.bookkeeper.client.BookKeeper) bkc).getClientCtx().getBookieClient(), storage, Ticker.systemTicker());
        integCheck = new DataIntegrityCheckImpl(bookieId, ledgerManager, storage, copier, new BookKeeperAdmin(bkc, clientStats, clientConfiguration), rxScheduler);
        // run the preboot check to protect against data loss and to perform data repair
        if (!conf.getServerConf().getJournalWriteData() && uncleanShutdownDetection.lastShutdownWasUnclean()) {
            integCheck.runPreBootCheck("UNCLEAN_SHUTDOWN");
        }
        CookieValidation cookieValidation = new DataIntegrityCookieValidation(conf.getServerConf(), rm, integCheck);
        cookieValidation.checkCookies(storageDirectoriesFromConf(conf.getServerConf()));
    } else {
        CookieValidation cookieValidation = new LegacyCookieValidation(conf.getServerConf(), rm);
        cookieValidation.checkCookies(storageDirectoriesFromConf(conf.getServerConf()));
        storage = BookieResources.createLedgerStorage(conf.getServerConf(), ledgerManager, ledgerDirsManager, indexDirsManager, bookieStats, allocator);
    }
    Bookie bookie;
    if (conf.getServerConf().isForceReadOnlyBookie()) {
        bookie = new ReadOnlyBookie(conf.getServerConf(), rm, storage, diskChecker, ledgerDirsManager, indexDirsManager, bookieStats, allocator, bookieServiceInfoProvider);
    } else {
        bookie = new BookieImpl(conf.getServerConf(), rm, storage, diskChecker, ledgerDirsManager, indexDirsManager, bookieStats, allocator, bookieServiceInfoProvider);
    }
    // 5. build bookie server
    BookieService bookieService = new BookieService(conf, bookie, rootStatsLogger, allocator, uncleanShutdownDetection);
    serverBuilder.addComponent(bookieService);
    log.info("Load lifecycle component : {}", BookieService.class.getName());
    if (conf.getServerConf().isLocalScrubEnabled()) {
        serverBuilder.addComponent(new ScrubberService(rootStatsLogger.scope(ScrubberStats.SCOPE), conf, bookieService.getServer().getBookie().getLedgerStorage()));
    }
    // 6. build auto recovery
    if (conf.getServerConf().isAutoRecoveryDaemonEnabled()) {
        AutoRecoveryService autoRecoveryService = new AutoRecoveryService(conf, rootStatsLogger.scope(REPLICATION_SCOPE));
        serverBuilder.addComponent(autoRecoveryService);
        log.info("Load lifecycle component : {}", AutoRecoveryService.class.getName());
    }
    // 7. build data integrity check service
    if (conf.getServerConf().isDataIntegrityCheckingEnabled()) {
        checkNotNull(integCheck, "integCheck should have been initialized with the cookie validation");
        DataIntegrityService dataIntegrityService = new DataIntegrityService(conf, rootStatsLogger.scope(REPLICATION_SCOPE), integCheck);
        serverBuilder.addComponent(dataIntegrityService);
        log.info("Load lifecycle component : {}", DataIntegrityService.class.getName());
    }
    // 8. build http service
    if (conf.getServerConf().isHttpServerEnabled()) {
        BKHttpServiceProvider provider = new BKHttpServiceProvider.Builder().setBookieServer(bookieService.getServer()).setServerConfiguration(conf.getServerConf()).setStatsProvider(statsProviderService.getStatsProvider()).setLedgerManagerFactory(metadataDriver.getLedgerManagerFactory()).build();
        HttpService httpService = new HttpService(provider, conf, rootStatsLogger);
        serverBuilder.addComponent(httpService);
        log.info("Load lifecycle component : {}", HttpService.class.getName());
    }
    // 9. build extra services
    String[] extraComponents = conf.getServerConf().getExtraServerComponents();
    if (null != extraComponents) {
        try {
            List<ServerLifecycleComponent> components = loadServerComponents(extraComponents, conf, rootStatsLogger);
            for (ServerLifecycleComponent component : components) {
                serverBuilder.addComponent(component);
                log.info("Load lifecycle component : {}", component.getClass().getName());
            }
        } catch (Exception e) {
            if (conf.getServerConf().getIgnoreExtraServerComponentsStartupFailures()) {
                log.info("Failed to load extra components '{}' - {}. Continuing without those components.", StringUtils.join(extraComponents), e.getMessage());
            } else {
                throw e;
            }
        }
    }
    return serverBuilder.build();
}
Also used : BookieId(org.apache.bookkeeper.net.BookieId) MetadataBookieDriver(org.apache.bookkeeper.meta.MetadataBookieDriver) DataIntegrityCheck(org.apache.bookkeeper.bookie.datainteg.DataIntegrityCheck) DiskChecker(org.apache.bookkeeper.util.DiskChecker) LegacyCookieValidation(org.apache.bookkeeper.bookie.LegacyCookieValidation) DataIntegrityCookieValidation(org.apache.bookkeeper.bookie.datainteg.DataIntegrityCookieValidation) CookieValidation(org.apache.bookkeeper.bookie.CookieValidation) ReadOnlyBookie(org.apache.bookkeeper.bookie.ReadOnlyBookie) Bookie(org.apache.bookkeeper.bookie.Bookie) ServerLifecycleComponent(org.apache.bookkeeper.server.component.ServerLifecycleComponent) EntryCopierImpl(org.apache.bookkeeper.bookie.datainteg.EntryCopierImpl) BookieService(org.apache.bookkeeper.server.service.BookieService) RegistrationManager(org.apache.bookkeeper.discover.RegistrationManager) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) ScrubberService(org.apache.bookkeeper.server.service.ScrubberService) ComponentInfoPublisher(org.apache.bookkeeper.common.component.ComponentInfoPublisher) LedgerManagerFactory(org.apache.bookkeeper.meta.LedgerManagerFactory) BKHttpServiceProvider(org.apache.bookkeeper.server.http.BKHttpServiceProvider) EntryCopier(org.apache.bookkeeper.bookie.datainteg.EntryCopier) LifecycleComponentStack(org.apache.bookkeeper.common.component.LifecycleComponentStack) StatsProviderService(org.apache.bookkeeper.server.service.StatsProviderService) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) LedgerStorage(org.apache.bookkeeper.bookie.LedgerStorage) LedgerManager(org.apache.bookkeeper.meta.LedgerManager) LedgerDirsManager(org.apache.bookkeeper.bookie.LedgerDirsManager) Scheduler(io.reactivex.rxjava3.core.Scheduler) UncleanShutdownDetection(org.apache.bookkeeper.bookie.UncleanShutdownDetection) ReadOnlyBookie(org.apache.bookkeeper.bookie.ReadOnlyBookie) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) BookKeeperAdmin(org.apache.bookkeeper.client.BookKeeperAdmin) DataIntegrityCookieValidation(org.apache.bookkeeper.bookie.datainteg.DataIntegrityCookieValidation) AutoCloseableLifecycleComponent(org.apache.bookkeeper.common.component.AutoCloseableLifecycleComponent) RxSchedulerLifecycleComponent(org.apache.bookkeeper.common.component.RxSchedulerLifecycleComponent) ByteBufAllocatorWithOomHandler(org.apache.bookkeeper.common.allocator.ByteBufAllocatorWithOomHandler) DataIntegrityService(org.apache.bookkeeper.bookie.datainteg.DataIntegrityService) BookKeeper(org.apache.bookkeeper.client.BookKeeper) UncheckedConfigurationException(org.apache.bookkeeper.conf.UncheckedConfigurationException) ParseException(org.apache.commons.cli.ParseException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DataIntegrityCheckImpl(org.apache.bookkeeper.bookie.datainteg.DataIntegrityCheckImpl) BookieImpl(org.apache.bookkeeper.bookie.BookieImpl) BookieServiceInfo(org.apache.bookkeeper.discover.BookieServiceInfo) HttpService(org.apache.bookkeeper.server.service.HttpService) ExecutorService(java.util.concurrent.ExecutorService) AutoRecoveryService(org.apache.bookkeeper.server.service.AutoRecoveryService) UncleanShutdownDetectionImpl(org.apache.bookkeeper.bookie.UncleanShutdownDetectionImpl) LegacyCookieValidation(org.apache.bookkeeper.bookie.LegacyCookieValidation)

Aggregations

Test (org.junit.Test)37 TestException (io.reactivex.rxjava3.exceptions.TestException)33 Observable (io.reactivex.rxjava3.core.Observable)13 IOException (java.io.IOException)9 InOrder (org.mockito.InOrder)8 java.util (java.util)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Disposable (io.reactivex.rxjava3.disposables.Disposable)5 Supplier (io.reactivex.rxjava3.functions.Supplier)5 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)5 TestObserver (io.reactivex.rxjava3.observers.TestObserver)5 Observer (io.reactivex.rxjava3.core.Observer)4 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)4 ImmutableList (com.google.common.collect.ImmutableList)3 io.reactivex.rxjava3.processors (io.reactivex.rxjava3.processors)3 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)3 java.util.function (java.util.function)3 java.util.stream (java.util.stream)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 SQLUtils (com.alibaba.druid.sql.SQLUtils)2