Skip to content

Commit

Permalink
Add tests for monitor fault callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
aidnem committed Nov 25, 2024
1 parent 35f0f5d commit f84a43c
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions monitors/src/test/java/MonitorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
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;

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;
Expand All @@ -25,67 +30,84 @@ 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()
.withName("exampleMonitor")
.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);
}
}

0 comments on commit f84a43c

Please sign in to comment.