Skip to content

Commit 6a5cc13

Browse files
committed
[JAVA-8353] Update the test to wait for assertion with timeout
1 parent 3caa9fd commit 6a5cc13

5 files changed

Lines changed: 23 additions & 15 deletions

File tree

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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.baeldung.exceptions.illegalmonitorstate;
22

3+
import com.google.common.util.concurrent.Uninterruptibles;
34
import org.junit.jupiter.api.Test;
45

6+
import java.time.Duration;
7+
58
import static org.junit.jupiter.api.Assertions.*;
69

710
public class IllegalMonitorStateExceptionUnitTest {
@@ -20,10 +23,9 @@ void whenSyncSenderAndSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldNotBe
2023

2124
senderThread.join(1000);
2225
receiverThread.join(1000);
23-
24-
Thread.sleep(2000);
2526

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

0 commit comments

Comments
 (0)