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