Skip to content

Commit fec352d

Browse files
committed
add unit tests for retry in TableDataWriteChannel
1 parent c46deaa commit fec352d

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryBaseService.java

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected BigQueryBaseService(ServiceOptions options) {
3131
.abortOn(RuntimeException.class)
3232
.retryOn(java.net.ConnectException.class) // retry on Connection Exception
3333
.retryOn(java.net.UnknownHostException.class) // retry on UnknownHostException
34+
.retryOn(java.net.SocketException.class) // retry on SocketException
3435
.addInterceptors(EXCEPTION_HANDLER_INTERCEPTOR)
3536
.build();
3637
}

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/TableDataWriteChannelTest.java

+53-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import com.google.cloud.bigquery.spi.BigQueryRpcFactory;
3434
import com.google.cloud.bigquery.spi.v2.HttpBigQueryRpc;
3535
import java.io.IOException;
36+
import java.net.ConnectException;
3637
import java.net.SocketException;
38+
import java.net.UnknownHostException;
3739
import java.nio.ByteBuffer;
3840
import java.util.Arrays;
3941
import java.util.Random;
@@ -114,18 +116,19 @@ public void testCreate() throws IOException {
114116
}
115117

116118
@Test
117-
public void testCreateRetryableError() throws IOException {
118-
BigQueryException exception = new BigQueryException(new SocketException("Socket closed"));
119+
public void testCreateRetryableErrors() throws IOException {
119120
when(bigqueryRpcMock.openSkipExceptionTranslation(
120121
new com.google.api.services.bigquery.model.Job()
121122
.setJobReference(JOB_INFO.getJobId().toPb())
122123
.setConfiguration(LOAD_CONFIGURATION.toPb())))
123-
.thenThrow(exception)
124+
.thenThrow(new SocketException("Socket closed"))
125+
.thenThrow(new UnknownHostException())
126+
.thenThrow(new ConnectException())
124127
.thenReturn(UPLOAD_ID);
125128
writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION);
126129
assertTrue(writer.isOpen());
127130
assertNull(writer.getJob());
128-
verify(bigqueryRpcMock, times(2))
131+
verify(bigqueryRpcMock, times(4))
129132
.openSkipExceptionTranslation(
130133
new com.google.api.services.bigquery.model.Job()
131134
.setJobReference(JOB_INFO.getJobId().toPb())
@@ -134,12 +137,11 @@ public void testCreateRetryableError() throws IOException {
134137

135138
@Test
136139
public void testCreateNonRetryableError() throws IOException {
137-
RuntimeException ex = new RuntimeException("expected");
138140
when(bigqueryRpcMock.openSkipExceptionTranslation(
139141
new com.google.api.services.bigquery.model.Job()
140142
.setJobReference(JOB_INFO.getJobId().toPb())
141143
.setConfiguration(LOAD_CONFIGURATION.toPb())))
142-
.thenThrow(ex);
144+
.thenThrow(new RuntimeException("expected"));
143145
try (TableDataWriteChannel channel =
144146
new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION)) {
145147
Assert.fail();
@@ -207,7 +209,7 @@ public void testWriteWithFlush() throws IOException {
207209
}
208210

209211
@Test
210-
public void testWritesAndFlush() throws IOException {
212+
public void testWritesAndFlushRetryableErrors() throws IOException {
211213
when(bigqueryRpcMock.openSkipExceptionTranslation(
212214
new com.google.api.services.bigquery.model.Job()
213215
.setJobReference(JOB_INFO.getJobId().toPb())
@@ -220,6 +222,9 @@ public void testWritesAndFlush() throws IOException {
220222
eq(0L),
221223
eq(DEFAULT_CHUNK_SIZE),
222224
eq(false)))
225+
.thenThrow(new SocketException("Socket closed"))
226+
.thenThrow(new UnknownHostException())
227+
.thenThrow(new ConnectException())
223228
.thenReturn(null);
224229
writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION);
225230
ByteBuffer[] buffers = new ByteBuffer[DEFAULT_CHUNK_SIZE / MIN_CHUNK_SIZE];
@@ -239,7 +244,48 @@ public void testWritesAndFlush() throws IOException {
239244
new com.google.api.services.bigquery.model.Job()
240245
.setJobReference(JOB_INFO.getJobId().toPb())
241246
.setConfiguration(LOAD_CONFIGURATION.toPb()));
247+
verify(bigqueryRpcMock, times(4))
248+
.writeSkipExceptionTranslation(
249+
eq(UPLOAD_ID),
250+
capturedBuffer.capture(),
251+
eq(0),
252+
eq(0L),
253+
eq(DEFAULT_CHUNK_SIZE),
254+
eq(false));
255+
}
256+
257+
@Test
258+
public void testWritesAndFlushNonRetryableError() throws IOException {
259+
when(bigqueryRpcMock.openSkipExceptionTranslation(
260+
new com.google.api.services.bigquery.model.Job()
261+
.setJobReference(JOB_INFO.getJobId().toPb())
262+
.setConfiguration(LOAD_CONFIGURATION.toPb())))
263+
.thenReturn(UPLOAD_ID);
264+
when(bigqueryRpcMock.writeSkipExceptionTranslation(
265+
eq(UPLOAD_ID),
266+
capturedBuffer.capture(),
267+
eq(0),
268+
eq(0L),
269+
eq(DEFAULT_CHUNK_SIZE),
270+
eq(false)))
271+
.thenThrow(new RuntimeException("expected"));
272+
try {
273+
writer = new TableDataWriteChannel(options, JOB_INFO.getJobId(), LOAD_CONFIGURATION);
274+
ByteBuffer[] buffers = new ByteBuffer[DEFAULT_CHUNK_SIZE / MIN_CHUNK_SIZE];
275+
for (int i = 0; i < buffers.length; i++) {
276+
buffers[i] = randomBuffer(MIN_CHUNK_SIZE);
277+
assertEquals(MIN_CHUNK_SIZE, writer.write(buffers[i]));
278+
}
279+
Assert.fail();
280+
} catch (RuntimeException expected) {
281+
Assert.assertEquals("java.lang.RuntimeException: expected", expected.getMessage());
282+
}
242283
verify(bigqueryRpcMock)
284+
.openSkipExceptionTranslation(
285+
new com.google.api.services.bigquery.model.Job()
286+
.setJobReference(JOB_INFO.getJobId().toPb())
287+
.setConfiguration(LOAD_CONFIGURATION.toPb()));
288+
verify(bigqueryRpcMock, times(1))
243289
.writeSkipExceptionTranslation(
244290
eq(UPLOAD_ID),
245291
capturedBuffer.capture(),

0 commit comments

Comments
 (0)