Skip to content

Commit afd2a7a

Browse files
committed
fix possible data race issue in test cases
1 parent 4e3a884 commit afd2a7a

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

src/test/java/com/qiniu/storage/FixBlockUploaderWithRecorderTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.charset.Charset;
2020
import java.util.Date;
2121
import java.util.concurrent.*;
22+
import java.util.concurrent.atomic.AtomicBoolean;
2223

2324
public class FixBlockUploaderWithRecorderTest {
2425
int blockSize = 1024 * 1024 * 8;
@@ -102,7 +103,7 @@ public void breakThenUpload(final ExecutorService pool1, final ExecutorService p
102103
StringMap p = new StringMap().put("returnBody", returnBody);
103104

104105
final String token = TestConfig.testAuth.uploadToken(bucket, expectKey, 3600, p);
105-
final int[] t1Finished = { 0 };
106+
final AtomicBoolean t1Finished = new AtomicBoolean(false);
106107

107108
Thread t1 = new Thread() {
108109
@Override
@@ -154,14 +155,14 @@ public void run() {
154155
int it1Finished = 0;
155156
for (; it1Finished < 1500; it1Finished++) {
156157
doSleep(100);
157-
if (t1Finished[0] == 1) {
158+
if (t1Finished.get()) {
158159
break;
159160
}
160161
}
161-
System.out.println("t1Finished[0] == 1 " + (t1Finished[0] == 1) + " " + it1Finished);
162+
System.out.println("t1Finished.get() == true " + t1Finished.get() + " " + it1Finished);
162163
doSleep(1500);
163164
final FixBlockUploader.FileBlockData fbd2 = new FixBlockUploader.FileBlockData(blockSize, f);
164-
final int[] t2Finished = { 0 };
165+
final AtomicBoolean t2Finished = new AtomicBoolean(false);
165166
Thread t2 = new Thread() {
166167
@Override
167168
public void run() {
@@ -197,11 +198,11 @@ public void run() {
197198
int it2Finished = 0;
198199
for (; it2Finished < 1500; it2Finished++) {
199200
doSleep(100);
200-
if (t2Finished[0] == 1) {
201+
if (t2Finished.get()) {
201202
break;
202203
}
203204
}
204-
System.out.println("t2Finished[0] == 1 " + (t2Finished[0] == 1) + " " + it2Finished);
205+
System.out.println("t2Finished.get() == true " + t2Finished.get() + " " + it2Finished);
205206
doSleep(1500);
206207
System.out.println("------ start 3, " + new Date());
207208
try {
@@ -232,7 +233,7 @@ public void run() {
232233
}
233234

234235
private void upload(int idx, FixBlockUploader.FileBlockData fbd, String token, String expectKey,
235-
final ExecutorService pool, int maxRunningBlock, final int[] tFinished, String etag) {
236+
final ExecutorService pool, int maxRunningBlock, final AtomicBoolean tFinished, String etag) {
236237
try {
237238
System.out.println("------ start " + idx + ", " + new Date());
238239
Response r = up.upload(fbd, new FixBlockUploader.StaticToken(token), expectKey, null, pool,
@@ -242,10 +243,10 @@ private void upload(int idx, FixBlockUploader.FileBlockData fbd, String token, S
242243
assertEquals(expectKey, ret.key);
243244
assertEquals(String.valueOf(fbd.size()), ret.fsize);
244245
assertEquals(etag, ret.hash);
245-
tFinished[0] = 1;
246+
tFinished.set(true);
246247
} catch (Exception e) {
247248
System.out.println("======= end " + idx + ": ");
248-
tFinished[0] = 1;
249+
tFinished.set(true);
249250
e.printStackTrace();
250251
}
251252
}

src/test/java/test/com/qiniu/storage/RecordUploadTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.lang.reflect.Method;
2222
import java.util.Date;
2323
import java.util.Random;
24+
import java.util.concurrent.atomic.AtomicBoolean;
25+
2426
import org.junit.jupiter.api.Tag;
2527
import org.junit.jupiter.api.Test;
2628

@@ -89,15 +91,15 @@ public void run() {
8991
System.out.println("UP: " + i + ", exception run");
9092
e.printStackTrace();
9193
}
92-
complete.isComplete = true;
94+
complete.isComplete.set(true);
9395
}
9496
}.start();
9597

96-
final boolean[] ch = new boolean[] { true };
98+
final AtomicBoolean ch = new AtomicBoolean(true);
9799
// 显示断点记录文件
98100
Thread showRecord = new Thread() {
99101
public void run() {
100-
for (; ch[0];) {
102+
while (ch.get()) {
101103
doSleep(1000);
102104
showRecord("normal: " + size + " :", recorder, recordKey);
103105
}
@@ -122,7 +124,7 @@ public void run() {
122124
}
123125

124126
System.out.println("response is " + response);
125-
while (!complete.isComplete) {
127+
while (!complete.isComplete.get()) {
126128
doSleep(200);
127129
}
128130

@@ -138,7 +140,7 @@ public void run() {
138140

139141
showRecord("done: " + size + " :", recorder, recordKey);
140142

141-
ch[0] = false;
143+
ch.set(false);
142144

143145
String etag = Etag.file(f);
144146
System.out.println("etag: " + etag);
@@ -363,6 +365,6 @@ public Response up(Region region) throws Exception {
363365
}
364366

365367
static class Complete {
366-
boolean isComplete = false;
368+
AtomicBoolean isComplete = new AtomicBoolean(false);
367369
}
368370
}

0 commit comments

Comments
 (0)