[JAVA-8353] Update the test to wait for assertion with timeout
This commit is contained in:
parent
871b044ea5
commit
48a1cc5624
|
@ -4,7 +4,9 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SynchronizedReceiver implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(SynchronizedReceiver.class);
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SynchronizedReceiver.class);
|
||||
|
||||
private final Data data;
|
||||
private String message;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
@ -20,10 +22,10 @@ public class SynchronizedReceiver implements Runnable {
|
|||
data.wait();
|
||||
this.message = data.receive();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
LOG.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
LOG.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SynchronizedSender implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(SynchronizedSender.class);
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SynchronizedSender.class);
|
||||
|
||||
private final Data data;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
||||
|
@ -22,10 +24,10 @@ public class SynchronizedSender implements Runnable {
|
|||
|
||||
data.notifyAll();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
LOG.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
LOG.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,8 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class UnsynchronizedReceiver implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(UnsynchronizedReceiver.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UnsynchronizedReceiver.class);
|
||||
|
||||
private final Data data;
|
||||
private String message;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
@ -19,10 +20,10 @@ public class UnsynchronizedReceiver implements Runnable {
|
|||
data.wait();
|
||||
this.message = data.receive();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
LOG.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
LOG.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,8 @@ import org.slf4j.Logger;
|
|||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class UnsynchronizedSender implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(UnsynchronizedSender.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UnsynchronizedSender.class);
|
||||
|
||||
private final Data data;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
||||
|
@ -21,10 +22,10 @@ public class UnsynchronizedSender implements Runnable {
|
|||
|
||||
data.notifyAll();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
LOG.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
LOG.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package com.baeldung.exceptions.illegalmonitorstate;
|
||||
|
||||
import com.google.common.util.concurrent.Uninterruptibles;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class IllegalMonitorStateExceptionUnitTest {
|
||||
|
@ -20,10 +23,9 @@ public class IllegalMonitorStateExceptionUnitTest {
|
|||
|
||||
senderThread.join(1000);
|
||||
receiverThread.join(1000);
|
||||
|
||||
Thread.sleep(2000);
|
||||
|
||||
assertEquals("test", receiver.getMessage());
|
||||
// we need to wait for enough time so that sender has had a chance to send the data
|
||||
assertTimeout(Duration.ofSeconds(5), () -> assertEquals("test", receiver.getMessage()));
|
||||
assertFalse(sender.hasIllegalMonitorStateExceptionOccurred());
|
||||
assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue