From f84a43c59ec5466214e3ac2f90e7d182253823a4 Mon Sep 17 00:00:00 2001 From: aidnem <99768676+aidnem@users.noreply.github.com> Date: Mon, 25 Nov 2024 10:43:40 -0500 Subject: [PATCH] Add tests for monitor fault callbacks --- monitors/src/test/java/MonitorTests.java | 32 ++++++++++++++++++++---- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/monitors/src/test/java/MonitorTests.java b/monitors/src/test/java/MonitorTests.java index 42dd1a3..430840e 100644 --- a/monitors/src/test/java/MonitorTests.java +++ b/monitors/src/test/java/MonitorTests.java @@ -5,9 +5,6 @@ import org.junit.jupiter.api.Test; public class MonitorTests { - // TODO: FIGURE OUT HOW TO TEST THIS - // Callbacks are hard :( - // Mimics a changing robot state private boolean isStateValid = true; @@ -15,6 +12,14 @@ private boolean getIsStateValid() { return isStateValid; } + // Number to be incremented by fault callback + private int faultCallbackCalls = 0; + + // Example fault callback to verify that the callback was called + private void callFaultCallback() { + faultCallbackCalls++; + } + @Test public void nonStickyTest() { isStateValid = true; @@ -25,36 +30,45 @@ public void nonStickyTest() { .withStickyness(false) .withIsStateValidSupplier(() -> getIsStateValid()) .withTimeToFault(1.0) - .withFaultCallback(() -> {}) + .withFaultCallback( + () -> { + callFaultCallback(); + }) .build(); exampleMonitor.periodic(0.0); Assertions.assertFalse(exampleMonitor.isFaulted()); Assertions.assertFalse(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 0); isStateValid = false; exampleMonitor.periodic(1.0); Assertions.assertFalse(exampleMonitor.isFaulted()); Assertions.assertTrue(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 0); exampleMonitor.periodic(1.25); Assertions.assertFalse(exampleMonitor.isFaulted()); Assertions.assertTrue(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 0); exampleMonitor.periodic(2.0); Assertions.assertTrue(exampleMonitor.isFaulted()); Assertions.assertTrue(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 1); isStateValid = true; exampleMonitor.periodic(3.0); Assertions.assertFalse(exampleMonitor.isFaulted()); Assertions.assertFalse(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 1); } @Test public void stickyTest() { // Mimics a changing robot state isStateValid = true; + faultCallbackCalls = 0; Monitor exampleMonitor = new Monitor.MonitorBuilder() @@ -62,30 +76,38 @@ public void stickyTest() { .withStickyness(true) .withIsStateValidSupplier(() -> getIsStateValid()) .withTimeToFault(1.0) - .withFaultCallback(() -> {}) + .withFaultCallback( + () -> { + callFaultCallback(); + }) .build(); exampleMonitor.periodic(0.0); Assertions.assertFalse(exampleMonitor.isFaulted()); Assertions.assertFalse(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 0); isStateValid = false; exampleMonitor.periodic(1.0); Assertions.assertFalse(exampleMonitor.isFaulted()); Assertions.assertFalse(getIsStateValid()); Assertions.assertTrue(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 0); exampleMonitor.periodic(1.25); Assertions.assertFalse(exampleMonitor.isFaulted()); Assertions.assertTrue(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 0); exampleMonitor.periodic(2.0); Assertions.assertTrue(exampleMonitor.isFaulted()); Assertions.assertTrue(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 1); isStateValid = true; exampleMonitor.periodic(3.0); Assertions.assertTrue(exampleMonitor.isFaulted()); Assertions.assertFalse(exampleMonitor.isTriggered()); + Assertions.assertTrue(faultCallbackCalls == 2); } }