### Question:
GrpcRootRangeService extends RootRangeServiceImplBase { @Override public void deleteStream(DeleteStreamRequest request, StreamObserver<DeleteStreamResponse> responseObserver) { rs.deleteStream(request).whenComplete( new ResponseHandler<DeleteStreamResponse>(responseObserver) { @Override protected DeleteStreamResponse createErrorResp(Throwable cause) { return DeleteStreamResponse.newBuilder() .setCode(StatusCode.INTERNAL_SERVER_ERROR) .build(); } }); } GrpcRootRangeService(RangeStoreService rs); @Override void createNamespace(CreateNamespaceRequest request,
                                StreamObserver<CreateNamespaceResponse> responseObserver); @Override void deleteNamespace(DeleteNamespaceRequest request,
                                StreamObserver<DeleteNamespaceResponse> responseObserver); @Override void getNamespace(GetNamespaceRequest request,
                             StreamObserver<GetNamespaceResponse> responseObserver); @Override void createStream(CreateStreamRequest request,
                             StreamObserver<CreateStreamResponse> responseObserver); @Override void deleteStream(DeleteStreamRequest request,
                             StreamObserver<DeleteStreamResponse> responseObserver); @Override void getStream(GetStreamRequest request,
                          StreamObserver<GetStreamResponse> responseObserver);  }### Answer:
@Test public void testDeleteStreamSuccess() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService); DeleteStreamResponse deleteResp = DeleteStreamResponse.newBuilder() .setCode(StatusCode.SUCCESS) .build(); DeleteStreamRequest deleteReq = createDeleteStreamRequest(nsName, streamName); when(rangeService.deleteStream(deleteReq)).thenReturn( CompletableFuture.completedFuture(deleteResp)); AtomicReference<DeleteStreamResponse> resultHolder = new AtomicReference<>(); AtomicReference<Throwable> exceptionHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); StreamObserver<DeleteStreamResponse> streamObserver = new StreamObserver<DeleteStreamResponse>() { @Override public void onNext(DeleteStreamResponse resp) { resultHolder.set(resp); } @Override public void onError(Throwable t) { exceptionHolder.set(t); latch.countDown(); } @Override public void onCompleted() { latch.countDown(); } }; grpcService.deleteStream( DeleteStreamRequest.newBuilder() .setNsName(nsName) .setName(streamName) .build(), streamObserver); latch.await(); assertNull(exceptionHolder.get()); assertNotNull(resultHolder.get()); assertTrue(deleteResp == resultHolder.get()); verify(rangeService, times(1)).deleteStream(deleteReq); }
@Test public void testDeleteStreamFailure() throws Exception { RangeStoreService rangeService = mock(RangeStoreService.class); GrpcRootRangeService grpcService = new GrpcRootRangeService(rangeService); DeleteStreamResponse deleteResp = DeleteStreamResponse.newBuilder() .setCode(StatusCode.INTERNAL_SERVER_ERROR) .build(); DeleteStreamRequest deleteReq = createDeleteStreamRequest(nsName, streamName); when(rangeService.deleteStream(deleteReq)).thenReturn( FutureUtils.exception(CAUSE)); AtomicReference<DeleteStreamResponse> resultHolder = new AtomicReference<>(); AtomicReference<Throwable> exceptionHolder = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); StreamObserver<DeleteStreamResponse> streamObserver = new StreamObserver<DeleteStreamResponse>() { @Override public void onNext(DeleteStreamResponse resp) { resultHolder.set(resp); } @Override public void onError(Throwable t) { exceptionHolder.set(t); latch.countDown(); } @Override public void onCompleted() { latch.countDown(); } }; grpcService.deleteStream( DeleteStreamRequest.newBuilder() .setNsName(nsName) .setName(streamName) .build(), streamObserver); latch.await(); assertNull(exceptionHolder.get()); assertNotNull(resultHolder.get()); assertEquals(deleteResp, resultHolder.get()); verify(rangeService, times(1)).deleteStream(deleteReq); }