diff --git a/wpilibcIntegrationTests/src/main/native/cpp/NotifierTest.cpp b/wpilibcIntegrationTests/src/main/native/cpp/NotifierTest.cpp index 04c181b2e6..e613007019 100644 --- a/wpilibcIntegrationTests/src/main/native/cpp/NotifierTest.cpp +++ b/wpilibcIntegrationTests/src/main/native/cpp/NotifierTest.cpp @@ -6,34 +6,38 @@ #include -#include "TestBench.h" #include "frc/Timer.h" #include "gtest/gtest.h" -unsigned notifierCounter; +TEST(NotifierTest, TestStartPeriodicAndStop) { + uint32_t counter = 0; -void notifierHandler(void*) { - notifierCounter++; -} - -/** - * Test if the Wait function works - */ -TEST(NotifierTest, DISABLED_TestTimerNotifications) { - fmt::print("NotifierTest...\n"); - notifierCounter = 0; - fmt::print("notifier(notifierHandler, nullptr)...\n"); - frc::Notifier notifier(notifierHandler, nullptr); - fmt::print("Start Periodic...\n"); + frc::Notifier notifier{[&] { ++counter; }}; notifier.StartPeriodic(1_s); - fmt::print("Wait...\n"); frc::Wait(10.5_s); - fmt::print("...Wait\n"); - EXPECT_EQ(10u, notifierCounter) - << "Received " << notifierCounter << " notifications in 10.5 seconds"; - fmt::print("Received {} notifications in 10.5 seconds", notifierCounter); + notifier.Stop(); + EXPECT_EQ(10u, counter) << "Received " << counter + << " notifications in 10.5 seconds\n"; + fmt::print("Received {} notifications in 10.5 seconds\n", counter); - fmt::print("...NotifierTest\n"); + frc::Wait(3_s); + + EXPECT_EQ(10u, counter) << "Received " << counter - 10 + << " notifications in 3 seconds\n"; + fmt::print("Received {} notifications in 3 seconds\n", counter - 10); +} + +TEST(NotifierTest, TestStartSingle) { + uint32_t counter = 0; + + frc::Notifier notifier{[&] { ++counter; }}; + notifier.StartSingle(1_s); + + frc::Wait(10.5_s); + + EXPECT_EQ(1u, counter) << "Received " << counter + << " notifications in 10.5 seconds\n"; + fmt::print("Received {} notifications in 10.5 seconds\n", counter); } diff --git a/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/NotifierTest.java b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/NotifierTest.java new file mode 100644 index 0000000000..2f2ca74f58 --- /dev/null +++ b/wpilibjIntegrationTests/src/main/java/edu/wpi/first/wpilibj/NotifierTest.java @@ -0,0 +1,56 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +package edu.wpi.first.wpilibj; + +import static org.junit.Assert.assertEquals; + +import edu.wpi.first.wpilibj.test.AbstractComsSetup; +import java.util.logging.Logger; +import org.junit.Test; + +/** Tests to see if the Notifier is working properly. */ +public class NotifierTest extends AbstractComsSetup { + private static final Logger logger = Logger.getLogger(NotifierTest.class.getName()); + private static int counter = 0; + + @Override + protected Logger getClassLogger() { + return logger; + } + + @Test + public void testStartPeriodicAndStop() { + counter = 0; + Notifier notifier = new Notifier(() -> ++counter); + notifier.startPeriodic(1.0); + + Timer.delay(10.5); + + notifier.stop(); + assertEquals("Received " + counter + " notifications in 10.5 seconds\n", 10, counter); + System.out.println("Received " + counter + " notifications in 10.5 seconds"); + + Timer.delay(3.0); + + assertEquals("Received " + (counter - 10) + " notifications in 3 seconds\n", 10, counter); + System.out.println("Received " + (counter - 10) + " notifications in 3 seconds"); + + notifier.close(); + } + + @Test + public void testStartSingle() { + counter = 0; + Notifier notifier = new Notifier(() -> ++counter); + notifier.startSingle(1.0); + + Timer.delay(10.5); + + assertEquals("Received " + counter + " notifications in 10.5 seconds\n", 1, counter); + System.out.println("Received " + counter + " notifications in 10.5 seconds"); + + notifier.close(); + } +}