Skip to content

Commit 9cb4658

Browse files
authored
Merge pull request eugenp#11487 from hkhan/JAVA-8353-fix-monitor-state-exception
[JAVA-8353] Update the test to wait for assertion with timeout
2 parents 97f900f + 6964f93 commit 9cb4658

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/SynchronizedReceiver.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.slf4j.LoggerFactory;
55

66
public class SynchronizedReceiver implements Runnable {
7-
private static Logger log = LoggerFactory.getLogger(SynchronizedReceiver.class);
7+
8+
private static final Logger LOG = LoggerFactory.getLogger(SynchronizedReceiver.class);
9+
810
private final Data data;
911
private String message;
1012
private boolean illegalMonitorStateExceptionOccurred;
@@ -20,10 +22,10 @@ public void run() {
2022
data.wait();
2123
this.message = data.receive();
2224
} catch (InterruptedException e) {
23-
log.error("thread was interrupted", e);
25+
LOG.error("thread was interrupted", e);
2426
Thread.currentThread().interrupt();
2527
} catch (IllegalMonitorStateException e) {
26-
log.error("illegal monitor state exception occurred", e);
28+
LOG.error("illegal monitor state exception occurred", e);
2729
illegalMonitorStateExceptionOccurred = true;
2830
}
2931
}

core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/SynchronizedSender.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.slf4j.LoggerFactory;
55

66
public class SynchronizedSender implements Runnable {
7-
private static Logger log = LoggerFactory.getLogger(SynchronizedSender.class);
7+
8+
private static final Logger LOG = LoggerFactory.getLogger(SynchronizedSender.class);
9+
810
private final Data data;
911
private boolean illegalMonitorStateExceptionOccurred;
1012

@@ -22,10 +24,10 @@ public void run() {
2224

2325
data.notifyAll();
2426
} catch (InterruptedException e) {
25-
log.error("thread was interrupted", e);
27+
LOG.error("thread was interrupted", e);
2628
Thread.currentThread().interrupt();
2729
} catch (IllegalMonitorStateException e) {
28-
log.error("illegal monitor state exception occurred", e);
30+
LOG.error("illegal monitor state exception occurred", e);
2931
illegalMonitorStateExceptionOccurred = true;
3032
}
3133
}

core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/UnsynchronizedReceiver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import org.slf4j.LoggerFactory;
55

66
public class UnsynchronizedReceiver implements Runnable {
7-
private static Logger log = LoggerFactory.getLogger(UnsynchronizedReceiver.class);
7+
private static final Logger LOG = LoggerFactory.getLogger(UnsynchronizedReceiver.class);
8+
89
private final Data data;
910
private String message;
1011
private boolean illegalMonitorStateExceptionOccurred;
@@ -19,10 +20,10 @@ public void run() {
1920
data.wait();
2021
this.message = data.receive();
2122
} catch (InterruptedException e) {
22-
log.error("thread was interrupted", e);
23+
LOG.error("thread was interrupted", e);
2324
Thread.currentThread().interrupt();
2425
} catch (IllegalMonitorStateException e) {
25-
log.error("illegal monitor state exception occurred", e);
26+
LOG.error("illegal monitor state exception occurred", e);
2627
illegalMonitorStateExceptionOccurred = true;
2728
}
2829
}

core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/UnsynchronizedSender.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import org.slf4j.LoggerFactory;
55

66
public class UnsynchronizedSender implements Runnable {
7-
private static Logger log = LoggerFactory.getLogger(UnsynchronizedSender.class);
7+
private static final Logger LOG = LoggerFactory.getLogger(UnsynchronizedSender.class);
8+
89
private final Data data;
910
private boolean illegalMonitorStateExceptionOccurred;
1011

@@ -21,10 +22,10 @@ public void run() {
2122

2223
data.notifyAll();
2324
} catch (InterruptedException e) {
24-
log.error("thread was interrupted", e);
25+
LOG.error("thread was interrupted", e);
2526
Thread.currentThread().interrupt();
2627
} catch (IllegalMonitorStateException e) {
27-
log.error("illegal monitor state exception occurred", e);
28+
LOG.error("illegal monitor state exception occurred", e);
2829
illegalMonitorStateExceptionOccurred = true;
2930
}
3031
}

core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/exceptions/illegalmonitorstate/IllegalMonitorStateExceptionUnitTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import java.time.Duration;
6+
57
import static org.junit.jupiter.api.Assertions.*;
68

79
public class IllegalMonitorStateExceptionUnitTest {
@@ -20,10 +22,9 @@ void whenSyncSenderAndSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldNotBe
2022

2123
senderThread.join(1000);
2224
receiverThread.join(1000);
23-
24-
Thread.sleep(2000);
2525

26-
assertEquals("test", receiver.getMessage());
26+
// we need to wait for enough time so that sender has had a chance to send the data
27+
assertTimeout(Duration.ofSeconds(10), () -> assertEquals("test", receiver.getMessage()));
2728
assertFalse(sender.hasIllegalMonitorStateExceptionOccurred());
2829
assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred());
2930
}

0 commit comments

Comments
 (0)