Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit 89c8623

Browse files
authored
fix: flaky writeapi manual client tests (#238)
1. There are some warnings in the test runs saying that open connections are not closed. Make sure everything is shutdown after test. 2. There is some unexpected exceptions thrown which is not caught. Now catch a more general exception and also fix some issues that incorrectly calling remove on List (which is not supported). 3. Make sure the executor in the tests are finished running, so it will not run into a race with test shutdown.
1 parent e0b0fcd commit 89c8623

6 files changed

Lines changed: 70 additions & 20 deletions

File tree

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1alpha2/DirectWriter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,9 @@ public static void testSetStub(
102102
BigQueryWriteClient stub, int maxTableEntry, SchemaCompact schemaCheck) {
103103
cache = WriterCache.getTestInstance(stub, maxTableEntry, schemaCheck);
104104
}
105+
106+
/** Clears the underlying cache and all the transport connections. */
107+
public static void clearCache() {
108+
cache.clear();
109+
}
105110
}

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1alpha2/WriterCache.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.cache.CacheBuilder;
2121
import com.google.protobuf.Descriptors.Descriptor;
2222
import java.io.IOException;
23+
import java.util.concurrent.ConcurrentMap;
2324
import java.util.logging.Logger;
2425
import java.util.regex.Matcher;
2526
import java.util.regex.Pattern;
@@ -144,6 +145,22 @@ public StreamWriter getTableWriter(String tableName, Descriptor userSchema)
144145
return writer;
145146
}
146147

