Skip to content

Commit

Permalink
replaced TestCatcher in clock test with mock gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick committed Dec 9, 2023
1 parent ffe474d commit 22fbee4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 91 deletions.
5 changes: 3 additions & 2 deletions src/main/java/org/myrobotlab/service/MockGateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,9 @@ public Message waitForMsg(String name, String callback, long maxTimeWaitMs) thro
String fullName = getFullRemoteName(name);

String key = String.format("%s.%s", fullName, callback);
if (!sendQueues.containsKey(key)) {
return null;

if (!sendQueues.containsKey(key)) {
sendQueues.put(key, new LinkedBlockingQueue<>());
}

Message msg = sendQueues.get(key).poll(maxTimeWaitMs, TimeUnit.MILLISECONDS);
Expand Down
119 changes: 30 additions & 89 deletions src/test/java/org/myrobotlab/service/ClockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.myrobotlab.framework.Message;
import org.myrobotlab.framework.Service;
import org.myrobotlab.test.AbstractTest;

Expand All @@ -20,8 +22,11 @@ public class ClockTest extends AbstractTest {
public void testService() throws Exception {

Python python = (Python) Runtime.start("python", "Python");
TestCatcher catcher = (TestCatcher) Runtime.start("catcher", "TestCatcher");
catcher.clear();

MockGateway gateway = (MockGateway)Runtime.start("gateway", "MockGateway");
gateway.clear();

// Runtime.start("webgui", "WebGui");

// check service script
// python.execResource("Clock/Clock.py");
Expand All @@ -46,115 +51,48 @@ public void testService() throws Exception {

// set subscription
clock.addListener("publishEpoch", "catcher", "onLong");
clock.addListener("publishEpoch", "mocker@mockId");

// watchdog - by default it starts with the "wait" when started vs the event
// must not have generated a pulse
catcher.longs.clear();
clock.startClock();
Service.sleep(500);
// starting clock should not immediately fire pulse
assertEquals("start sleep 500ms", 0, catcher.longs.size());
assertNull("start sleep 500ms", gateway.getMsg("mocker", "onEpoch"));

Service.sleep(800);
assertEquals(1, catcher.longs.size());
assertNotNull("should have an epoch msg", gateway.getMsg("mocker", "onEpoch"));

// resetting watchdog
clock.stopClock();
Service.sleep(10);
assertTrue(!clock.isClockRunning());
catcher.longs.clear();
gateway.clear();

clock.startClock();
Service.sleep(100);
assertEquals("after 100ms", 0, catcher.longs.size());
assertNull("no msg yet", gateway.getMsg("mocker", "onEpoch"));
clock.restartClock();

Service.sleep(100);
assertEquals("restart 1", 0, catcher.longs.size());
Service.sleep(600);
assertNull("restart 1 no msg yet", gateway.getMsg("mocker", "onEpoch"));
clock.restartClock();

Service.sleep(100);
assertEquals("restart 2", 0, catcher.longs.size());
Service.sleep(600);
assertNull("restart 2 no msg yet", gateway.getMsg("mocker", "onEpoch"));
clock.restartClock();

Service.sleep(100);
assertEquals("restart 3", 0, catcher.longs.size());
Service.sleep(600);
assertNull("restart 3 no msg yet", gateway.getMsg("mocker", "onEpoch"));
clock.restartClock();

Service.sleep(100);
assertEquals("restart 4", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 5", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 6", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 7", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 8", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 9", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 10", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 11", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 12", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 13", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 14", 0, catcher.longs.size());
Service.sleep(600);
assertNull("restart 4 no msg yet", gateway.getMsg("mocker", "onEpoch"));
clock.restartClock();
Service.sleep(100);
assertEquals("restart 15", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 16", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 17", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 18", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 19", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 20", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 21", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 22", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 23", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 24", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 25", 0, catcher.longs.size());
clock.restartClock();
Service.sleep(100);
assertEquals("restart 26", 0, catcher.longs.size());
clock.restartClock();


// wait now for the event
Service.sleep(1100);
log.info("size {}",catcher.longs.size());
assertEquals(1, catcher.longs.size());
assertNotNull("should have event now", gateway.getMsg("mocker", "onEpoch"));
clock.restartClock();
Service.sleep(500);
assertTrue(clock.isClockRunning());
Expand All @@ -163,23 +101,26 @@ public void testService() throws Exception {
clock.stopClock();
Service.sleep(10);
assertTrue(!clock.isClockRunning());
catcher.longs.clear();
gateway.clear();

clock.addClockEvent("catcher", "onString", "hello!");
clock.addClockEvent("mocker@mockId", "onString", "hello!");
clock.startClock();

// reset
Message msg = gateway.getMsg("mocker", "onString");
Service.sleep(500);
assertEquals("after adding clock event start", 0, catcher.longs.size());
assertNull("should not have msg yet", msg);
clock.restartClock();

// reset
Service.sleep(500);
assertEquals("after restart with event", 0, catcher.longs.size());
msg = gateway.getMsg("mocker", "onString");
assertNull("should not have msg yet after restart", msg);
clock.restartClock();

String hello = catcher.strings.poll(1500, TimeUnit.MILLISECONDS);
assertEquals("hello!", hello);
msg = gateway.waitForMsg("mocker", "onString", 5000);
assertEquals("hello!", msg.data[0]);

Runtime.release("clockTest");

Expand Down

0 comments on commit 22fbee4

Please sign in to comment.