Merge pull request #11521 from hkhan/JAVA-8353-fix-monitor-exception

[JAVA-8353] Wait longer for the threads to finish
This commit is contained in:
kwoyke 2021-11-28 13:02:09 +01:00 committed by GitHub
commit 8809312a13
1 changed files with 9 additions and 8 deletions

View File

@ -1,11 +1,12 @@
package com.baeldung.exceptions.illegalmonitorstate; package com.baeldung.exceptions.illegalmonitorstate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.*;
public class IllegalMonitorStateExceptionUnitTest { public class IllegalMonitorStateExceptionUnitTest {
@Test @Test
@ -20,11 +21,11 @@ public class IllegalMonitorStateExceptionUnitTest {
Thread senderThread = new Thread(sender, "sender-thread"); Thread senderThread = new Thread(sender, "sender-thread");
senderThread.start(); senderThread.start();
senderThread.join(1000); // we need to wait for the sender and receiver threads to finish
receiverThread.join(1000); senderThread.join(10_000);
receiverThread.join(10_000);
// we need to wait for enough time so that sender has had a chance to send the data assertEquals("test", receiver.getMessage());
assertTimeout(Duration.ofSeconds(10), () -> assertEquals("test", receiver.getMessage()));
assertFalse(sender.hasIllegalMonitorStateExceptionOccurred()); assertFalse(sender.hasIllegalMonitorStateExceptionOccurred());
assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred()); assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred());
} }