/** * Adds the current non-null OnSchedule hook to this handler and replaces it * in RxJavaPlugins with this OnScheduleMultiHandlerManager. */ public void append() { @SuppressWarnings("unchecked") Function<Runnable, Runnable> existing = (Function<Runnable, Runnable>)RxJavaPlugins.getScheduleHandler(); if (existing != this) { if (existing != null) { register(existing); } RxJavaPlugins.setScheduleHandler(this); } }
/** * Adds the current non-null OnSchedule hook to this handler and replaces it * in RxJavaPlugins with this OnScheduleMultiHandlerManager. */ public void append() { @SuppressWarnings("unchecked") Function<Runnable, Runnable> existing = (Function<Runnable, Runnable>)RxJavaPlugins.getScheduleHandler(); if (existing != this) { if (existing != null) { register(existing); } RxJavaPlugins.setScheduleHandler(this); } }
@Test public void forEachConsumerRace() { for (int i = 0; i < 1000; i++) { final Disposable d = manager.register(Functions.<Runnable>identity()); assertTrue(manager.hasHandlers()); Runnable r1 = new Runnable() { @Override public void run() { d.dispose(); } }; Runnable r2 = new Runnable() { @Override public void run() { manager.forEach(Functions.<Function<Runnable, Runnable>>emptyConsumer()); } }; TestHelper.race(r1, r2, Schedulers.single()); assertFalse(manager.hasHandlers()); } }
@Test public void forEachBiConsumerRace() { for (int i = 0; i < 1000; i++) { final Disposable d = manager.register(Functions.<Runnable>identity());
@Test public void append() { try { manager.append(); manager.disable(); manager.enable(); manager.append(); manager.disable(); assertNull(RxJavaPlugins.getScheduleHandler()); RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() { @Override public Runnable apply(Runnable r) throws Exception { calls++; return r; } }); manager.append(); manager.register(new Function<Runnable, Runnable>() { @Override public Runnable apply(Runnable r) throws Exception { calls++; return r; } }); RxJavaPlugins.onSchedule(Functions.EMPTY_RUNNABLE); assertEquals(2, calls); } finally { RxJavaPlugins.reset(); } }