Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 1 | #include "absl/log/check.h" |
| 2 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 3 | #include "gtest/gtest.h" |
| 4 | |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 5 | #include "aos/events/event_loop_runtime_test_lib_rs_cxxgen.h" |
| 6 | #include "aos/events/ping_generated.h" |
| 7 | #include "aos/events/pong_generated.h" |
| 8 | #include "aos/events/simulated_event_loop.h" |
| 9 | #include "aos/testing/path.h" |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 10 | |
| 11 | namespace aos::events::testing { |
| 12 | namespace { |
| 13 | |
Brian Silverman | 90221f8 | 2022-08-22 23:46:09 -0700 | [diff] [blame] | 14 | template <typename F> |
| 15 | void MakeAndTestApplication(int value, F constructor) { |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 16 | const int32_t starting_count = completed_test_count(); |
| 17 | const aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 18 | aos::configuration::ReadConfig( |
| 19 | aos::testing::ArtifactPath("aos/events/pingpong_config.json")); |
| 20 | SimulatedEventLoopFactory factory{&config.message()}; |
| 21 | const auto ping_event_loop = factory.MakeEventLoop("ping"); |
| 22 | auto ping_sender = ping_event_loop->MakeSender<examples::Ping>("/test"); |
| 23 | auto pong_fetcher = ping_event_loop->MakeFetcher<examples::Pong>("/test"); |
| 24 | const auto rust_event_loop = factory.MakeEventLoop("pong"); |
| 25 | { |
Brian Silverman | 90221f8 | 2022-08-22 23:46:09 -0700 | [diff] [blame] | 26 | auto test_application = constructor(rust_event_loop.get()); |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 27 | int iteration = 0; |
| 28 | ping_event_loop |
| 29 | ->AddTimer([&]() { |
| 30 | if (iteration++ > 0) { |
| 31 | test_application->after_sending(); |
| 32 | factory.Exit(); |
| 33 | return; |
| 34 | } |
| 35 | test_application->before_sending(); |
| 36 | ASSERT_FALSE(pong_fetcher.Fetch()); |
| 37 | { |
| 38 | auto builder = ping_sender.MakeBuilder(); |
| 39 | examples::Ping::Builder ping(*builder.fbb()); |
| 40 | ping.add_value(value); |
| 41 | builder.CheckOk(builder.Send(ping.Finish())); |
| 42 | } |
| 43 | }) |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 44 | ->Schedule( |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 45 | ping_event_loop->monotonic_now() + std::chrono::milliseconds(10), |
| 46 | std::chrono::milliseconds(10)); |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 47 | ASSERT_EQ(starting_count, started_test_count()); |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 48 | factory.Run(); |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 49 | ASSERT_EQ(starting_count + 1, started_test_count()); |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 50 | EXPECT_EQ(2, iteration); |
| 51 | } |
| 52 | ASSERT_EQ(starting_count + 1, completed_test_count()); |
| 53 | ASSERT_TRUE(pong_fetcher.Fetch()); |
| 54 | ASSERT_EQ(value, pong_fetcher->value()); |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
Brian Silverman | 90221f8 | 2022-08-22 23:46:09 -0700 | [diff] [blame] | 59 | TEST(EventLoopRustTest, TestApplicationOnce) { |
| 60 | MakeAndTestApplication(971, &make_test_application); |
| 61 | } |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 62 | |
| 63 | TEST(EventLoopRustTest, TestApplicationTwice) { |
Brian Silverman | 90221f8 | 2022-08-22 23:46:09 -0700 | [diff] [blame] | 64 | MakeAndTestApplication(971, &make_test_application); |
| 65 | MakeAndTestApplication(254, &make_test_application); |
| 66 | } |
| 67 | |
| 68 | TEST(EventLoopRustTest, TestTypedApplicationTwice) { |
| 69 | MakeAndTestApplication(971, &make_typed_test_application); |
| 70 | MakeAndTestApplication(254, &make_typed_test_application); |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Brian Silverman | 1431a77 | 2022-08-31 20:44:36 -0700 | [diff] [blame] | 73 | TEST(EventLoopRustDeathTest, PanicImmediately) { |
| 74 | const aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 75 | aos::configuration::ReadConfig( |
| 76 | aos::testing::ArtifactPath("aos/events/pingpong_config.json")); |
| 77 | SimulatedEventLoopFactory factory{&config.message()}; |
| 78 | const auto rust_event_loop = factory.MakeEventLoop("pong"); |
| 79 | EXPECT_DEATH(make_panic_application(rust_event_loop.get()), |
| 80 | "Test Rust panic.*Rust panic, aborting"); |
| 81 | } |
| 82 | |
| 83 | TEST(EventLoopRustDeathTest, PanicOnRun) { |
| 84 | const aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 85 | aos::configuration::ReadConfig( |
| 86 | aos::testing::ArtifactPath("aos/events/pingpong_config.json")); |
| 87 | SimulatedEventLoopFactory factory{&config.message()}; |
| 88 | const auto rust_event_loop = factory.MakeEventLoop("pong"); |
| 89 | auto application = make_panic_on_run_application(rust_event_loop.get()); |
| 90 | EXPECT_DEATH(factory.Run(), "Test Rust panic.*Rust panic, aborting"); |
| 91 | } |
| 92 | |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 93 | } // namespace aos::events::testing |