private Reactor getReactor() { if (reactor == null) { final Timer timer = Timer.timer(); //getTimer(); //Todo this should work return Reactor.reactor(timeoutDuration, timer::now); } return reactor; }
/** * Creates a QBit callback based on promise created. * * @param promiseConsumer promise consumer * @param <T> T * @return QBit callback */ public <T> Callback<T> callback(final Consumer<Promise<T>> promiseConsumer) { Promise<T> promise = reactor.promise(); promiseConsumer.accept(promise); return Reakt.convertPromise(promise); }
public void process() { time = timer.time(); reactor.process(); processHandler.ifPresent(Runnable::run); stats.clientProxyFlush(); healthServiceClient.ifPresent(ClientProxy::clientProxyFlush); servicesToFlush.forEach((service) -> { try { ServiceProxyUtils.flushServiceProxy(service); } catch (Exception ex) { logger.error("Unable to flush service on behalf of service " + serviceName, ex); } }); }
@Test public void utilityMethod() { Promise promise; promise = reactor.promiseNotify(); assertTrue(promise instanceof ReplayPromise); promise = reactor.promise(Employee.class); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseString(); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseBoolean(); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseLong(); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseInt(); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseFloat(); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseDouble(); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseList(Employee.class); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseSet(Employee.class); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseCollection(Employee.class); assertTrue(promise instanceof ReplayPromise); promise = reactor.promiseMap(String.class, Employee.class); assertTrue(promise instanceof ReplayPromise); }
@Test public void testTimeout() { final AtomicBoolean errorFound = new AtomicBoolean(); final AtomicBoolean thenCalled = new AtomicBoolean(); final Promise<Object> promise = reactor.promise(Duration.ofSeconds(1)) .catchError(error -> errorFound.set(true)) .then(object -> thenCalled.set(true)); reactor.process(); //Force a timeout. assertTrue(!errorFound.get()); //No errors yet. testTimer.setTime(System.currentTimeMillis() + Duration.ofSeconds(30).toMillis()); reactor.process(); assertTrue(errorFound.get()); //Now we should have an error assertTrue(!thenCalled.get()); //Then should not be called promise.asHandler().resolve(new Object()); reactor.process(); assertTrue(!thenCalled.get()); //Then should still not be called }
@Test public void testAllList() { AtomicBoolean thenCalled = new AtomicBoolean(); Promise<String> promise1 = reactor.promise(); Promise<String> promise2 = reactor.promise(); final Promise<Void> promise = reactor.all(Arrays.asList(promise1, promise2)); promise.then(s -> thenCalled.set(true)); assertFalse(promise.asHandler().complete()); reactor.process(); assertFalse(promise.asHandler().complete()); /** Invoke the callback. */ promise1.asHandler().resolve("DONE"); promise2.asHandler().resolve("DONE"); reactor.process(); //play it /** You can see the results but the events wont fire until reactor plays the replay promises. */ assertTrue(promise.asHandler().complete()); assertTrue(promise.asHandler().success()); /** Now we see the event. */ assertTrue(thenCalled.get()); }
@Test public void testAnyList() { AtomicBoolean thenCalled = new AtomicBoolean(); Promise<String> promise1 = reactor.promise(); Promise<String> promise2 = reactor.promise(); final Promise<Void> promise = reactor.any(Arrays.asList(promise1, promise2)); promise.then(s -> thenCalled.set(true)); assertFalse(promise.asHandler().complete()); reactor.process(); assertFalse(promise.asHandler().complete()); /** Invoke the callback. */ promise1.asHandler().resolve("DONE"); promise2.asHandler().resolve("DONE"); reactor.process(); //play it /** You can see the results but the events wont fire until reactor plays the replay promises. */ assertTrue(promise.asHandler().complete()); assertTrue(promise.asHandler().success()); /** Now we see the event. */ assertTrue(thenCalled.get()); }
@Test public void testAllListInvokeable() throws InterruptedException { final AtomicInteger thenCalled = new AtomicInteger(); final List<Promise<String>> promiseList = new ArrayList<>(); for (int index = 0; index < 100; index++) { promiseList.add(invokeMe()); } final Promise<Void> promise = reactor.all(promiseList); promise.then(s -> { thenCalled.incrementAndGet(); }).catchError(Throwable::printStackTrace); assertFalse(promise.asHandler().complete()); reactor.process(); assertFalse(promise.asHandler().complete()); process(); promiseList.stream().forEach(Promise::invoke); for (int index = 0; index < 1000; index++) { Thread.sleep(50); if (promise.asHandler().complete()) break; } /** You can see the results but the events wont fire until reactor plays the replay promises. */ assertTrue(promise.asHandler().complete()); assertTrue(promise.asHandler().success()); Thread.sleep(50); /** Now we see the event. */ assertEquals(1, thenCalled.get()); }
@Test public void testRepeatingTask() { AtomicLong count = new AtomicLong(); reactor.addRepeatingTask(Duration.ofSeconds(1), count::incrementAndGet); assertEquals(0, count.get()); reactor.process(); assertEquals(1, count.get()); testTimer.setTime(System.currentTimeMillis() + Duration.ofMillis(1001).toMillis()); reactor.process(); assertEquals(2, count.get()); }
@Test public void testOneShotTask() { AtomicLong count = new AtomicLong(); reactor.runTaskAfter(Duration.ofSeconds(1), count::incrementAndGet); assertEquals(0, count.get()); reactor.process(); assertEquals(0, count.get()); testTimer.setTime(System.currentTimeMillis() + Duration.ofMillis(1001).toMillis()); reactor.process(); assertEquals(1, count.get()); testTimer.setTime(System.currentTimeMillis() + Duration.ofMillis(2002).toMillis()); reactor.process(); assertEquals(1, count.get()); }
@Test public void testDeferTask() { AtomicLong count = new AtomicLong(); reactor.deferRun(count::incrementAndGet); assertEquals(0, count.get()); reactor.process(); assertEquals(1, count.get()); testTimer.setTime(System.currentTimeMillis() + Duration.ofMillis(1001).toMillis()); reactor.process(); assertEquals(1, count.get()); testTimer.setTime(System.currentTimeMillis() + Duration.ofMillis(2002).toMillis()); reactor.process(); assertEquals(1, count.get()); }
public TodoManagerImpl(Reactor reactor, StatsCollector statsCollector) { this.reactor = reactor; this.statsCollector = statsCollector; /** Send stat count i.am.alive every three seconds. */ this.reactor.addRepeatingTask(Duration.ofSeconds(3), () -> statsCollector.increment("todoservice.i.am.alive")); this.reactor.addRepeatingTask(Duration.ofSeconds(1), statsCollector::clientProxyFlush); }
@Test public void normalNullCall() { final AtomicBoolean errorFound = new AtomicBoolean(); final AtomicBoolean thenCalled = new AtomicBoolean(); final Promise<Object> promise = reactor.promise(Duration.ofSeconds(1)) .catchError(error -> errorFound.set(true)) .then(object -> thenCalled.set(true)); reactor.process(); assertTrue(!errorFound.get()); //No errors. assertTrue(!thenCalled.get()); //Then should still not be called promise.asHandler().resolve(null); reactor.process(); assertTrue(!errorFound.get()); //No errors. assertTrue(thenCalled.get()); //Then should be called }
@Test public void testAll() { AtomicInteger thenCalled = new AtomicInteger(); Promise<String> promise1 = reactor.promise(); Promise<String> promise2 = reactor.promise(); final Promise<Void> promise = reactor.all(promise1, promise2); promise.then(s -> thenCalled.incrementAndGet()); assertFalse(promise.asHandler().complete()); reactor.process(); assertFalse(promise.asHandler().complete()); /** Invoke the callback. */ promise1.asHandler().resolve("DONE"); promise2.asHandler().resolve("DONE"); reactor.process(); //play it /** You can see the results but the events wont fire until reactor plays the replay promises. */ assertTrue(promise.asHandler().complete()); assertTrue(promise.asHandler().success()); /** Now we see the event. */ assertEquals(1, thenCalled.get()); }
@Test public void testAny() { AtomicBoolean thenCalled = new AtomicBoolean(); Promise<String> promise1 = reactor.promise(); Promise<String> promise2 = reactor.promise(); final Promise<Void> promise = reactor.any(promise1, promise2); promise.then(s -> thenCalled.set(true)); assertFalse(promise.asHandler().complete()); reactor.process(); assertFalse(promise.asHandler().complete()); /** Invoke the callback. */ promise1.asHandler().resolve("DONE"); promise2.asHandler().resolve("DONE"); reactor.process(); //play it /** You can see the results but the events wont fire until reactor plays the replay promises. */ assertTrue(promise.asHandler().complete()); assertTrue(promise.asHandler().success()); /** Now we see the event. */ assertTrue(thenCalled.get()); }
@Test public void testAllList2() throws InterruptedException { final AtomicInteger thenCalled = new AtomicInteger(); final List<Promise<String>> promiseList = new ArrayList<>(); for (int index = 0; index < 100; index++) { promiseList.add(Promises.promiseString()); } final Promise<Void> promise = reactor.all(promiseList).catchError(Throwable::printStackTrace); promise.then(s -> thenCalled.incrementAndGet()); assertFalse(promise.asHandler().complete()); reactor.process(); assertFalse(promise.asHandler().complete()); process(); for (Promise<String> stringPromise : promiseList) { new Thread(() -> { stringPromise.asHandler().resolve("DONE"); }).start(); } for (int index = 0; index < 100; index++) { Thread.sleep(50); if (promise.asHandler().complete()) break; } /** You can see the results but the events wont fire until reactor plays the replay promises. */ assertTrue(promise.asHandler().complete()); assertTrue(promise.asHandler().success()); /** Now we see the event. */ assertEquals(1, thenCalled.get()); }
/** * Creates a default reactor with timeout. * * @param timeout timeout * @return a reactor */ static Reactor reactor(final Duration timeout) { return reactor(timeout, System::currentTimeMillis); }
@Test public void normalCall() { final AtomicBoolean errorFound = new AtomicBoolean(); final AtomicBoolean thenCalled = new AtomicBoolean(); final Promise<Object> promise = reactor.promise(Duration.ofSeconds(1)) .catchError(error -> errorFound.set(true)) .then(object -> thenCalled.set(true)); reactor.process(); assertTrue(!errorFound.get()); //No errors. assertTrue(!thenCalled.get()); //Then should still not be called promise.asHandler().resolve(new Object()); reactor.process(); assertTrue(!errorFound.get()); //No errors. assertTrue(thenCalled.get()); //Then should be called }
@Override public PromiseHandler<T> invokeWithReactor(final Reactor reactor, Duration timeout) { final BasePromise<T> reactorPromise = (BasePromise<T>) reactor.promise(timeout); copyPromiseFieldsToReactorPromise(reactorPromise); return this; }