Skip to content

Commit 02f1126

Browse files
authored
Fix flaky unit test (#6984)
This test is flaky because there is a race condition. If the extra thread calls the read method before the test writes the second string, then the read method will not wait for new bytes and will return -1 because the download state is COMPLETED. The test only passes when the timing works out such that the write happens before the extra thread calls the read method. Now, the test sets the download state to IN_PROGRESS, so the read method will correctly wait for new bytes if it is called before the write happens.
1 parent f068c54 commit 02f1126

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

catalog/core/catalog-core-standardframework/src/test/java/ddf/catalog/resource/download/ReliableResourceInputStreamTest.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,13 @@ public void testReadByteBufferFbosBytesWritten() throws Exception {
149149

150150
@Test
151151
public void testReadByteBufferBlocksUntilNewFbosBytesWritten() throws Exception {
152+
when(downloadState.getDownloadState())
153+
.thenReturn(DownloadManagerState.DownloadState.IN_PROGRESS);
152154
final ReliableResourceInputStream is =
153155
new ReliableResourceInputStream(
154156
fbos, countingFbos, downloadState, downloadIdentifier, resourceResponse);
155157
is.setCallableAndItsFuture(reliableResourceCallable, downloadFuture);
156-
byte[] bytes = new String("Hello World").getBytes();
158+
byte[] bytes = "Hello World".getBytes();
157159
countingFbos.write(bytes, 0, bytes.length);
158160
final byte[] buffer = new byte[50];
159161
int numBytesRead = is.read(buffer, 0, buffer.length);
@@ -163,33 +165,33 @@ public void testReadByteBufferBlocksUntilNewFbosBytesWritten() throws Exception
163165
// to FileBackedOutputStream)
164166

165167
Callable<Integer> callable =
166-
new Callable<Integer>() {
167-
@Override
168-
public Integer call() {
169-
int numBytesRead2 = 0;
170-
try {
171-
numBytesRead2 = is.read(buffer, 0, buffer.length);
172-
} catch (IOException e) {
173-
LOGGER.info("Failed to read bytes second time", e);
174-
}
175-
return numBytesRead2;
168+
() -> {
169+
int numBytesRead2 = 0;
170+
try {
171+
numBytesRead2 = is.read(buffer, 0, buffer.length);
172+
} catch (IOException e) {
173+
LOGGER.info("Failed to read bytes second time", e);
176174
}
175+
return numBytesRead2;
177176
};
178177

179178
ExecutorService executor = Executors.newCachedThreadPool();
180179
Future<Integer> future = executor.submit(callable);
181180

182-
// Write second string to FileBackedOutputStream - ReliableResourceInputStream's running
183-
// read(byte[], off, len) method is in loop waiting for new bytes to be written and should
184-
// detect this, read the new bytes and put them in the buffer
185-
String secondString = "Hello a second time";
186-
byte[] bytes2 = secondString.getBytes();
187-
countingFbos.write(bytes2, 0, bytes2.length);
188-
Integer bytesReadCount = future.get();
189-
assertThat(bytesReadCount, is(bytes2.length));
190-
assertThat(new String(buffer), containsString(secondString));
191-
192-
is.close();
181+
try {
182+
// Write second string to FileBackedOutputStream - ReliableResourceInputStream's running
183+
// read(byte[], off, len) method is in loop waiting for new bytes to be written and should
184+
// detect this, read the new bytes and put them in the buffer
185+
String secondString = "Hello a second time";
186+
byte[] bytes2 = secondString.getBytes();
187+
countingFbos.write(bytes2, 0, bytes2.length);
188+
Integer bytesReadCount = future.get();
189+
assertThat(bytesReadCount, is(bytes2.length));
190+
assertThat(new String(buffer), containsString(secondString));
191+
} finally {
192+
executor.shutdownNow();
193+
is.close();
194+
}
193195
}
194196

195197
@Test(expected = NullPointerException.class)

0 commit comments

Comments
 (0)