mirror of https://github.com/grpc/grpc-java.git
stub: Utility method StreamObservers.nextAndComplete() that does both onNext and onComplete (#11778)
This commit is contained in:
parent
5ca4d852ae
commit
edc2bf7346
|
@ -23,12 +23,21 @@ import java.util.Iterator;
|
||||||
/**
|
/**
|
||||||
* Utility functions for working with {@link StreamObserver} and it's common subclasses like
|
* Utility functions for working with {@link StreamObserver} and it's common subclasses like
|
||||||
* {@link CallStreamObserver}.
|
* {@link CallStreamObserver}.
|
||||||
*
|
|
||||||
* @deprecated Of questionable utility and generally not used.
|
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
|
||||||
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4694")
|
|
||||||
public final class StreamObservers {
|
public final class StreamObservers {
|
||||||
|
// Prevent instantiation
|
||||||
|
private StreamObservers() { }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility method to call {@link StreamObserver#onNext(Object)} and
|
||||||
|
* {@link StreamObserver#onCompleted()} on the specified responseObserver.
|
||||||
|
*/
|
||||||
|
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/10957")
|
||||||
|
public static <T> void nextAndComplete(StreamObserver<T> responseObserver, T response) {
|
||||||
|
responseObserver.onNext(response);
|
||||||
|
responseObserver.onCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy the values of an {@link Iterator} to the target {@link CallStreamObserver} while properly
|
* Copy the values of an {@link Iterator} to the target {@link CallStreamObserver} while properly
|
||||||
* accounting for outbound flow-control. After calling this method, {@code target} should no
|
* accounting for outbound flow-control. After calling this method, {@code target} should no
|
||||||
|
@ -40,7 +49,10 @@ public final class StreamObservers {
|
||||||
*
|
*
|
||||||
* @param source of values expressed as an {@link Iterator}.
|
* @param source of values expressed as an {@link Iterator}.
|
||||||
* @param target {@link CallStreamObserver} which accepts values from the source.
|
* @param target {@link CallStreamObserver} which accepts values from the source.
|
||||||
|
* @deprecated Of questionable utility and generally not used.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4694")
|
||||||
public static <V> void copyWithFlowControl(final Iterator<V> source,
|
public static <V> void copyWithFlowControl(final Iterator<V> source,
|
||||||
final CallStreamObserver<V> target) {
|
final CallStreamObserver<V> target) {
|
||||||
Preconditions.checkNotNull(source, "source");
|
Preconditions.checkNotNull(source, "source");
|
||||||
|
@ -80,7 +92,10 @@ public final class StreamObservers {
|
||||||
*
|
*
|
||||||
* @param source of values expressed as an {@link Iterable}.
|
* @param source of values expressed as an {@link Iterable}.
|
||||||
* @param target {@link CallStreamObserver} which accepts values from the source.
|
* @param target {@link CallStreamObserver} which accepts values from the source.
|
||||||
|
* @deprecated Of questionable utility and generally not used.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4694")
|
||||||
public static <V> void copyWithFlowControl(final Iterable<V> source,
|
public static <V> void copyWithFlowControl(final Iterable<V> source,
|
||||||
CallStreamObserver<V> target) {
|
CallStreamObserver<V> target) {
|
||||||
Preconditions.checkNotNull(source, "source");
|
Preconditions.checkNotNull(source, "source");
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 The gRPC Authors
|
||||||
|
*
|
||||||
|
* 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.stub;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.InOrder;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
public class StreamObserversTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nextAndComplete() {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
StreamObserver<String> observer = Mockito.mock(StreamObserver.class);
|
||||||
|
InOrder inOrder = Mockito.inOrder(observer);
|
||||||
|
StreamObservers.nextAndComplete(observer, "TEST");
|
||||||
|
inOrder.verify(observer).onNext("TEST");
|
||||||
|
inOrder.verify(observer).onCompleted();
|
||||||
|
inOrder.verifyNoMoreInteractions();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue