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

[JAVA-8353] Update the test to wait for assertion with timeout
This commit is contained in:
kwoyke 2021-11-22 09:15:45 +01:00 committed by GitHub
commit dd03c1ec48
5 changed files with 22 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

@ -2,6 +2,8 @@ package com.baeldung.exceptions.illegalmonitorstate;
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 +23,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(10), () -> assertEquals("test", receiver.getMessage()));
assertEquals("test", receiver.getMessage());
assertFalse(sender.hasIllegalMonitorStateExceptionOccurred()); assertFalse(sender.hasIllegalMonitorStateExceptionOccurred());
assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred()); assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred());
} }