Skip to content

Commit abd6627

Browse files
authored
feat: add isUserClosed to indicate that user explicitly closed the StreamWriter (#1983)
To be used by the sample here: https://togithub.com/googleapis/java-bigquerystorage/pull/1982/files Also change the name of isDone to be isClosed, since we use StreamWriterClosedException to indicate a writer is shutdown for writing. Making the terms more consistent with each other.
1 parent cd1dc60 commit abd6627

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

google-cloud-bigquerystorage/clirr-ignored-differences.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,15 @@
147147
<className>com/google/cloud/bigquery/storage/v1/ConnectionWorkerPool</className>
148148
<method>ConnectionWorkerPool(long, long, java.time.Duration, com.google.api.gax.batching.FlowController$LimitExceededBehavior, java.lang.String, com.google.cloud.bigquery.storage.v1.BigQueryWriteSettings)</method>
149149
</difference>
150+
<difference>
151+
<differenceType>7002</differenceType>
152+
<className>com/google/cloud/bigquery/storage/v1/StreamWriter</className>
153+
<method>boolean isDone()</method>
154+
</difference>
155+
<difference>
156+
<differenceType>7002</differenceType>
157+
<className>com/google/cloud/bigquery/storage/v1/JsonStreamWriter</className>
158+
<method>boolean isDone()</method>
159+
</difference>
150160
</differences>
161+

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,13 @@ public void close() {
387387
* JsonStreamWriter is explicitly closed or the underlying connection is broken when
388388
* connection pool is not used. Client should recreate JsonStreamWriter in this case.
389389
*/
390-
public boolean isDone() {
391-
return this.streamWriter.isDone();
390+
public boolean isClosed() {
391+
return this.streamWriter.isClosed();
392+
}
393+
394+
/** @return if user explicitly closed the writer. */
395+
public boolean isUserClosed() {
396+
return this.streamWriter.isUserClosed();
392397
}
393398

394399
public static final class Builder {

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/StreamWriter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public String getLocation() {
425425
* StreamWriter is explicitly closed or the underlying connection is broken when connection
426426
* pool is not used. Client should recreate StreamWriter in this case.
427427
*/
428-
public boolean isDone() {
428+
public boolean isClosed() {
429429
if (singleConnectionOrConnectionPool.getKind() == Kind.CONNECTION_WORKER) {
430430
return userClosed.get()
431431
|| singleConnectionOrConnectionPool.connectionWorker().isConnectionInUnrecoverableState();
@@ -435,6 +435,11 @@ public boolean isDone() {
435435
}
436436
}
437437

438+
/** @return if user explicitly closed the writer. */
439+
public boolean isUserClosed() {
440+
return userClosed.get();
441+
}
442+
438443
/** Close the stream writer. Shut down all resources. */
439444
@Override
440445
public void close() {

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriterTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,10 +1141,11 @@ public void testWriterId()
11411141

11421142
@Test
11431143
public void testIsDone() throws DescriptorValidationException, IOException, InterruptedException {
1144-
JsonStreamWriter writer1 = getTestJsonStreamWriterBuilder(TEST_STREAM, TABLE_SCHEMA).build();
1145-
Assert.assertFalse(writer1.isDone());
1146-
writer1.close();
1147-
Assert.assertTrue(writer1.isDone());
1144+
JsonStreamWriter writer = getTestJsonStreamWriterBuilder(TEST_STREAM, TABLE_SCHEMA).build();
1145+
Assert.assertFalse(writer.isClosed());
1146+
writer.close();
1147+
Assert.assertTrue(writer.isClosed());
1148+
Assert.assertTrue(writer.isUserClosed());
11481149
}
11491150

11501151
private AppendRowsResponse createAppendResponse(long offset) {

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/StreamWriterTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ public void testStreamWriterUserCloseMultiplexing() throws Exception {
12371237
.build();
12381238

12391239
writer.close();
1240-
assertTrue(writer.isDone());
1240+
assertTrue(writer.isClosed());
12411241
ApiFuture<AppendRowsResponse> appendFuture1 = sendTestMessage(writer, new String[] {"A"});
12421242
ExecutionException ex =
12431243
assertThrows(
@@ -1248,6 +1248,7 @@ public void testStreamWriterUserCloseMultiplexing() throws Exception {
12481248
assertEquals(
12491249
Status.Code.FAILED_PRECONDITION,
12501250
((StatusRuntimeException) ex.getCause()).getStatus().getCode());
1251+
assertTrue(writer.isUserClosed());
12511252
}
12521253

12531254
@Test(timeout = 10000)
@@ -1256,7 +1257,7 @@ public void testStreamWriterUserCloseNoMultiplexing() throws Exception {
12561257
StreamWriter.newBuilder(TEST_STREAM_1, client).setWriterSchema(createProtoSchema()).build();
12571258

12581259
writer.close();
1259-
assertTrue(writer.isDone());
1260+
assertTrue(writer.isClosed());
12601261
ApiFuture<AppendRowsResponse> appendFuture1 = sendTestMessage(writer, new String[] {"A"});
12611262
ExecutionException ex =
12621263
assertThrows(
@@ -1267,6 +1268,7 @@ public void testStreamWriterUserCloseNoMultiplexing() throws Exception {
12671268
assertEquals(
12681269
Status.Code.FAILED_PRECONDITION,
12691270
((StatusRuntimeException) ex.getCause()).getStatus().getCode());
1271+
assertTrue(writer.isUserClosed());
12701272
}
12711273

12721274
@Test(timeout = 10000)
@@ -1291,7 +1293,8 @@ public void testStreamWriterPermanentErrorMultiplexing() throws Exception {
12911293
appendFuture2.get();
12921294
});
12931295
assertTrue(ex.getCause() instanceof InvalidArgumentException);
1294-
assertFalse(writer.isDone());
1296+
assertFalse(writer.isClosed());
1297+
assertFalse(writer.isUserClosed());
12951298
}
12961299

12971300
@Test(timeout = 10000)
@@ -1311,7 +1314,8 @@ public void testStreamWriterPermanentErrorNoMultiplexing() throws Exception {
13111314
() -> {
13121315
appendFuture2.get();
13131316
});
1314-
assertTrue(writer.isDone());
1317+
assertTrue(writer.isClosed());
13151318
assertTrue(ex.getCause() instanceof InvalidArgumentException);
1319+
assertFalse(writer.isUserClosed());
13161320
}
13171321
}

0 commit comments

Comments
 (0)