Skip to content

Commit

Permalink
added some more unit tests to java
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-yes-0-fps committed Sep 26, 2024
1 parent a536db3 commit c1bcc63
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 8 deletions.
18 changes: 14 additions & 4 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static final class TracerState {
* If the cycle is poisened, it will warn the user
* and not publish any data.
*/
boolean m_cyclePoisened = false;
boolean m_cyclePoisoned = false;

/** If the tracer is disabled, it will not publish any data or do any string manipulation. */
boolean m_disabled = false;
Expand Down Expand Up @@ -150,8 +150,8 @@ private String popTraceStack() {
if (m_disabled) {
return "";
}
if (m_traceStack.isEmpty() || m_traceStackHistory.isEmpty() || m_cyclePoisened) {
m_cyclePoisened = true;
if (m_traceStack.isEmpty() || m_traceStackHistory.isEmpty() || m_cyclePoisoned) {
m_cyclePoisoned = true;
return "";
}
m_traceStack.remove(m_traceStack.size() - 1);
Expand All @@ -167,7 +167,7 @@ private double totalGCTime() {
}

private void endCycle() {
if (m_disabled != m_disableNextCycle || m_cyclePoisened) {
if (m_disabled != m_disableNextCycle || m_cyclePoisoned) {
// Gives publishers empty times,
// reporting no data is better than bad data
for (var publisher : m_publishers.entrySet()) {
Expand Down Expand Up @@ -345,6 +345,16 @@ public static <R> R traceFunc(String name, Supplier<R> supplier) {
return ret;
}

/**
* This function is only to be used in tests
* and is package private to prevent misuse.
*/
static void resetForTest() {
threadLocalState.remove();
singleThreadedMode.set(false);
anyTracesStarted.set(false);
}

// A REIMPLEMENTATION OF THE OLD TRACER TO NOT BREAK OLD CODE

private static final long kMinPrintPeriod = 1000000; // microseconds
Expand Down
107 changes: 103 additions & 4 deletions wpilibj/src/test/java/edu/wpi/first/wpilibj/TracerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package edu.wpi.first.wpilibj;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import edu.wpi.first.hal.HAL;
import edu.wpi.first.networktables.DoubleEntry;
Expand All @@ -20,6 +21,7 @@ class TracerTest {
void setup() {
HAL.initialize(500, 0);
SimHooks.pauseTiming();
Tracer.resetForTest();
}

@AfterEach
Expand All @@ -33,21 +35,118 @@ void traceFuncTest() {
final String threadName = Thread.currentThread().getName();
Tracer.disableGcLoggingForCurrentThread();

Tracer.startTrace("Test1");
Tracer.traceFunc("Test2", () -> SimHooks.stepTiming(0.4));
Tracer.startTrace("FuncTest1");
Tracer.traceFunc("FuncTest2", () -> SimHooks.stepTiming(0.4));
SimHooks.stepTiming(0.1);
Tracer.endTrace();

DoubleEntry test1Entry =
NetworkTableInstance.getDefault()
.getDoubleTopic("/Tracer/" + threadName + "/Test1")
.getDoubleTopic("/Tracer/" + threadName + "/FuncTest1")
.getEntry(0.0);
DoubleEntry test2Entry =
NetworkTableInstance.getDefault()
.getDoubleTopic("/Tracer/" + threadName + "/Test1/Test2")
.getDoubleTopic("/Tracer/" + threadName + "/FuncTest1/FuncTest2")
.getEntry(0.0);

assertEquals(500.0, test1Entry.get(), 1.0);
assertEquals(400.0, test2Entry.get(), 1.0);
}

@Test
@ResourceLock("timing")
void traceThreadTest() {
final String threadName = Thread.currentThread().getName();

Tracer.disableGcLoggingForCurrentThread();

// run a trace in the main thread, assert that the tracer ran
{
Tracer.startTrace("ThreadTest1");
SimHooks.stepTiming(0.1);
Tracer.endTrace();

DoubleEntry test1Entry =
NetworkTableInstance.getDefault()
.getDoubleTopic("/Tracer/" + threadName + "/ThreadTest1")
.getEntry(0.0);
assertEquals(100.0, test1Entry.get(), 1.0);
}

// run a trace in a new thread, assert that the tracer ran
// and that the output position and value are correct
{
final String newThreadName = "TestThread";
try {
Thread thread = new Thread(
() -> {
Tracer.disableGcLoggingForCurrentThread();
Tracer.startTrace("ThreadTest1");
SimHooks.stepTiming(0.4);
Tracer.endTrace();
},
newThreadName
);
thread.start();
thread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

DoubleEntry test2Entry =
NetworkTableInstance.getDefault()
.getDoubleTopic("/Tracer/" + newThreadName + "/ThreadTest1")
.getEntry(0.0);
assertEquals(400.0, test2Entry.get(), 1.0);
}
}

@Test
@ResourceLock("timing")
void traceSingleThreadTest() {
final String newThreadName = "TestThread";

// start a trace in the main thread, assert that the tracer ran
// and that the thread name is not in the trace path
{
Tracer.enableSingleThreadedMode();
Tracer.disableGcLoggingForCurrentThread();

Tracer.startTrace("SingleThreadTest1");
SimHooks.stepTiming(0.1);
Tracer.endTrace();

DoubleEntry test1Entry = NetworkTableInstance.getDefault()
.getDoubleTopic("/Tracer/SingleThreadTest1")
.getEntry(0.0);
assertEquals(100.0, test1Entry.get(), 1.0);
}

// start a trace in a new thread after enabling single threaded mode,
// this should disable the tracer on the new thread, assert that the tracer did not run
{
try {
Thread thread = new Thread(
() -> {
Tracer.disableGcLoggingForCurrentThread();
Tracer.startTrace("SingleThreadTest1");
SimHooks.stepTiming(0.4);
Tracer.endTrace();
},
newThreadName
);
thread.start();
thread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

boolean test2EntryExists =
NetworkTableInstance.getDefault()
.getDoubleTopic("/Tracer/" + newThreadName + "/SingleThreadTest1")
.exists();

assertTrue(!test2EntryExists);
}
}
}

0 comments on commit c1bcc63

Please sign in to comment.