PromiseNotifier implements GenericFutureListener<F> { @Override public void operationComplete(F future) throws Exception { InternalLogger internalLogger = logNotifyFailure ? logger : null; if (future.isSuccess()) { V result = future.get(); for (Promise<? super V> p: promises) { PromiseNotificationUtil.trySuccess(p, result, internalLogger); } } else if (future.isCancelled()) { for (Promise<? super V> p: promises) { PromiseNotificationUtil.tryCancel(p, internalLogger); } } else { Throwable cause = future.cause(); for (Promise<? super V> p: promises) { PromiseNotificationUtil.tryFailure(p, cause, internalLogger); } } } @SafeVarargs  PromiseNotifier(Promise<? super V>... promises); @SafeVarargs  PromiseNotifier(boolean logNotifyFailure, Promise<? super V>... promises); @Override void operationComplete(F future);  }
@Test public void testListenerSuccess() throws Exception { @SuppressWarnings("unchecked") Promise<Void> p1 = mock(Promise.class); @SuppressWarnings("unchecked") Promise<Void> p2 = mock(Promise.class); @SuppressWarnings("unchecked") PromiseNotifier<Void, Future<Void>> notifier = new PromiseNotifier<Void, Future<Void>>(p1, p2); @SuppressWarnings("unchecked") Future<Void> future = mock(Future.class); when(future.isSuccess()).thenReturn(true); when(future.get()).thenReturn(null); when(p1.trySuccess(null)).thenReturn(true); when(p2.trySuccess(null)).thenReturn(true); notifier.operationComplete(future); verify(p1).trySuccess(null); verify(p2).trySuccess(null); }
@Test public void testListenerFailure() throws Exception { @SuppressWarnings("unchecked") Promise<Void> p1 = mock(Promise.class); @SuppressWarnings("unchecked") Promise<Void> p2 = mock(Promise.class); @SuppressWarnings("unchecked") PromiseNotifier<Void, Future<Void>> notifier = new PromiseNotifier<Void, Future<Void>>(p1, p2); @SuppressWarnings("unchecked") Future<Void> future = mock(Future.class); Throwable t = mock(Throwable.class); when(future.isSuccess()).thenReturn(false); when(future.isCancelled()).thenReturn(false); when(future.cause()).thenReturn(t); when(p1.tryFailure(t)).thenReturn(true); when(p2.tryFailure(t)).thenReturn(true); notifier.operationComplete(future); verify(p1).tryFailure(t); verify(p2).tryFailure(t); }