Skip to content

Commit d0e0d87

Browse files
committed
Add Service Executor Start and Wake State Test
1 parent 30d6eb3 commit d0e0d87

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/MessageQueue.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
public class MessageQueue {
3737

3838
private final BlockingQueue<Message> blkQueue;
39+
public final Object serviceExecutorWait = new Object();
3940

4041
// Default constructor when called creates Blocking Queue object.
4142
public MessageQueue() {
@@ -50,8 +51,8 @@ public void submitMsg(Message msg) {
5051
try {
5152
if (null != msg) {
5253
blkQueue.add(msg);
53-
synchronized (ServiceExecutor.serviceExecutorWait) {
54-
ServiceExecutor.serviceExecutorWait.notifyAll();
54+
synchronized (serviceExecutorWait) {
55+
serviceExecutorWait.notifyAll();
5556
}
5657
}
5758
} catch (Exception e) {

queue-based-load-leveling/src/main/java/com/iluwatar/queue/load/leveling/ServiceExecutor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
@Slf4j
3434
public class ServiceExecutor implements Runnable {
3535
private final MessageQueue msgQueue;
36-
public static final Object serviceExecutorWait = new Object();
3736
public ServiceExecutor(MessageQueue msgQueue) {
3837
this.msgQueue = msgQueue;
3938
}
@@ -50,8 +49,8 @@ public void run() {
5049
LOGGER.info(msg + " is served.");
5150
} else {
5251
LOGGER.info("Service Executor: Waiting for Messages to serve .. ");
53-
synchronized (serviceExecutorWait) {
54-
serviceExecutorWait.wait();
52+
synchronized (msgQueue.serviceExecutorWait) {
53+
msgQueue.serviceExecutorWait.wait();
5554
}
5655
}
5756
}

queue-based-load-leveling/src/test/java/com/iluwatar/queue/load/leveling/TaskGenSrvExeTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
*/
2525
package com.iluwatar.queue.load.leveling;
2626

27+
import static org.junit.jupiter.api.Assertions.assertEquals;
2728
import static org.junit.jupiter.api.Assertions.assertNotNull;
2829

30+
import lombok.extern.slf4j.Slf4j;
2931
import org.junit.jupiter.api.Test;
3032

3133
/**
3234
* Test case for submitting Message to Blocking Queue by TaskGenerator and retrieve the message by
3335
* ServiceExecutor.
3436
*/
37+
@Slf4j
3538
class TaskGenSrvExeTest {
3639

3740
@Test
@@ -53,4 +56,36 @@ void taskGeneratorTest() {
5356
assertNotNull(srvExeThr);
5457
}
5558

59+
/**
60+
* Tests that service executor waits at start since no message is sent to execute upon.
61+
* @throws InterruptedException
62+
*/
63+
@Test
64+
void serviceExecutorStartStateTest() throws InterruptedException {
65+
var msgQueue = new MessageQueue();
66+
var srvRunnable = new ServiceExecutor(msgQueue);
67+
var srvExeThr = new Thread(srvRunnable);
68+
srvExeThr.start();
69+
Thread.sleep(200); // sleep a little until service executor thread waits
70+
LOGGER.info("Current Service Executor State: " + srvExeThr.getState());
71+
assertEquals(srvExeThr.getState(), Thread.State.WAITING);
72+
73+
}
74+
75+
@Test
76+
void serviceExecutorWakeStateTest() throws InterruptedException {
77+
var msgQueue = new MessageQueue();
78+
var srvRunnable = new ServiceExecutor(msgQueue);
79+
var srvExeThr = new Thread(srvRunnable);
80+
srvExeThr.start();
81+
Thread.sleep(200); // sleep a little until service executor thread waits
82+
synchronized (msgQueue.serviceExecutorWait){
83+
msgQueue.serviceExecutorWait.notifyAll();
84+
}
85+
var srvExeState = srvExeThr.getState();
86+
LOGGER.info("Current Service Executor State: " + srvExeState);
87+
assertEquals(srvExeState, Thread.State.RUNNABLE);
88+
89+
}
90+
5691
}

0 commit comments

Comments
 (0)