@SuppressWarnings("unchecked") @Test public void testNewManaged() throws Exception { final Completable<Object> startFuture = mock(Completable.class); final Completable<Object> zeroLeaseFuture = mock(Completable.class); final Completable<Object> stopReferenceFuture = mock(Completable.class); final Completable<Object> stopFuture = mock(Completable.class); when(async.completable()) .thenReturn(startFuture) .thenReturn(zeroLeaseFuture) .thenReturn(stopReferenceFuture); final AtomicReference<Function<Object, Stage<Object>>> transform1 = new AtomicReference<>(); doAnswer(new Answer<Stage<Object>>() { @Override public Stage<Object> answer(InvocationOnMock invocation) throws Throwable { transform1.set(invocation.getArgumentAt(0, Function.class)); return stopFuture; } }).when(zeroLeaseFuture).thenCompose((Function<Object, Stage<Object>>) any(Function.class)); final AtomicReference<Function<Object, Stage<Object>>> transform2 = new AtomicReference<>(); doAnswer(new Answer<Stage<Object>>() { @Override public Stage<Object> answer(InvocationOnMock invocation) throws Throwable { transform2.set(invocation.getArgumentAt(0, Function.class)); return stopFuture; } }).when(stopReferenceFuture).thenCompose((Function<Object, Stage<Object>>) any(Function.class)); ConcurrentManaged.newManaged(async, caller, ManagedOptions.newDefault(), setup, teardown); verify(async, times(3)).completable(); verify(zeroLeaseFuture).thenCompose((Function<Object, Stage<Object>>) any(Function.class)); verify(teardown, never()).apply(reference); verify(stopReferenceFuture, never()).thenCompose( (Function<Object, Stage<Object>>) any(Function.class)); transform1.get().apply(null); verify(stopReferenceFuture).thenCompose((Function<Object, Stage<Object>>) any(Function.class)); transform2.get().apply(reference); verify(teardown).apply(reference); }
public static <T> ConcurrentManaged<T> newManaged( final Async async, final Caller caller, final ManagedOptions options, final Supplier<? extends Stage<T>> setup, final Function<? super T, ? extends Stage<Void>> teardown ) { final Completable<Void> startFuture = async.completable(); final Completable<Void> zeroLeaseFuture = async.completable(); final Completable<T> stopReferenceFuture = async.completable(); final Stage<Void> stopFuture = zeroLeaseFuture.thenCompose(v -> stopReferenceFuture.thenCompose(teardown)); return new ConcurrentManaged<>(caller, options, setup, startFuture, zeroLeaseFuture, stopReferenceFuture, stopFuture); }
ConcurrentManaged implements Managed<T> { public static <T> ConcurrentManaged<T> newManaged( final Async async, final Caller caller, final ManagedOptions options, final Supplier<? extends Stage<T>> setup, final Function<? super T, ? extends Stage<Void>> teardown ) { final Completable<Void> startFuture = async.completable(); final Completable<Void> zeroLeaseFuture = async.completable(); final Completable<T> stopReferenceFuture = async.completable(); final Stage<Void> stopFuture = zeroLeaseFuture.thenCompose(v -> stopReferenceFuture.thenCompose(teardown)); return new ConcurrentManaged<>(caller, options, setup, startFuture, zeroLeaseFuture, stopReferenceFuture, stopFuture); } }
ConcurrentManaged implements Managed<T> { public static <T> ConcurrentManaged<T> newManaged( final Async async, final Caller caller, final ManagedOptions options, final Supplier<? extends Stage<T>> setup, final Function<? super T, ? extends Stage<Void>> teardown ) { final Completable<Void> startFuture = async.completable(); final Completable<Void> zeroLeaseFuture = async.completable(); final Completable<T> stopReferenceFuture = async.completable(); final Stage<Void> stopFuture = zeroLeaseFuture.thenCompose(v -> stopReferenceFuture.thenCompose(teardown)); return new ConcurrentManaged<>(caller, options, setup, startFuture, zeroLeaseFuture, stopReferenceFuture, stopFuture); } ConcurrentManaged(
      final Caller caller, final ManagedOptions options, final Supplier<? extends Stage<T>> setup,
      final Completable<Void> startFuture, final Completable<Void> zeroLeaseFuture,
      final Completable<T> stopReferenceFuture, final Stage<Void> stopFuture
  ); }
ConcurrentManaged implements Managed<T> { public static <T> ConcurrentManaged<T> newManaged( final Async async, final Caller caller, final ManagedOptions options, final Supplier<? extends Stage<T>> setup, final Function<? super T, ? extends Stage<Void>> teardown ) { final Completable<Void> startFuture = async.completable(); final Completable<Void> zeroLeaseFuture = async.completable(); final Completable<T> stopReferenceFuture = async.completable(); final Stage<Void> stopFuture = zeroLeaseFuture.thenCompose(v -> stopReferenceFuture.thenCompose(teardown)); return new ConcurrentManaged<>(caller, options, setup, startFuture, zeroLeaseFuture, stopReferenceFuture, stopFuture); } ConcurrentManaged(
      final Caller caller, final ManagedOptions options, final Supplier<? extends Stage<T>> setup,
      final Completable<Void> startFuture, final Completable<Void> zeroLeaseFuture,
      final Completable<T> stopReferenceFuture, final Stage<Void> stopFuture
  ); static ConcurrentManaged<T> newManaged(
      final Async async, final Caller caller, final ManagedOptions options,
      final Supplier<? extends Stage<T>> setup,
      final Function<? super T, ? extends Stage<Void>> teardown
  ); @Override Stage<R> doto(
      final Function<? super T, ? extends Stage<R>> action
  ); @Override Borrowed<T> borrow(); @Override boolean isReady(); @Override Stage<Void> start(); @Override Stage<Void> stop(); @Override String toString(); }
ConcurrentManaged implements Managed<T> { public static <T> ConcurrentManaged<T> newManaged( final Async async, final Caller caller, final ManagedOptions options, final Supplier<? extends Stage<T>> setup, final Function<? super T, ? extends Stage<Void>> teardown ) { final Completable<Void> startFuture = async.completable(); final Completable<Void> zeroLeaseFuture = async.completable(); final Completable<T> stopReferenceFuture = async.completable(); final Stage<Void> stopFuture = zeroLeaseFuture.thenCompose(v -> stopReferenceFuture.thenCompose(teardown)); return new ConcurrentManaged<>(caller, options, setup, startFuture, zeroLeaseFuture, stopReferenceFuture, stopFuture); } ConcurrentManaged(
      final Caller caller, final ManagedOptions options, final Supplier<? extends Stage<T>> setup,
      final Completable<Void> startFuture, final Completable<Void> zeroLeaseFuture,
      final Completable<T> stopReferenceFuture, final Stage<Void> stopFuture
  ); static ConcurrentManaged<T> newManaged(
      final Async async, final Caller caller, final ManagedOptions options,
      final Supplier<? extends Stage<T>> setup,
      final Function<? super T, ? extends Stage<Void>> teardown
  ); @Override Stage<R> doto(
      final Function<? super T, ? extends Stage<R>> action
  ); @Override Borrowed<T> borrow(); @Override boolean isReady(); @Override Stage<Void> start(); @Override Stage<Void> stop(); @Override String toString();  }