@Test public void testStartCancel() throws Exception { setupStart(true, null, true, false); assertEquals(startFuture, underTest.start()); assertEquals(null, underTest.reference.get()); verify(startFuture, never()).fail(e); verify(startFuture, never()).complete(null); verify(startFuture).cancel(); }
@Override public Stage<Void> start() { if (!state.compareAndSet(ManagedState.INITIALIZED, ManagedState.STARTED)) { return startFuture; } final Stage<T> constructor; try { constructor = setup.get(); } catch (final Exception e) { return new ImmediateFailed<>(caller, e); } if (constructor == null) { throw new NullPointerException("setup returned null as stage"); } return constructor.<Void>thenApply(result -> { if (result == null) { throw new IllegalArgumentException("setup reference must be non-null"); } reference.set(result); return null; }).handle(new Handle<Void>() { @Override public void failed(final Throwable cause) { startFuture.fail(cause); } @Override public void completed(Void result) { startFuture.complete(null); } @Override public void cancelled() { startFuture.cancel(); } }); }
ConcurrentManaged implements Managed<T> { @Override public Stage<Void> start() { if (!state.compareAndSet(ManagedState.INITIALIZED, ManagedState.STARTED)) { return startFuture; } final Stage<T> constructor; try { constructor = setup.get(); } catch (final Exception e) { return new ImmediateFailed<>(caller, e); } if (constructor == null) { throw new NullPointerException("setup returned null as stage"); } return constructor.<Void>thenApply(result -> { if (result == null) { throw new IllegalArgumentException("setup reference must be non-null"); } reference.set(result); return null; }).handle(new Handle<Void>() { @Override public void failed(final Throwable cause) { startFuture.fail(cause); } @Override public void completed(Void result) { startFuture.complete(null); } @Override public void cancelled() { startFuture.cancel(); } }); } }
ConcurrentManaged implements Managed<T> { @Override public Stage<Void> start() { if (!state.compareAndSet(ManagedState.INITIALIZED, ManagedState.STARTED)) { return startFuture; } final Stage<T> constructor; try { constructor = setup.get(); } catch (final Exception e) { return new ImmediateFailed<>(caller, e); } if (constructor == null) { throw new NullPointerException("setup returned null as stage"); } return constructor.<Void>thenApply(result -> { if (result == null) { throw new IllegalArgumentException("setup reference must be non-null"); } reference.set(result); return null; }).handle(new Handle<Void>() { @Override public void failed(final Throwable cause) { startFuture.fail(cause); } @Override public void completed(Void result) { startFuture.complete(null); } @Override public void cancelled() { startFuture.cancel(); } }); } 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> { @Override public Stage<Void> start() { if (!state.compareAndSet(ManagedState.INITIALIZED, ManagedState.STARTED)) { return startFuture; } final Stage<T> constructor; try { constructor = setup.get(); } catch (final Exception e) { return new ImmediateFailed<>(caller, e); } if (constructor == null) { throw new NullPointerException("setup returned null as stage"); } return constructor.<Void>thenApply(result -> { if (result == null) { throw new IllegalArgumentException("setup reference must be non-null"); } reference.set(result); return null; }).handle(new Handle<Void>() { @Override public void failed(final Throwable cause) { startFuture.fail(cause); } @Override public void completed(Void result) { startFuture.complete(null); } @Override public void cancelled() { startFuture.cancel(); } }); } 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> { @Override public Stage<Void> start() { if (!state.compareAndSet(ManagedState.INITIALIZED, ManagedState.STARTED)) { return startFuture; } final Stage<T> constructor; try { constructor = setup.get(); } catch (final Exception e) { return new ImmediateFailed<>(caller, e); } if (constructor == null) { throw new NullPointerException("setup returned null as stage"); } return constructor.<Void>thenApply(result -> { if (result == null) { throw new IllegalArgumentException("setup reference must be non-null"); } reference.set(result); return null; }).handle(new Handle<Void>() { @Override public void failed(final Throwable cause) { startFuture.fail(cause); } @Override public void completed(Void result) { startFuture.complete(null); } @Override public void cancelled() { startFuture.cancel(); } }); } 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();  }