From c4ba25773e28ed7de92f6f5ba8a67c9c4e06f75d Mon Sep 17 00:00:00 2001 From: Daniel Cameron Date: Sat, 21 Jan 2017 10:27:54 +1100 Subject: [PATCH] Defensive programming around test case race condition to address #776 --- .../java/htsjdk/samtools/util/AsyncBufferedIteratorTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/test/java/htsjdk/samtools/util/AsyncBufferedIteratorTest.java b/src/test/java/htsjdk/samtools/util/AsyncBufferedIteratorTest.java index 817c60e54..4a330c469 100644 --- a/src/test/java/htsjdk/samtools/util/AsyncBufferedIteratorTest.java +++ b/src/test/java/htsjdk/samtools/util/AsyncBufferedIteratorTest.java @@ -73,9 +73,15 @@ public void testBackgroundBlocks() throws InterruptedException { TestCloseableIterator it = new TestCloseableIterator(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); AsyncBufferedIterator abi = new AsyncBufferedIterator(it, 3, 2, "testBackgroundBlocks"); Assert.assertNotNull(getThreadWithName("testBackgroundBlocks")); - Thread.sleep(10); // how do we write this test and not be subject to race conditions? + // how do we write this test and not be subject to race conditions? // should have read 9 records: 2*3 in the buffers, and another 3 read but - // blocking waiting to be added + // blocking waiting to be added + for (int i = 0; i < 64; i++) { + if (it.consumed() >= 9) { + break; + } + Thread.sleep(1); + } Assert.assertEquals(it.consumed(), 9); abi.close(); }