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