Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/Notifier.h" // NOLINT(build/include_order) |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 7 | #include <fmt/core.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | #include "frc/Timer.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 12 | TEST(NotifierTest, StartPeriodicAndStop) { |
| 13 | uint32_t counter = 0; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 15 | frc::Notifier notifier{[&] { ++counter; }}; |
| 16 | notifier.StartPeriodic(1_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 18 | frc::Wait(10.5_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 19 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 20 | notifier.Stop(); |
| 21 | EXPECT_EQ(10u, counter) << "Received " << counter |
| 22 | << " notifications in 10.5 seconds\n"; |
| 23 | fmt::print("Received {} notifications in 10.5 seconds\n", counter); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 24 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 25 | frc::Wait(3_s); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 26 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 27 | EXPECT_EQ(10u, counter) << "Received " << counter - 10 |
| 28 | << " notifications in 3 seconds\n"; |
| 29 | fmt::print("Received {} notifications in 3 seconds\n", counter - 10); |
| 30 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 32 | TEST(NotifierTest, StartSingle) { |
| 33 | uint32_t counter = 0; |
| 34 | |
| 35 | frc::Notifier notifier{[&] { ++counter; }}; |
| 36 | notifier.StartSingle(1_s); |
| 37 | |
| 38 | frc::Wait(10.5_s); |
| 39 | |
| 40 | EXPECT_EQ(1u, counter) << "Received " << counter |
| 41 | << " notifications in 10.5 seconds\n"; |
| 42 | fmt::print("Received {} notifications in 10.5 seconds\n", counter); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 43 | } |