Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/Watchdog.h" // NOLINT(build/include_order) |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <thread> |
| 13 | |
| 14 | #include <wpi/raw_ostream.h> |
| 15 | |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | using namespace frc; |
| 19 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 20 | #ifdef __APPLE__ |
| 21 | TEST(WatchdogTest, DISABLED_EnableDisable) { |
| 22 | #else |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 23 | TEST(WatchdogTest, EnableDisable) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 24 | #endif |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 25 | uint32_t watchdogCounter = 0; |
| 26 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 27 | Watchdog watchdog(0.4_s, [&] { watchdogCounter++; }); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 28 | |
| 29 | wpi::outs() << "Run 1\n"; |
| 30 | watchdog.Enable(); |
| 31 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 32 | watchdog.Disable(); |
| 33 | |
| 34 | EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early"; |
| 35 | |
| 36 | wpi::outs() << "Run 2\n"; |
| 37 | watchdogCounter = 0; |
| 38 | watchdog.Enable(); |
| 39 | std::this_thread::sleep_for(std::chrono::milliseconds(600)); |
| 40 | watchdog.Disable(); |
| 41 | |
| 42 | EXPECT_EQ(1u, watchdogCounter) |
| 43 | << "Watchdog either didn't trigger or triggered more than once"; |
| 44 | |
| 45 | wpi::outs() << "Run 3\n"; |
| 46 | watchdogCounter = 0; |
| 47 | watchdog.Enable(); |
| 48 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); |
| 49 | watchdog.Disable(); |
| 50 | |
| 51 | EXPECT_EQ(1u, watchdogCounter) |
| 52 | << "Watchdog either didn't trigger or triggered more than once"; |
| 53 | } |
| 54 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 55 | #ifdef __APPLE__ |
| 56 | TEST(WatchdogTest, DISABLED_Reset) { |
| 57 | #else |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 58 | TEST(WatchdogTest, Reset) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 59 | #endif |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 60 | uint32_t watchdogCounter = 0; |
| 61 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 62 | Watchdog watchdog(0.4_s, [&] { watchdogCounter++; }); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 63 | |
| 64 | watchdog.Enable(); |
| 65 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 66 | watchdog.Reset(); |
| 67 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 68 | watchdog.Disable(); |
| 69 | |
| 70 | EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early"; |
| 71 | } |
| 72 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 73 | #ifdef __APPLE__ |
| 74 | TEST(WatchdogTest, DISABLED_SetTimeout) { |
| 75 | #else |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 76 | TEST(WatchdogTest, SetTimeout) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 77 | #endif |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 78 | uint32_t watchdogCounter = 0; |
| 79 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 80 | Watchdog watchdog(1.0_s, [&] { watchdogCounter++; }); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 81 | |
| 82 | watchdog.Enable(); |
| 83 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 84 | watchdog.SetTimeout(0.2_s); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 85 | |
| 86 | EXPECT_EQ(0.2, watchdog.GetTimeout()); |
| 87 | EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early"; |
| 88 | |
| 89 | std::this_thread::sleep_for(std::chrono::milliseconds(300)); |
| 90 | watchdog.Disable(); |
| 91 | |
| 92 | EXPECT_EQ(1u, watchdogCounter) |
| 93 | << "Watchdog either didn't trigger or triggered more than once"; |
| 94 | } |
| 95 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 96 | #ifdef __APPLE__ |
| 97 | TEST(WatchdogTest, DISABLED_IsExpired) { |
| 98 | #else |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 99 | TEST(WatchdogTest, IsExpired) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 100 | #endif |
| 101 | Watchdog watchdog(0.2_s, [] {}); |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame] | 102 | EXPECT_FALSE(watchdog.IsExpired()); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 103 | watchdog.Enable(); |
| 104 | |
| 105 | EXPECT_FALSE(watchdog.IsExpired()); |
| 106 | std::this_thread::sleep_for(std::chrono::milliseconds(300)); |
| 107 | EXPECT_TRUE(watchdog.IsExpired()); |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame] | 108 | |
| 109 | watchdog.Disable(); |
| 110 | EXPECT_TRUE(watchdog.IsExpired()); |
| 111 | |
| 112 | watchdog.Reset(); |
| 113 | EXPECT_FALSE(watchdog.IsExpired()); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 114 | } |
| 115 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 116 | #ifdef __APPLE__ |
| 117 | TEST(WatchdogTest, DISABLED_Epochs) { |
| 118 | #else |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 119 | TEST(WatchdogTest, Epochs) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 120 | #endif |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 121 | uint32_t watchdogCounter = 0; |
| 122 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 123 | Watchdog watchdog(0.4_s, [&] { watchdogCounter++; }); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 124 | |
| 125 | wpi::outs() << "Run 1\n"; |
| 126 | watchdog.Enable(); |
| 127 | watchdog.AddEpoch("Epoch 1"); |
| 128 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 129 | watchdog.AddEpoch("Epoch 2"); |
| 130 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 131 | watchdog.AddEpoch("Epoch 3"); |
| 132 | watchdog.Disable(); |
| 133 | |
| 134 | EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early"; |
| 135 | |
| 136 | wpi::outs() << "Run 2\n"; |
| 137 | watchdog.Enable(); |
| 138 | watchdog.AddEpoch("Epoch 1"); |
| 139 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 140 | watchdog.Reset(); |
| 141 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 142 | watchdog.AddEpoch("Epoch 2"); |
| 143 | watchdog.Disable(); |
| 144 | |
| 145 | EXPECT_EQ(0u, watchdogCounter) << "Watchdog triggered early"; |
| 146 | } |
| 147 | |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame] | 148 | #ifdef __APPLE__ |
| 149 | TEST(WatchdogTest, DISABLED_MultiWatchdog) { |
| 150 | #else |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 151 | TEST(WatchdogTest, MultiWatchdog) { |
Brian Silverman | 6024609 | 2019-03-02 13:29:58 -0800 | [diff] [blame] | 152 | #endif |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 153 | uint32_t watchdogCounter1 = 0; |
| 154 | uint32_t watchdogCounter2 = 0; |
| 155 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 156 | Watchdog watchdog1(0.2_s, [&] { watchdogCounter1++; }); |
| 157 | Watchdog watchdog2(0.6_s, [&] { watchdogCounter2++; }); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 158 | |
| 159 | watchdog2.Enable(); |
| 160 | std::this_thread::sleep_for(std::chrono::milliseconds(200)); |
| 161 | EXPECT_EQ(0u, watchdogCounter1) << "Watchdog triggered early"; |
| 162 | EXPECT_EQ(0u, watchdogCounter2) << "Watchdog triggered early"; |
| 163 | |
| 164 | // Sleep enough such that only the watchdog enabled later times out first |
| 165 | watchdog1.Enable(); |
| 166 | std::this_thread::sleep_for(std::chrono::milliseconds(300)); |
| 167 | watchdog1.Disable(); |
| 168 | watchdog2.Disable(); |
| 169 | |
| 170 | EXPECT_EQ(1u, watchdogCounter1) |
| 171 | << "Watchdog either didn't trigger or triggered more than once"; |
| 172 | EXPECT_EQ(0u, watchdogCounter2) << "Watchdog triggered early"; |
| 173 | } |