Skip to content
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 @@ -52,6 +52,7 @@ class GrpcStreamIterator extends AbstractIterator<PartialResultSet>
private TimeUnit streamWaitTimeoutUnit;
private long streamWaitTimeoutValue;
private SpannerException error;
private boolean done;

@VisibleForTesting
GrpcStreamIterator(int prefetchChunks, boolean cancelQueryWhenClientIsClosed) {
Expand Down Expand Up @@ -166,11 +167,17 @@ private class ConsumerImpl implements SpannerRpc.ResultStreamConsumer {
@Override
public void onPartialResultSet(PartialResultSet results) {
addToStream(results);
if (results.getLast()) {
done = true;
addToStream(END_OF_STREAM);
}
}

@Override
public void onCompleted() {
addToStream(END_OF_STREAM);
if (!done) {
addToStream(END_OF_STREAM);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.testing.SerializableTester.reserialize;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -1115,4 +1116,58 @@ public void getProtoEnumList() {
resultSet.getProtoEnum(0, Genre::forNumber);
});
}

@Test
public void verifyResultSetWithLastTrue() {
long[] longArray = {111, 333, 444, 0, -1, -2234, Long.MAX_VALUE, Long.MIN_VALUE};

consumer.onPartialResultSet(
PartialResultSet.newBuilder()
.setMetadata(
makeMetadata(Type.struct(Type.StructField.of("f", Type.array(Type.int64())))))
.addValues(Value.int64Array(longArray).toProto())
.setLast(false)
.build());
assertTrue(resultSet.next());
consumer.onPartialResultSet(
PartialResultSet.newBuilder()
.setMetadata(
makeMetadata(Type.struct(Type.StructField.of("f", Type.array(Type.int64())))))
.addValues(Value.int64Array(longArray).toProto())
.setLast(true)
.build());
assertTrue(resultSet.next());
assertFalse(resultSet.next());
consumer.onCompleted();
}

@Test
public void shouldThrowDeadlineExceededIfLastTrueIsNotReceived() {
long[] longArray = {111, 333, 444, 0, -1, -2234, Long.MAX_VALUE, Long.MIN_VALUE};

consumer.onPartialResultSet(
PartialResultSet.newBuilder()
.setMetadata(
makeMetadata(Type.struct(Type.StructField.of("f", Type.array(Type.int64())))))
.addValues(Value.int64Array(longArray).toProto())
.setLast(false)
.build());
assertTrue(resultSet.next());
consumer.onPartialResultSet(
PartialResultSet.newBuilder()
.setMetadata(
makeMetadata(Type.struct(Type.StructField.of("f", Type.array(Type.int64())))))
.addValues(Value.int64Array(longArray).toProto())
.setLast(false)
.build());
assertTrue(resultSet.next());
SpannerException spannerException =
assertThrows(
SpannerException.class,
() -> {
assertThat(resultSet.next()).isFalse();
Copy link
Collaborator

Choose a reason for hiding this comment

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

How long does this take to execute?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

});
assertEquals("DEADLINE_EXCEEDED: stream wait timeout", spannerException.getMessage());
consumer.onCompleted();
}
}
Loading