[sim] Change StepTiming to wait for notifiers (#2603)

Old behavior is available via StepTimingAsync.

This makes it significantly easier to use simulation timing with notifiers.

Also update tests to use simulation framework.  This also speeds up the
timing-dependent tests by using simulation timing.  ResourceLock is used
in the Java tests to prevent parallel execution.

While we're here, tweak HAL Notifier implementation:
- Use wait_for instead of wait_until in WaitForNotifierAlarm
- Check for triggerTime = UINT64_MAX in UpdateNotifierAlarm
This commit is contained in:
Peter Johnson
2020-09-27 13:27:53 -07:00
committed by GitHub
parent 62731bea20
commit c3b3fb8b74
23 changed files with 278 additions and 185 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
/* Copyright (c) 2018-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
@@ -9,26 +9,31 @@
#include <stdint.h>
#include <thread>
#include <wpi/raw_ostream.h>
#include "frc/simulation/SimHooks.h"
#include "gtest/gtest.h"
using namespace frc;
#ifdef __APPLE__
TEST(WatchdogTest, DISABLED_EnableDisable) {
#else
TEST(WatchdogTest, EnableDisable) {
#endif
namespace {
class WatchdogTest : public ::testing::Test {
protected:
void SetUp() override { frc::sim::PauseTiming(); }
void TearDown() override { frc::sim::ResumeTiming(); }
};
} // namespace
TEST_F(WatchdogTest, EnableDisable) {
uint32_t watchdogCounter = 0;
Watchdog watchdog(0.4_s, [&] { watchdogCounter++; });
wpi::outs() << "Run 1\n";
watchdog.Enable();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
frc::sim::StepTiming(0.2_s);
watchdog.Disable();
EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early";
@@ -36,7 +41,7 @@ TEST(WatchdogTest, EnableDisable) {
wpi::outs() << "Run 2\n";
watchdogCounter = 0;
watchdog.Enable();
std::this_thread::sleep_for(std::chrono::milliseconds(600));
frc::sim::StepTiming(0.6_s);
watchdog.Disable();
EXPECT_EQ(1u, watchdogCounter)
@@ -45,65 +50,53 @@ TEST(WatchdogTest, EnableDisable) {
wpi::outs() << "Run 3\n";
watchdogCounter = 0;
watchdog.Enable();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
frc::sim::StepTiming(1_s);
watchdog.Disable();
EXPECT_EQ(1u, watchdogCounter)
<< "Watchdog either didn't trigger or triggered more than once";
}
#ifdef __APPLE__
TEST(WatchdogTest, DISABLED_Reset) {
#else
TEST(WatchdogTest, Reset) {
#endif
TEST_F(WatchdogTest, Reset) {
uint32_t watchdogCounter = 0;
Watchdog watchdog(0.4_s, [&] { watchdogCounter++; });
watchdog.Enable();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
frc::sim::StepTiming(0.2_s);
watchdog.Reset();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
frc::sim::StepTiming(0.2_s);
watchdog.Disable();
EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early";
}
#ifdef __APPLE__
TEST(WatchdogTest, DISABLED_SetTimeout) {
#else
TEST(WatchdogTest, SetTimeout) {
#endif
TEST_F(WatchdogTest, SetTimeout) {
uint32_t watchdogCounter = 0;
Watchdog watchdog(1.0_s, [&] { watchdogCounter++; });
watchdog.Enable();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
frc::sim::StepTiming(0.2_s);
watchdog.SetTimeout(0.2_s);
EXPECT_EQ(0.2, watchdog.GetTimeout());
EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early";
std::this_thread::sleep_for(std::chrono::milliseconds(300));
frc::sim::StepTiming(0.3_s);
watchdog.Disable();
EXPECT_EQ(1u, watchdogCounter)
<< "Watchdog either didn't trigger or triggered more than once";
}
#ifdef __APPLE__
TEST(WatchdogTest, DISABLED_IsExpired) {
#else
TEST(WatchdogTest, IsExpired) {
#endif
TEST_F(WatchdogTest, IsExpired) {
Watchdog watchdog(0.2_s, [] {});
EXPECT_FALSE(watchdog.IsExpired());
watchdog.Enable();
EXPECT_FALSE(watchdog.IsExpired());
std::this_thread::sleep_for(std::chrono::milliseconds(300));
frc::sim::StepTiming(0.3_s);
EXPECT_TRUE(watchdog.IsExpired());
watchdog.Disable();
@@ -113,11 +106,7 @@ TEST(WatchdogTest, IsExpired) {
EXPECT_FALSE(watchdog.IsExpired());
}
#ifdef __APPLE__
TEST(WatchdogTest, DISABLED_Epochs) {
#else
TEST(WatchdogTest, Epochs) {
#endif
TEST_F(WatchdogTest, Epochs) {
uint32_t watchdogCounter = 0;
Watchdog watchdog(0.4_s, [&] { watchdogCounter++; });
@@ -125,9 +114,9 @@ TEST(WatchdogTest, Epochs) {
wpi::outs() << "Run 1\n";
watchdog.Enable();
watchdog.AddEpoch("Epoch 1");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
frc::sim::StepTiming(0.1_s);
watchdog.AddEpoch("Epoch 2");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
frc::sim::StepTiming(0.1_s);
watchdog.AddEpoch("Epoch 3");
watchdog.Disable();
@@ -136,20 +125,16 @@ TEST(WatchdogTest, Epochs) {
wpi::outs() << "Run 2\n";
watchdog.Enable();
watchdog.AddEpoch("Epoch 1");
std::this_thread::sleep_for(std::chrono::milliseconds(200));
frc::sim::StepTiming(0.2_s);
watchdog.Reset();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
frc::sim::StepTiming(0.2_s);
watchdog.AddEpoch("Epoch 2");
watchdog.Disable();
EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early";
}
#ifdef __APPLE__
TEST(WatchdogTest, DISABLED_MultiWatchdog) {
#else
TEST(WatchdogTest, MultiWatchdog) {
#endif
TEST_F(WatchdogTest, MultiWatchdog) {
uint32_t watchdogCounter1 = 0;
uint32_t watchdogCounter2 = 0;
@@ -157,13 +142,13 @@ TEST(WatchdogTest, MultiWatchdog) {
Watchdog watchdog2(0.6_s, [&] { watchdogCounter2++; });
watchdog2.Enable();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
frc::sim::StepTiming(0.25_s);
EXPECT_EQ(0u, watchdogCounter1) << "Watchdog triggered early";
EXPECT_EQ(0u, watchdogCounter2) << "Watchdog triggered early";
// Sleep enough such that only the watchdog enabled later times out first
watchdog1.Enable();
std::this_thread::sleep_for(std::chrono::milliseconds(300));
frc::sim::StepTiming(0.25_s);
watchdog1.Disable();
watchdog2.Disable();