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

feat: add stream method for ServerStream #1575

Merged
merged 10 commits into from
Apr 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import com.google.api.core.InternalApi;
import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javax.annotation.Nonnull;

/**
Expand Down Expand Up @@ -89,6 +91,15 @@ public Iterator<V> iterator() {
return iterator;
}

/**
* Returns a sequential {@code Stream} with server responses as its source.
*
* @return a sequential {@code Stream} over the elements in server responses
*/
public Stream<V> stream() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add Javadoc? All public methods are required to have Javadoc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return StreamSupport.stream(this.spliterator(), false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the second argument is set to false, which means this is a sequential stream, do we want to expose another parallelStream method as well?

Copy link
Collaborator Author

@JoeWang1127 JoeWang1127 Mar 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether a deterministic result is required as parallel stream can randomize the result.

Also, we could explore more about parallel stream later as we did in Page.

}

/**
* Returns true if the next call to the iterator's hasNext() or next() is guaranteed to be
* nonblocking.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -109,6 +110,31 @@ public List<Integer> call() {
Truth.assertThat(results).containsExactly(0, 1, 2, 3, 4);
}

@Test
public void testMultipleItemStreamMethod() throws Exception {
Future<Void> producerFuture =
executor.submit(
() -> {
for (int i = 0; i < 5; i++) {
int requestCount = controller.popLastPull();

Truth.assertWithMessage("ServerStream should request one item at a time")
.that(requestCount)
.isEqualTo(1);

stream.observer().onResponse(i);
}
stream.observer().onComplete();
return null;
});
Future<List<Integer>> consumerFuture =
executor.submit(() -> stream.stream().collect(Collectors.toList()));

producerFuture.get(60, TimeUnit.SECONDS);
List<Integer> results = consumerFuture.get();
Truth.assertThat(results).containsExactly(0, 1, 2, 3, 4);
}

@Test
public void testEarlyTermination() throws Exception {
Future<Void> taskFuture =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
Expand Down Expand Up @@ -71,6 +72,18 @@ public void testGrpc_receiveStreamedContent() {
.inOrder();
}

@Test
public void testGrpc_receiveStreamedContentStreamAPI() {
String content = "The rain in Spain stays mainly on the plain!";
ServerStream<EchoResponse> responseStream =
grpcClient.expandCallable().call(ExpandRequest.newBuilder().setContent(content).build());
assertThat(responseStream.stream().map(EchoResponse::getContent).collect(Collectors.toList()))
.containsExactlyElementsIn(
ImmutableList.of(
"The", "rain", "in", "Spain", "stays", "mainly", "on", "the", "plain!"))
.inOrder();
}

@Test
public void testGrpc_serverError_receiveErrorAfterLastWordInStream() {
String content = "The rain in Spain";
Expand Down