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();
}
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();
}
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);
}
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();
}
}
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();
}
Aggregations