PromiseNotifier implements GenericFutureListener<F> { @Override public void operationComplete(F future) throws Exception { if (future.isSuccess()) { V result = future.get(); for (Promise<? super V> p: promises) { if (!p.trySuccess(result)) { logger.warn("Failed to mark a promise as success because it is done already: {}", p); } } return; } Throwable cause = future.cause(); for (Promise<? super V> p: promises) { if (!p.tryFailure(cause)) { logger.warn("Failed to mark a promise as failure because it's done already: {}", p, cause); } } } @SafeVarargs  PromiseNotifier(Promise<? super V>... promises); @Override void operationComplete(F future);  }
@Test public void testListenerSuccess() throws Exception { @SuppressWarnings("unchecked") Promise<Void> p1 = createStrictMock(Promise.class); @SuppressWarnings("unchecked") Promise<Void> p2 = createStrictMock(Promise.class); @SuppressWarnings("unchecked") PromiseNotifier<Void, Future<Void>> notifier = new PromiseNotifier<Void, Future<Void>>(p1, p2); @SuppressWarnings("unchecked") Future<Void> future = createStrictMock(Future.class); expect(future.isSuccess()).andReturn(true); expect(future.get()).andReturn(null); expect(p1.trySuccess(null)).andReturn(true); expect(p2.trySuccess(null)).andReturn(true); replay(p1, p2, future); notifier.operationComplete(future); verify(p1, p2); }
@Test public void testListenerFailure() throws Exception { @SuppressWarnings("unchecked") Promise<Void> p1 = createStrictMock(Promise.class); @SuppressWarnings("unchecked") Promise<Void> p2 = createStrictMock(Promise.class); @SuppressWarnings("unchecked") PromiseNotifier<Void, Future<Void>> notifier = new PromiseNotifier<Void, Future<Void>>(p1, p2); @SuppressWarnings("unchecked") Future<Void> future = createStrictMock(Future.class); Throwable t = createStrictMock(Throwable.class); expect(future.isSuccess()).andReturn(false); expect(future.cause()).andReturn(t); expect(p1.tryFailure(t)).andReturn(true); expect(p2.tryFailure(t)).andReturn(true); replay(p1, p2, future); notifier.operationComplete(future); verify(p1, p2); }