Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

static method StreamObservers.nextAndComplete() #11778

Merged
merged 5 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions stub/src/main/java/io/grpc/stub/StreamObservers.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@
/**
* Utility functions for working with {@link StreamObserver} and it's common subclasses like
* {@link CallStreamObserver}.
*
* @deprecated Of questionable utility and generally not used.
*/
@Deprecated
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4694")
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
* accounting for outbound flow-control. After calling this method, {@code target} should no
Expand All @@ -40,7 +49,10 @@ public final class StreamObservers {
*
* @param source of values expressed as an {@link Iterator}.
* @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,
final CallStreamObserver<V> target) {
Preconditions.checkNotNull(source, "source");
Expand Down Expand Up @@ -80,7 +92,10 @@ public void run() {
*
* @param source of values expressed as an {@link Iterable}.
* @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,
CallStreamObserver<V> target) {
Preconditions.checkNotNull(source, "source");
Expand Down
35 changes: 35 additions & 0 deletions stub/src/test/java/io/grpc/stub/StreamObserversTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}