Skip to content
Permalink
Browse files
fix: A test race in JsonStreamWriterTest (#722)
* fix: a race condition in test

* .

* .
  • Loading branch information
yirutang committed Dec 10, 2020
1 parent d90977f commit 35fe606e43a47dfe844666bf3e385f9c2e582977
@@ -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;
@@ -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() {
@@ -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());
}
@@ -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,
@@ -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++) {
@@ -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;
@@ -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() {
@@ -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());
}
@@ -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,
@@ -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++) {

0 comments on commit 35fe606

Please sign in to comment.