mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
@@ -6,34 +6,38 @@
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user