### Question:
PersistBlockTask extends AbstractEthTask<Block> { public static PersistBlockTask create( final ProtocolSchedule protocolSchedule, final ProtocolContext protocolContext, final EthContext ethContext, final Block block, final HeaderValidationMode headerValidationMode, final MetricsSystem metricsSystem) { return new PersistBlockTask( protocolSchedule, protocolContext, ethContext, block, headerValidationMode, metricsSystem); } private  PersistBlockTask(
      final ProtocolSchedule protocolSchedule,
      final ProtocolContext protocolContext,
      final EthContext ethContext,
      final Block block,
      final HeaderValidationMode headerValidationMode,
      final MetricsSystem metricsSystem); static PersistBlockTask create(
      final ProtocolSchedule protocolSchedule,
      final ProtocolContext protocolContext,
      final EthContext ethContext,
      final Block block,
      final HeaderValidationMode headerValidationMode,
      final MetricsSystem metricsSystem); static Supplier<CompletableFuture<List<Block>>> forSequentialBlocks(
      final ProtocolSchedule protocolSchedule,
      final ProtocolContext protocolContext,
      final EthContext ethContext,
      final List<Block> blocks,
      final HeaderValidationMode headerValidationMode,
      final MetricsSystem metricsSystem); static Supplier<CompletableFuture<List<Block>>> forUnorderedBlocks(
      final ProtocolSchedule protocolSchedule,
      final ProtocolContext protocolContext,
      final EthContext ethContext,
      final List<Block> blocks,
      final HeaderValidationMode headerValidationMode,
      final MetricsSystem metricsSystem);  }### Answer:
@Test public void importsValidBlock() throws Exception { blockchainUtil.importFirstBlocks(3); final Block nextBlock = blockchainUtil.getBlock(3); assertThat(blockchain.contains(nextBlock.getHash())).isFalse(); final PersistBlockTask task = PersistBlockTask.create( protocolSchedule, protocolContext, ethContext, nextBlock, HeaderValidationMode.FULL, metricsSystem); final CompletableFuture<Block> result = task.run(); Awaitility.await().atMost(30, SECONDS).until(result::isDone); assertThat(result.isCompletedExceptionally()).isFalse(); assertThat(result.get()).isEqualTo(nextBlock); assertThat(blockchain.contains(nextBlock.getHash())).isTrue(); }
@Test public void failsToImportInvalidBlock() { final BlockDataGenerator gen = new BlockDataGenerator(); blockchainUtil.importFirstBlocks(3); final Block nextBlock = gen.block(); assertThat(blockchain.contains(nextBlock.getHash())).isFalse(); final PersistBlockTask task = PersistBlockTask.create( protocolSchedule, protocolContext, ethContext, nextBlock, HeaderValidationMode.FULL, metricsSystem); final CompletableFuture<Block> result = task.run(); Awaitility.await().atMost(30, SECONDS).until(result::isDone); assertThat(result.isCompletedExceptionally()).isTrue(); assertThatThrownBy(result::get).hasCauseInstanceOf(InvalidBlockException.class); assertThat(blockchain.contains(nextBlock.getHash())).isFalse(); }
@Test public void cancelBeforeRunning() { blockchainUtil.importFirstBlocks(3); final Block nextBlock = blockchainUtil.getBlock(3); assertThat(blockchain.contains(nextBlock.getHash())).isFalse(); final PersistBlockTask task = PersistBlockTask.create( protocolSchedule, protocolContext, ethContext, nextBlock, HeaderValidationMode.FULL, metricsSystem); task.cancel(); final CompletableFuture<Block> result = task.run(); assertThat(result.isCancelled()).isTrue(); assertThat(blockchain.contains(nextBlock.getHash())).isFalse(); }
@Test public void cancelAfterRunning() { blockchainUtil.importFirstBlocks(3); final Block nextBlock = blockchainUtil.getBlock(3); assertThat(blockchain.contains(nextBlock.getHash())).isFalse(); final PersistBlockTask task = PersistBlockTask.create( protocolSchedule, protocolContext, ethContext, nextBlock, HeaderValidationMode.FULL, metricsSystem); final PersistBlockTask taskSpy = Mockito.spy(task); Mockito.doNothing().when(taskSpy).executeTaskTimed(); final CompletableFuture<Block> result = taskSpy.run(); taskSpy.cancel(); assertThat(result.isCancelled()).isTrue(); assertThat(blockchain.contains(nextBlock.getHash())).isFalse(); }