### Question:
WaitForPeerTask extends AbstractEthTask<Void> { public static WaitForPeerTask create( final EthContext ethContext, final MetricsSystem metricsSystem) { return new WaitForPeerTask(ethContext, metricsSystem); } private  WaitForPeerTask(final EthContext ethContext, final MetricsSystem metricsSystem); static WaitForPeerTask create(
      final EthContext ethContext, final MetricsSystem metricsSystem);  }### Answer:
@Test public void completesWhenPeerConnects() throws ExecutionException, InterruptedException { final AtomicBoolean successful = new AtomicBoolean(false); final EthTask<Void> task = WaitForPeerTask.create(ethContext, metricsSystem); final CompletableFuture<Void> future = task.run(); future.whenComplete( (result, error) -> { if (error == null) { successful.compareAndSet(false, true); } }); EthProtocolManagerTestUtil.createPeer(ethProtocolManager); assertThat(successful).isTrue(); }
@Test public void doesNotCompleteWhenNoPeerConnects() throws ExecutionException, InterruptedException { final AtomicBoolean successful = new AtomicBoolean(false); final EthTask<Void> task = WaitForPeerTask.create(ethContext, metricsSystem); final CompletableFuture<Void> future = task.run(); future.whenComplete( (result, error) -> { if (error == null) { successful.compareAndSet(false, true); } }); assertThat(successful).isFalse(); }
@Test public void cancel() throws ExecutionException, InterruptedException { final EthTask<Void> task = WaitForPeerTask.create(ethContext, metricsSystem); final CompletableFuture<Void> future = task.run(); assertThat(future.isDone()).isFalse(); task.cancel(); assertThat(future.isDone()).isTrue(); assertThat(future.isCancelled()).isTrue(); assertThat(task.run().isCancelled()).isTrue(); }