Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 1 | #include "frc971/wpilib/loop_output_handler.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/simulated_event_loop.h" |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 8 | #include "aos/logging/logging.h" |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 9 | #include "aos/testing/test_logging.h" |
| 10 | #include "aos/time/time.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include "frc971/wpilib/loop_output_handler_test_generated.h" |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 12 | |
| 13 | namespace frc971 { |
| 14 | namespace wpilib { |
| 15 | namespace testing { |
| 16 | namespace { |
| 17 | namespace chrono = ::std::chrono; |
| 18 | using ::aos::monotonic_clock; |
| 19 | } // namespace |
| 20 | |
| 21 | class LoopOutputHandlerTest : public ::testing::Test { |
| 22 | public: |
| 23 | LoopOutputHandlerTest() |
| 24 | : ::testing::Test(), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | configuration_(aos::configuration::MergeConfiguration( |
| 26 | aos::FlatbufferDetachedBuffer<aos::Configuration>( |
| 27 | aos::JsonToFlatbuffer( |
| 28 | "{\n" |
| 29 | " \"channels\": [ \n" |
| 30 | " {\n" |
| 31 | " \"name\": \"/test\",\n" |
| 32 | " \"type\": " |
| 33 | "\"frc971.wpilib.LoopOutputHandlerTestOutput\"\n" |
| 34 | " }\n" |
| 35 | " ]\n" |
| 36 | "}\n", |
| 37 | aos::Configuration::MiniReflectTypeTable())))), |
| 38 | event_loop_factory_(&configuration_.message()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame^] | 39 | loop_output_hander_event_loop_( |
| 40 | event_loop_factory_.MakeEventLoop("output")), |
| 41 | test_event_loop_(event_loop_factory_.MakeEventLoop("test")) { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 42 | ::aos::testing::EnableTestLogging(); |
| 43 | } |
| 44 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 45 | aos::FlatbufferDetachedBuffer<aos::Configuration> configuration_; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 46 | ::aos::SimulatedEventLoopFactory event_loop_factory_; |
| 47 | ::std::unique_ptr<::aos::EventLoop> loop_output_hander_event_loop_; |
| 48 | ::std::unique_ptr<::aos::EventLoop> test_event_loop_; |
| 49 | }; |
| 50 | |
| 51 | // Test loop output handler which logs and counts. |
| 52 | class TestLoopOutputHandler |
| 53 | : public LoopOutputHandler<LoopOutputHandlerTestOutput> { |
| 54 | public: |
| 55 | TestLoopOutputHandler(::aos::EventLoop *event_loop, const ::std::string &name) |
| 56 | : LoopOutputHandler(event_loop, name) {} |
| 57 | |
| 58 | ~TestLoopOutputHandler() { Stop(); } |
| 59 | |
| 60 | int count() const { return count_; } |
| 61 | |
| 62 | ::aos::monotonic_clock::time_point last_time() const { return last_time_; } |
| 63 | ::aos::monotonic_clock::time_point stop_time() const { return stop_time_; } |
| 64 | |
| 65 | protected: |
| 66 | void Write(const LoopOutputHandlerTestOutput &output) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | LOG(INFO) << "output " << aos::FlatbufferToJson(&output); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 68 | ++count_; |
| 69 | last_time_ = event_loop()->monotonic_now(); |
| 70 | } |
| 71 | |
| 72 | void Stop() override { |
| 73 | stop_time_ = event_loop()->monotonic_now(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | LOG(INFO) << "Stopping"; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private: |
| 78 | int count_ = 0; |
| 79 | |
| 80 | ::aos::monotonic_clock::time_point last_time_ = |
| 81 | ::aos::monotonic_clock::min_time; |
| 82 | ::aos::monotonic_clock::time_point stop_time_ = |
| 83 | ::aos::monotonic_clock::min_time; |
| 84 | }; |
| 85 | |
| 86 | // Test that the watchdog calls Stop at the right time. |
| 87 | TEST_F(LoopOutputHandlerTest, WatchdogTest) { |
| 88 | TestLoopOutputHandler loop_output(loop_output_hander_event_loop_.get(), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | "/test"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 90 | |
| 91 | ::aos::Sender<LoopOutputHandlerTestOutput> output_sender = |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | test_event_loop_->MakeSender<LoopOutputHandlerTestOutput>("/test"); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 93 | |
| 94 | const monotonic_clock::time_point start_time = |
| 95 | test_event_loop_->monotonic_now(); |
| 96 | |
| 97 | int count = 0; |
| 98 | // Send outputs for 1 second. |
| 99 | ::aos::TimerHandler *timer_handle = test_event_loop_->AddTimer( |
| 100 | [this, &start_time, &output_sender, &loop_output, &count]() { |
| 101 | EXPECT_EQ(count, loop_output.count()); |
| 102 | if (test_event_loop_->monotonic_now() < |
| 103 | start_time + chrono::seconds(1)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 104 | auto builder = output_sender.MakeBuilder(); |
| 105 | LoopOutputHandlerTestOutput::Builder output_builder = |
| 106 | builder.MakeBuilder<LoopOutputHandlerTestOutput>(); |
| 107 | output_builder.add_voltage(5.0); |
| 108 | EXPECT_TRUE(builder.Send(output_builder.Finish())); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 109 | |
| 110 | ++count; |
| 111 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 112 | LOG(INFO) << "Ping"; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 113 | }); |
| 114 | |
| 115 | // Kick off the ping timer handler. |
| 116 | test_event_loop_->OnRun([this, &timer_handle]() { |
| 117 | timer_handle->Setup(test_event_loop_->monotonic_now(), |
| 118 | chrono::milliseconds(5)); |
| 119 | }); |
| 120 | |
| 121 | event_loop_factory_.RunFor(chrono::seconds(2)); |
| 122 | |
| 123 | // Confirm the watchdog |
| 124 | EXPECT_EQ(loop_output.stop_time(), |
| 125 | loop_output.last_time() + chrono::milliseconds(100)); |
| 126 | } |
| 127 | |
| 128 | } // namespace testing |
| 129 | } // namespace wpilib |
| 130 | } // namespace frc971 |