148+
/** Clear the cache and close all the writers in the cache. */
149+
public void clear() {
150+
synchronized (this) {
151+
ConcurrentMap<String, Cache<Descriptor, StreamWriter>> map = writerCache.asMap();
152+
for (String key : map.keySet()) {
153+
Cache<Descriptor, StreamWriter> entry = writerCache.getIfPresent(key);
154+
ConcurrentMap<Descriptor, StreamWriter> entryMap = entry.asMap();
155+
for (Descriptor descriptor : entryMap.keySet()) {
156+
StreamWriter writer = entry.getIfPresent(descriptor);
157+
writer.close();
158+
}
159+
}
160+
writerCache.cleanUp();
161+
}
162+
}
163+
147164
@VisibleForTesting
148165
public long cachedTableCount() {
149166
synchronized (writerCache) {

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1alpha2/DirectWriterTest.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
import com.google.api.gax.grpc.testing.MockGrpcService;
2525
import com.google.api.gax.grpc.testing.MockServiceHelper;
2626
import com.google.cloud.bigquery.storage.test.Test.*;
27+
import com.google.common.collect.Sets;
2728
import com.google.protobuf.AbstractMessage;
2829
import com.google.protobuf.Timestamp;
2930
import java.io.IOException;
30-
import java.util.Arrays;
31-
import java.util.List;
32-
import java.util.UUID;
33-
import java.util.concurrent.ExecutionException;
31+
import java.util.*;
3432
import java.util.concurrent.ExecutorService;
3533
import java.util.concurrent.Executors;
34+
import java.util.concurrent.TimeUnit;
35+
import java.util.logging.Logger;
3636
import org.junit.*;
3737
import org.junit.runner.RunWith;
3838
import org.junit.runners.JUnit4;
@@ -42,6 +42,8 @@
4242

4343
@RunWith(JUnit4.class)
4444
public class DirectWriterTest {
45+
private static final Logger LOG = Logger.getLogger(DirectWriterTest.class.getName());
46+
4547
private static final String TEST_TABLE = "projects/p/datasets/d/tables/t";
4648
private static final String TEST_STREAM = "projects/p/datasets/d/tables/t/streams/s";
4749
private static final String TEST_STREAM_2 = "projects/p/datasets/d/tables/t/streams/s2";
@@ -86,7 +88,7 @@ public void tearDown() throws Exception {
8688
}
8789

8890
/** Response mocks for create a new writer */
89-
void WriterCreationResponseMock(String testStreamName, List<Long> responseOffsets) {
91+
void WriterCreationResponseMock(String testStreamName, Set<Long> responseOffsets) {
9092
// Response from CreateWriteStream
9193
Stream.WriteStream expectedResponse =
9294
Stream.WriteStream.newBuilder().setName(testStreamName).build();
@@ -117,7 +119,7 @@ public void testWriteSuccess() throws Exception {
117119
FooType m1 = FooType.newBuilder().setFoo("m1").build();
118120
FooType m2 = FooType.newBuilder().setFoo("m2").build();
119121

120-
WriterCreationResponseMock(TEST_STREAM, Arrays.asList(Long.valueOf(0L)));
122+
WriterCreationResponseMock(TEST_STREAM, Sets.newHashSet(Long.valueOf(0L)));
121123
ApiFuture<Long> ret = DirectWriter.<FooType>append(TEST_TABLE, Arrays.asList(m1, m2));
122124
verify(schemaCheck).check(TEST_TABLE, FooType.getDescriptor());
123125
assertEquals(Long.valueOf(0L), ret.get());
@@ -159,7 +161,7 @@ public void testWriteSuccess() throws Exception {
159161
assertEquals(expectRequest.toString(), actualRequests.get(3).toString());
160162

161163
// Write with a different schema.
162-
WriterCreationResponseMock(TEST_STREAM_2, Arrays.asList(Long.valueOf(0L)));
164+
WriterCreationResponseMock(TEST_STREAM_2, Sets.newHashSet(Long.valueOf(0L)));
163165
AllSupportedTypes m3 = AllSupportedTypes.newBuilder().setStringValue("s").build();
164166
ret = DirectWriter.<AllSupportedTypes>append(TEST_TABLE, Arrays.asList(m3));
165167
verify(schemaCheck).check(TEST_TABLE, AllSupportedTypes.getDescriptor());
@@ -181,6 +183,8 @@ public void testWriteSuccess() throws Exception {
181183
((Storage.CreateWriteStreamRequest) actualRequests.get(4)).getWriteStream().getType());
182184
assertEquals(TEST_STREAM_2, ((Storage.GetWriteStreamRequest) actualRequests.get(5)).getName());
183185
assertEquals(expectRequest.toString(), actualRequests.get(6).toString());
186+
187+
DirectWriter.clearCache();
184188
}
185189

186190
@Test
@@ -195,15 +199,17 @@ public void testWriteBadTableName() throws Exception {
195199
} catch (IllegalArgumentException expected) {
196200
assertEquals("Invalid table name: abc", expected.getMessage());
197201
}
202+
203+
DirectWriter.clearCache();
198204
}
199205

200206
@Test
201207
public void testConcurrentAccess() throws Exception {
202-
WriterCache cache = WriterCache.getTestInstance(client, 2, schemaCheck);
208+
DirectWriter.testSetStub(client, 2, schemaCheck);
203209
final FooType m1 = FooType.newBuilder().setFoo("m1").build();
204210
final FooType m2 = FooType.newBuilder().setFoo("m2").build();
205-
final List<Long> expectedOffset =
206-
Arrays.asList(
211+
final Set<Long> expectedOffset =
212+
Sets.newHashSet(
207213
Long.valueOf(0L),
208214
Long.valueOf(2L),
209215
Long.valueOf(4L),
@@ -221,12 +227,21 @@ public void run() {
221227
try {
222228
ApiFuture<Long> result =
223229
DirectWriter.<FooType>append(TEST_TABLE, Arrays.asList(m1, m2));
224-
assertTrue(expectedOffset.remove(result.get()));
225-
} catch (IOException | InterruptedException | ExecutionException e) {
226-
fail(e.getMessage());
230+
synchronized (expectedOffset) {
231+
assertTrue(expectedOffset.remove(result.get()));
232+
}
233+
} catch (Exception e) {
234+
fail(e.toString());
227235
}
228236
}
229237
});
230238
}
239+
executor.shutdown();
240+
try {
241+
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
242+
} catch (InterruptedException e) {
243+
LOG.info(e.toString());
244+
}
245+
DirectWriter.clearCache();
231246
}
232247
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ private ApiFuture<AppendRowsResponse> sendTestMessage(StreamWriter writer, Strin
135135

136136
@Test
137137
public void testTableName() throws Exception {
138-
StreamWriter writer = getTestStreamWriterBuilder().build();
139-
assertEquals("projects/p/datasets/d/tables/t", writer.getTableNameString());
138+
try (StreamWriter writer = getTestStreamWriterBuilder().build()) {
139+
assertEquals("projects/p/datasets/d/tables/t", writer.getTableNameString());
140+
}
140141
}
141142

142143
@Test
@@ -175,7 +176,7 @@ public void testAppendByDuration() throws Exception {
175176
.getSerializedRowsCount());
176177
assertEquals(
177178
true, testBigQueryWrite.getAppendRequests().get(0).getProtoRows().hasWriterSchema());
178-
writer.shutdown();
179+
writer.close();
179180
}
180181

181182
@Test
@@ -228,7 +229,7 @@ public void testAppendByNumBatchedMessages() throws Exception {
228229
.getSerializedRowsCount());
229230
assertEquals(
230231
false, testBigQueryWrite.getAppendRequests().get(1).getProtoRows().hasWriterSchema());
231-
writer.shutdown();
232+
writer.close();
232233
}
233234

234235
@Test
@@ -264,7 +265,7 @@ public void testAppendByNumBytes() throws Exception {
264265

265266
assertEquals(3, testBigQueryWrite.getAppendRequests().size());
266267

267-
writer.shutdown();
268+
writer.close();
268269
}
269270

270271
@Test
@@ -429,7 +430,8 @@ public void testFlowControlBehaviorException() throws Exception {
429430
try {
430431
appendFuture2.get();
431432
Assert.fail("This should fail");
432-
} catch (ExecutionException e) {
433+
} catch (Exception e) {
434+
LOG.info("ControlFlow test exception: " + e.toString());
433435
assertEquals("The maximum number of batch elements: 1 have been reached.", e.getMessage());
434436
}
435437
assertEquals(1L, appendFuture1.get().getOffset());

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1alpha2/WriterCacheTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public void testCreateNewWriter() throws Exception {
143143
assertEquals(TEST_TABLE, writer.getTableNameString());
144144
assertEquals(TEST_STREAM, writer.getStreamNameString());
145145
assertEquals(1, cache.cachedTableCount());
146+
cache.clear();
146147
}
147148

148149
@Test
@@ -173,6 +174,7 @@ public void testWriterExpired() throws Exception {
173174
"Cannot write to a stream that is already expired: projects/p/datasets/d/tables/t/streams/s",
174175
e.getMessage());
175176
}
177+
cache.clear();
176178
}
177179

178180
@Test
@@ -216,6 +218,7 @@ public void testWriterWithNewSchema() throws Exception {
216218
assertEquals(TEST_STREAM_3, writer4.getStreamNameString());
217219
assertEquals(TEST_STREAM_4, writer5.getStreamNameString());
218220
assertEquals(1, cache.cachedTableCount());
221+
cache.clear();
219222
}
220223

221224
@Test
@@ -259,6 +262,7 @@ public void testWriterWithDifferentTable() throws Exception {
259262
assertEquals(TEST_STREAM_31, writer4.getStreamNameString());
260263
assertEquals(TEST_STREAM, writer5.getStreamNameString());
261264
assertEquals(2, cache.cachedTableCount());
265+
cache.clear();
262266
}
263267

264268
@Test
@@ -275,7 +279,7 @@ public void testConcurrentAccess() throws Exception {
275279
public void run() {
276280
try {
277281
assertTrue(cache.getTableWriter(TEST_TABLE, FooType.getDescriptor()) != null);
278-
} catch (IOException | InterruptedException e) {
282+
} catch (Exception e) {
279283
fail(e.getMessage());
280284
}
281285
}

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1alpha2/it/ITBigQueryWriteManualClientTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,5 +391,12 @@ public Long call() throws IOException, InterruptedException, ExecutionException
391391
assertTrue(expectedOffset.remove(response.get()));
392392
}
393393
assertTrue(expectedOffset.isEmpty());
394+
executor.shutdown();
395+
try {
396+
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
397+
} catch (InterruptedException e) {
398+
LOG.info(e.toString());
399+
}
400+
DirectWriter.clearCache();
394401
}
395402
}

0 commit comments

Comments
 (0)