Skip to content

Commit

Permalink
fix: A test race in JsonStreamWriterTest (#722)
Browse files Browse the repository at this point in the history
* fix: a race condition in test

* .

* .
  • Loading branch information
yirutang committed Dec 10, 2020
1 parent d90977f commit 35fe606
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
Expand Up @@ -34,9 +34,7 @@
import com.google.protobuf.Descriptors.DescriptorValidationException;
import com.google.protobuf.Timestamp;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import org.json.JSONArray;
Expand Down Expand Up @@ -777,13 +775,13 @@ public void testMultiThreadAppendNoSchemaUpdate() throws Exception {
final JSONArray jsonArr = new JSONArray();
jsonArr.put(foo);

final HashSet<Long> offset_sets = new HashSet<Long>();
final Collection<Long> offsetSets = Collections.synchronizedCollection(new HashSet<Long>());
int thread_nums = 5;
Thread[] thread_arr = new Thread[thread_nums];
for (int i = 0; i < thread_nums; i++) {
testBigQueryWrite.addResponse(
Storage.AppendRowsResponse.newBuilder().setOffset((long) i).build());
offset_sets.add((long) i);
offsetSets.add((long) i);
Thread t =
new Thread(
new Runnable() {
Expand All @@ -792,7 +790,7 @@ public void run() {
ApiFuture<AppendRowsResponse> appendFuture =
writer.append(jsonArr, -1, /* allowUnknownFields */ false);
AppendRowsResponse response = appendFuture.get();
offset_sets.remove(response.getOffset());
offsetSets.remove(response.getOffset());
} catch (Exception e) {
LOG.severe("Thread execution failed: " + e.getMessage());
}
Expand All @@ -805,7 +803,7 @@ public void run() {
for (int i = 0; i < thread_nums; i++) {
thread_arr[i].join();
}
assertTrue(offset_sets.size() == 0);
assertTrue(offsetSets.size() == 0);
for (int i = 0; i < thread_nums; i++) {
assertEquals(
1,
Expand Down Expand Up @@ -842,7 +840,7 @@ public void testMultiThreadAppendWithSchemaUpdate() throws Exception {
final JSONArray jsonArr = new JSONArray();
jsonArr.put(foo);

final HashSet<Long> offsetSets = new HashSet<Long>();
final Collection<Long> offsetSets = Collections.synchronizedCollection(new HashSet<Long>());
int numberThreads = 5;
Thread[] thread_arr = new Thread[numberThreads];
for (int i = 0; i < numberThreads; i++) {
Expand Down
Expand Up @@ -33,9 +33,7 @@
import com.google.protobuf.Descriptors.DescriptorValidationException;
import com.google.protobuf.Timestamp;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import org.json.JSONArray;
Expand Down Expand Up @@ -775,12 +773,12 @@ public void testMultiThreadAppendNoSchemaUpdate() throws Exception {
final JSONArray jsonArr = new JSONArray();
jsonArr.put(foo);

final HashSet<Long> offset_sets = new HashSet<Long>();
final Collection<Long> offsetSets = Collections.synchronizedCollection(new HashSet<Long>());
int thread_nums = 5;
Thread[] thread_arr = new Thread[thread_nums];
for (int i = 0; i < thread_nums; i++) {
testBigQueryWrite.addResponse(AppendRowsResponse.newBuilder().setOffset((long) i).build());
offset_sets.add((long) i);
offsetSets.add((long) i);
Thread t =
new Thread(
new Runnable() {
Expand All @@ -789,7 +787,7 @@ public void run() {
ApiFuture<AppendRowsResponse> appendFuture =
writer.append(jsonArr, -1, /* allowUnknownFields */ false);
AppendRowsResponse response = appendFuture.get();
offset_sets.remove(response.getOffset());
offsetSets.remove(response.getOffset());
} catch (Exception e) {
LOG.severe("Thread execution failed: " + e.getMessage());
}
Expand All @@ -802,7 +800,7 @@ public void run() {
for (int i = 0; i < thread_nums; i++) {
thread_arr[i].join();
}
assertTrue(offset_sets.size() == 0);
assertTrue(offsetSets.size() == 0);
for (int i = 0; i < thread_nums; i++) {
assertEquals(
1,
Expand Down Expand Up @@ -839,7 +837,7 @@ public void testMultiThreadAppendWithSchemaUpdate() throws Exception {
final JSONArray jsonArr = new JSONArray();
jsonArr.put(foo);

final HashSet<Long> offsetSets = new HashSet<Long>();
final Collection<Long> offsetSets = Collections.synchronizedCollection(new HashSet<Long>());
int numberThreads = 5;
Thread[] thread_arr = new Thread[numberThreads];
for (int i = 0; i < numberThreads; i++) {
Expand Down

0 comments on commit 35fe606

Please sign in to comment.