5151import java .util .Random ;
5252import java .util .Set ;
5353import java .util .concurrent .CountDownLatch ;
54+ import java .util .concurrent .ExecutorService ;
5455import java .util .concurrent .Executors ;
5556import java .util .concurrent .TimeUnit ;
5657import java .util .concurrent .atomic .AtomicBoolean ;
@@ -617,13 +618,14 @@ public void testWriteThenDeleteThenRead() throws IOException {
617618 Assertions .assertEquals (-1 , bytesRead );
618619 }
619620
620- @ RepeatedTest (10 )
621- void testConcurrentWriteAndTruncate () throws IOException , InterruptedException {
621+ @ RepeatedTest (50 )
622+ public void testConcurrentWriteAndTruncate () throws IOException , InterruptedException {
622623 AtomicBoolean keepWriting = new AtomicBoolean (true );
623- ByteBuffer buf = ByteBuffer .wrap ("the quick brown fox jumps over the lazy dog" .getBytes (StandardCharsets .UTF_8 ));
624- var timer = new CountDownLatch (1 );
625- var executor = Executors .newCachedThreadPool ();
626- try (FileChannel writingChannel = FileChannel .open (file , WRITE , CREATE )) {
624+ ByteBuffer buf = ByteBuffer .allocate (50_000 ); // 50 kiB
625+
626+ try (ExecutorService executor = Executors .newCachedThreadPool ();
627+ FileChannel writingChannel = FileChannel .open (file , WRITE , CREATE )) {
628+ var cdl = new CountDownLatch (3 );
627629 executor .submit (() -> {
628630 while (keepWriting .get ()) {
629631 try {
@@ -632,21 +634,22 @@ void testConcurrentWriteAndTruncate() throws IOException, InterruptedException {
632634 throw new UncheckedIOException (e );
633635 }
634636 buf .flip ();
637+ cdl .countDown ();
635638 }
636639 });
637- timer .await (500 , TimeUnit . MILLISECONDS );
640+ cdl .await ();
638641 try (FileChannel truncatingChannel = FileChannel .open (file , WRITE , TRUNCATE_EXISTING )) {
639642 keepWriting .set (false );
640643 }
641- executor .shutdown ();
642644 }
643645
644- Assertions .assertDoesNotThrow (() -> {
645- try (FileChannel readingChannel = FileChannel .open (file , READ )) {
646- var dst = ByteBuffer .allocate (buf .capacity ());
646+
647+ try (FileChannel readingChannel = FileChannel .open (file , READ )) {
648+ var dst = ByteBuffer .allocate (buf .capacity ());
649+ Assertions .assertDoesNotThrow (() -> {
647650 readingChannel .read (dst );
648- }
649- });
651+ });
652+ }
650653 }
651654
652655 }
0 commit comments