diff options
author | ZHANG Dapeng <zdapeng@google.com> | 2017-11-01 09:37:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-01 09:37:22 -0700 |
commit | d6ff4c259f373e9643b3e51185158f7030a33c91 (patch) | |
tree | 543fb8909ddc0a5ce8abd08495dfae8057996f4a /testing | |
parent | 27ae09522d69f0754c15311c527f190a56c1cc09 (diff) | |
download | grpc-grpc-java-d6ff4c259f373e9643b3e51185158f7030a33c91.tar.gz |
testing: move io.grpc.testing.StreamRecorder to internal
Diffstat (limited to 'testing')
-rw-r--r-- | testing/src/main/java/io/grpc/internal/testing/StreamRecorder.java | 113 | ||||
-rw-r--r-- | testing/src/main/java/io/grpc/testing/StreamRecorder.java | 3 |
2 files changed, 116 insertions, 0 deletions
diff --git a/testing/src/main/java/io/grpc/internal/testing/StreamRecorder.java b/testing/src/main/java/io/grpc/internal/testing/StreamRecorder.java new file mode 100644 index 000000000..4c4d07cd6 --- /dev/null +++ b/testing/src/main/java/io/grpc/internal/testing/StreamRecorder.java @@ -0,0 +1,113 @@ +/* + * Copyright 2014, gRPC Authors All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.grpc.internal.testing; + +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import javax.annotation.Nullable; + +/** + * Utility implementation of {@link StreamObserver} used in testing. Records all the observed + * values produced by the stream as well as any errors. + */ +public class StreamRecorder<T> implements StreamObserver<T> { + + /** + * Creates a new recorder. + */ + public static <T> StreamRecorder<T> create() { + return new StreamRecorder<T>(); + } + + private final CountDownLatch latch; + private final List<T> results; + private Throwable error; + private final SettableFuture<T> firstValue; + + private StreamRecorder() { + firstValue = SettableFuture.create(); + latch = new CountDownLatch(1); + results = Collections.synchronizedList(new ArrayList<T>()); + } + + @Override + public void onNext(T value) { + if (!firstValue.isDone()) { + firstValue.set(value); + } + results.add(value); + } + + @Override + public void onError(Throwable t) { + if (!firstValue.isDone()) { + firstValue.setException(t); + } + error = t; + latch.countDown(); + } + + @Override + public void onCompleted() { + if (!firstValue.isDone()) { + firstValue.setException(new IllegalStateException("No first value provided")); + } + latch.countDown(); + } + + /** + * Waits for the stream to terminate. + */ + public void awaitCompletion() throws Exception { + latch.await(); + } + + /** + * Waits a fixed timeout for the stream to terminate. + */ + public boolean awaitCompletion(int timeout, TimeUnit unit) throws Exception { + return latch.await(timeout, unit); + } + + /** + * Returns the current set of received values. + */ + public List<T> getValues() { + return Collections.unmodifiableList(results); + } + + /** + * Returns the stream terminating error. + */ + @Nullable public Throwable getError() { + return error; + } + + /** + * Returns a {@link ListenableFuture} for the first value received from the stream. Useful + * for testing unary call patterns. + */ + public ListenableFuture<T> firstValue() { + return firstValue; + } +} diff --git a/testing/src/main/java/io/grpc/testing/StreamRecorder.java b/testing/src/main/java/io/grpc/testing/StreamRecorder.java index ca382f0ab..9e5df2526 100644 --- a/testing/src/main/java/io/grpc/testing/StreamRecorder.java +++ b/testing/src/main/java/io/grpc/testing/StreamRecorder.java @@ -30,7 +30,10 @@ import javax.annotation.Nullable; /** * Utility implementation of {@link StreamObserver} used in testing. Records all the observed * values produced by the stream as well as any errors. + * + * @deprecated Not for public use */ +@Deprecated @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1791") public class StreamRecorder<T> implements StreamObserver<T> { |