From 22fbee43fbb27cbbf2ccc623369bb275b99d46d8 Mon Sep 17 00:00:00 2001 From: grog Date: Sat, 9 Dec 2023 11:04:57 -0800 Subject: [PATCH] replaced TestCatcher in clock test with mock gateway --- .../org/myrobotlab/service/MockGateway.java | 5 +- .../org/myrobotlab/service/ClockTest.java | 119 +++++------------- 2 files changed, 33 insertions(+), 91 deletions(-) diff --git a/src/main/java/org/myrobotlab/service/MockGateway.java b/src/main/java/org/myrobotlab/service/MockGateway.java index f16a0ec756..6d85e05d01 100644 --- a/src/main/java/org/myrobotlab/service/MockGateway.java +++ b/src/main/java/org/myrobotlab/service/MockGateway.java @@ -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); diff --git a/src/test/java/org/myrobotlab/service/ClockTest.java b/src/test/java/org/myrobotlab/service/ClockTest.java index b9761ac743..83de45d7fa 100644 --- a/src/test/java/org/myrobotlab/service/ClockTest.java +++ b/src/test/java/org/myrobotlab/service/ClockTest.java @@ -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; @@ -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"); @@ -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()); @@ -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");