[JAVA-8353] Update the test to wait for assertion with timeout

This commit is contained in:
Haroon Khan 2021-11-21 21:18:55 +00:00
parent 871b044ea5
commit 48a1cc5624
5 changed files with 23 additions and 15 deletions

View File

@ -4,7 +4,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class SynchronizedReceiver implements Runnable { 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 final Data data;
private String message; private String message;
private boolean illegalMonitorStateExceptionOccurred; private boolean illegalMonitorStateExceptionOccurred;
@ -20,10 +22,10 @@ public class SynchronizedReceiver implements Runnable {
data.wait(); data.wait();
this.message = data.receive(); this.message = data.receive();
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.error("thread was interrupted", e); LOG.error("thread was interrupted", e);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) { } catch (IllegalMonitorStateException e) {
log.error("illegal monitor state exception occurred", e); LOG.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true; illegalMonitorStateExceptionOccurred = true;
} }
} }

View File

@ -4,7 +4,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class SynchronizedSender implements Runnable { 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 final Data data;
private boolean illegalMonitorStateExceptionOccurred; private boolean illegalMonitorStateExceptionOccurred;
@ -22,10 +24,10 @@ public class SynchronizedSender implements Runnable {
data.notifyAll(); data.notifyAll();
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.error("thread was interrupted", e); LOG.error("thread was interrupted", e);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) { } catch (IllegalMonitorStateException e) {
log.error("illegal monitor state exception occurred", e); LOG.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true; illegalMonitorStateExceptionOccurred = true;
} }
} }

View File

@ -4,7 +4,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class UnsynchronizedReceiver implements Runnable { 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 final Data data;
private String message; private String message;
private boolean illegalMonitorStateExceptionOccurred; private boolean illegalMonitorStateExceptionOccurred;
@ -19,10 +20,10 @@ public class UnsynchronizedReceiver implements Runnable {
data.wait(); data.wait();
this.message = data.receive(); this.message = data.receive();
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.error("thread was interrupted", e); LOG.error("thread was interrupted", e);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) { } catch (IllegalMonitorStateException e) {
log.error("illegal monitor state exception occurred", e); LOG.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true; illegalMonitorStateExceptionOccurred = true;
} }
} }

View File

@ -4,7 +4,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class UnsynchronizedSender implements Runnable { 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 final Data data;
private boolean illegalMonitorStateExceptionOccurred; private boolean illegalMonitorStateExceptionOccurred;
@ -21,10 +22,10 @@ public class UnsynchronizedSender implements Runnable {
data.notifyAll(); data.notifyAll();
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.error("thread was interrupted", e); LOG.error("thread was interrupted", e);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) { } catch (IllegalMonitorStateException e) {
log.error("illegal monitor state exception occurred", e); LOG.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true; illegalMonitorStateExceptionOccurred = true;
} }
} }

View File

@ -1,7 +1,10 @@
package com.baeldung.exceptions.illegalmonitorstate; package com.baeldung.exceptions.illegalmonitorstate;
import com.google.common.util.concurrent.Uninterruptibles;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class IllegalMonitorStateExceptionUnitTest { public class IllegalMonitorStateExceptionUnitTest {
@ -21,9 +24,8 @@ public class IllegalMonitorStateExceptionUnitTest {
senderThread.join(1000); senderThread.join(1000);
receiverThread.join(1000); receiverThread.join(1000);
Thread.sleep(2000); // 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()));
assertEquals("test", receiver.getMessage());
assertFalse(sender.hasIllegalMonitorStateExceptionOccurred()); assertFalse(sender.hasIllegalMonitorStateExceptionOccurred());
assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred()); assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred());
} }