Skip to content

Commit 05f7232

Browse files
committed
Fix race condition in AbstractDataStoreTest.test_aliveField_volatileVisibility
The test had a timing issue where all reader threads could complete before the writer thread set alive=false, causing the assertion to fail. Changes: - Increased observation iterations from 100 to 1000 for longer read duration - Added 5ms delay in writer thread before setting alive=false - Start writer thread before reader threads to ensure proper scheduling - Writer now sets flag while readers are actively reading This ensures the volatile field visibility can be properly tested without timing-dependent failures.
1 parent 8a814df commit 05f7232

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/test/java/org/codelibs/fess/ds/AbstractDataStoreTest.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,27 @@ public void test_aliveField_volatileVisibility() throws Exception {
121121

122122
final int readerThreadCount = 10;
123123
final Thread[] readerThreads = new Thread[readerThreadCount];
124-
final boolean[][] observations = new boolean[readerThreadCount][100];
124+
final boolean[][] observations = new boolean[readerThreadCount][1000];
125+
126+
// Writer thread sets alive to false after a small delay
127+
Thread writerThread = new Thread(() -> {
128+
// Small delay to ensure reader threads are running first
129+
try {
130+
Thread.sleep(5);
131+
} catch (InterruptedException e) {
132+
Thread.currentThread().interrupt();
133+
}
134+
dataStore.alive = false;
135+
});
136+
137+
// Start writer thread first
138+
writerThread.start();
125139

126140
// Start reader threads that continuously check alive field
127141
for (int i = 0; i < readerThreadCount; i++) {
128142
final int threadIndex = i;
129143
readerThreads[i] = new Thread(() -> {
130-
for (int j = 0; j < 100; j++) {
144+
for (int j = 0; j < 1000; j++) {
131145
observations[threadIndex][j] = dataStore.alive;
132146
// Small yield to allow context switching
133147
Thread.yield();
@@ -136,11 +150,7 @@ public void test_aliveField_volatileVisibility() throws Exception {
136150
readerThreads[i].start();
137151
}
138152

139-
// Writer thread sets alive to false
140-
Thread writerThread = new Thread(() -> {
141-
dataStore.alive = false;
142-
});
143-
writerThread.start();
153+
// Wait for writer thread to complete
144154
writerThread.join();
145155

146156
// Wait for all reader threads to complete
@@ -155,7 +165,7 @@ public void test_aliveField_volatileVisibility() throws Exception {
155165
// due to volatile ensuring visibility
156166
int falseCount = 0;
157167
for (int i = 0; i < readerThreadCount; i++) {
158-
for (int j = 0; j < 100; j++) {
168+
for (int j = 0; j < 1000; j++) {
159169
if (!observations[i][j]) {
160170
falseCount++;
161171
}

0 commit comments

Comments
 (0)