|
37 | 37 | import io.envoyproxy.controlplane.cache.Watch;
|
38 | 38 | import io.envoyproxy.controlplane.cache.WatchCancelledException;
|
39 | 39 | import io.grpc.Status;
|
| 40 | +import io.grpc.StatusRuntimeException; |
40 | 41 | import io.grpc.stub.StreamObserver;
|
41 | 42 | import io.grpc.testing.GrpcServerRule;
|
| 43 | + |
| 44 | +import java.io.ByteArrayOutputStream; |
| 45 | +import java.io.PrintStream; |
42 | 46 | import java.util.Collection;
|
43 | 47 | import java.util.HashMap;
|
44 | 48 | import java.util.LinkedList;
|
@@ -748,6 +752,58 @@ public void onStreamCloseWithError(long streamId, String typeUrl, Throwable erro
|
748 | 752 | assertThat(callbacks.streamResponseCount).hasValue(0);
|
749 | 753 | }
|
750 | 754 |
|
| 755 | + @Test |
| 756 | + public void callbackOnError_logsError_onException() { |
| 757 | + MockConfigWatcher configWatcher = new MockConfigWatcher(false, createResponses()); |
| 758 | + DiscoveryServer server = new DiscoveryServer(configWatcher); |
| 759 | + |
| 760 | + AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase service = |
| 761 | + server.getAggregatedDiscoveryServiceImpl(); |
| 762 | + |
| 763 | + MockDiscoveryResponseObserver responseObserver = new MockDiscoveryResponseObserver(); |
| 764 | + StreamObserver<DiscoveryRequest> requestObserver = service.streamAggregatedResources(responseObserver); |
| 765 | + |
| 766 | + try { |
| 767 | + ByteArrayOutputStream stdErr = new ByteArrayOutputStream(); |
| 768 | + System.setErr(new PrintStream(stdErr)); |
| 769 | + |
| 770 | + requestObserver.onError(new StatusRuntimeException(Status.INTERNAL |
| 771 | + .withDescription("internal error") |
| 772 | + .withCause(new RuntimeException("some error")))); |
| 773 | + |
| 774 | + assertThat(stdErr.toString()).contains("ERROR "); |
| 775 | + assertThat(stdErr.toString()).contains("io.grpc.StatusRuntimeException: INTERNAL: internal error"); |
| 776 | + } finally { |
| 777 | + System.setErr(System.err); |
| 778 | + } |
| 779 | + } |
| 780 | + |
| 781 | + @Test |
| 782 | + public void callbackOnError_doesNotLogError_whenCancelled() { |
| 783 | + MockConfigWatcher configWatcher = new MockConfigWatcher(false, createResponses()); |
| 784 | + DiscoveryServer server = new DiscoveryServer(configWatcher); |
| 785 | + |
| 786 | + AggregatedDiscoveryServiceGrpc.AggregatedDiscoveryServiceImplBase service = |
| 787 | + server.getAggregatedDiscoveryServiceImpl(); |
| 788 | + |
| 789 | + MockDiscoveryResponseObserver responseObserver = new MockDiscoveryResponseObserver(); |
| 790 | + StreamObserver<DiscoveryRequest> requestObserver = service.streamAggregatedResources(responseObserver); |
| 791 | + |
| 792 | + try { |
| 793 | + ByteArrayOutputStream stdErr = new ByteArrayOutputStream(); |
| 794 | + System.setErr(new PrintStream(stdErr)); |
| 795 | + |
| 796 | + requestObserver.onError(new StatusRuntimeException(Status.CANCELLED |
| 797 | + .withDescription("internal error") |
| 798 | + .withCause(new RuntimeException("some error")))); |
| 799 | + |
| 800 | + assertThat(stdErr.toString()).doesNotContain("ERROR "); |
| 801 | + assertThat(stdErr.toString()).doesNotContain("io.grpc.StatusRuntimeException: CANCELLED:"); |
| 802 | + } finally { |
| 803 | + System.setErr(System.err); |
| 804 | + } |
| 805 | + } |
| 806 | + |
751 | 807 | private static Table<String, String, Collection<? extends Message>> createResponses() {
|
752 | 808 | return ImmutableTable.<String, String, Collection<? extends Message>>builder()
|
753 | 809 | .put(Resources.CLUSTER_TYPE_URL, VERSION, ImmutableList.of(CLUSTER))
|
|
0 commit comments