blob: 10ff99691a127393952798ddae344068f9ea4b9c [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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
18using namespace frc;
19
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080020#ifdef __APPLE__
21TEST(WatchdogTest, DISABLED_EnableDisable) {
22#else
Brian Silverman41cdd3e2019-01-19 19:48:58 -080023TEST(WatchdogTest, EnableDisable) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080024#endif
Brian Silverman41cdd3e2019-01-19 19:48:58 -080025 uint32_t watchdogCounter = 0;
26
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080027 Watchdog watchdog(0.4_s, [&] { watchdogCounter++; });
Brian Silverman41cdd3e2019-01-19 19:48:58 -080028
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 Kuszmaul4f3ad3c2019-12-01 16:35:21 -080055#ifdef __APPLE__
56TEST(WatchdogTest, DISABLED_Reset) {
57#else
Brian Silverman41cdd3e2019-01-19 19:48:58 -080058TEST(WatchdogTest, Reset) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080059#endif
Brian Silverman41cdd3e2019-01-19 19:48:58 -080060 uint32_t watchdogCounter = 0;
61
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080062 Watchdog watchdog(0.4_s, [&] { watchdogCounter++; });
Brian Silverman41cdd3e2019-01-19 19:48:58 -080063
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 Kuszmaul4f3ad3c2019-12-01 16:35:21 -080073#ifdef __APPLE__
74TEST(WatchdogTest, DISABLED_SetTimeout) {
75#else
Brian Silverman41cdd3e2019-01-19 19:48:58 -080076TEST(WatchdogTest, SetTimeout) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080077#endif
Brian Silverman41cdd3e2019-01-19 19:48:58 -080078 uint32_t watchdogCounter = 0;
79
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080080 Watchdog watchdog(1.0_s, [&] { watchdogCounter++; });
Brian Silverman41cdd3e2019-01-19 19:48:58 -080081
82 watchdog.Enable();
83 std::this_thread::sleep_for(std::chrono::milliseconds(200));
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080084 watchdog.SetTimeout(0.2_s);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080085
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 Kuszmaul4f3ad3c2019-12-01 16:35:21 -080096#ifdef __APPLE__
97TEST(WatchdogTest, DISABLED_IsExpired) {
98#else
Brian Silverman41cdd3e2019-01-19 19:48:58 -080099TEST(WatchdogTest, IsExpired) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800100#endif
101 Watchdog watchdog(0.2_s, [] {});
Brian Silverman60246092019-03-02 13:29:58 -0800102 EXPECT_FALSE(watchdog.IsExpired());
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800103 watchdog.Enable();
104
105 EXPECT_FALSE(watchdog.IsExpired());
106 std::this_thread::sleep_for(std::chrono::milliseconds(300));
107 EXPECT_TRUE(watchdog.IsExpired());
Brian Silverman60246092019-03-02 13:29:58 -0800108
109 watchdog.Disable();
110 EXPECT_TRUE(watchdog.IsExpired());
111
112 watchdog.Reset();
113 EXPECT_FALSE(watchdog.IsExpired());
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800114}
115
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800116#ifdef __APPLE__
117TEST(WatchdogTest, DISABLED_Epochs) {
118#else
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800119TEST(WatchdogTest, Epochs) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800120#endif
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800121 uint32_t watchdogCounter = 0;
122
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800123 Watchdog watchdog(0.4_s, [&] { watchdogCounter++; });
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800124
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 Silverman60246092019-03-02 13:29:58 -0800148#ifdef __APPLE__
149TEST(WatchdogTest, DISABLED_MultiWatchdog) {
150#else
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800151TEST(WatchdogTest, MultiWatchdog) {
Brian Silverman60246092019-03-02 13:29:58 -0800152#endif
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800153 uint32_t watchdogCounter1 = 0;
154 uint32_t watchdogCounter2 = 0;
155
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800156 Watchdog watchdog1(0.2_s, [&] { watchdogCounter1++; });
157 Watchdog watchdog2(0.6_s, [&] { watchdogCounter2++; });
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800158
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}