blob: 8557e552e61c95aa66a0ff0e7e792f0b821e2b5b [file] [log] [blame]
Brian Silverman9809c5f2022-07-23 16:12:23 -07001#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
9namespace aos::events::testing {
10namespace {
11
Brian Silverman90221f82022-08-22 23:46:09 -070012template <typename F>
13void MakeAndTestApplication(int value, F constructor) {
Brian Silverman9809c5f2022-07-23 16:12:23 -070014 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 Silverman90221f82022-08-22 23:46:09 -070024 auto test_application = constructor(rust_event_loop.get());
Brian Silverman9809c5f2022-07-23 16:12:23 -070025 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 Silverman76f48362022-08-24 21:09:08 -070045 ASSERT_EQ(starting_count, started_test_count());
Brian Silverman9809c5f2022-07-23 16:12:23 -070046 factory.Run();
Brian Silverman76f48362022-08-24 21:09:08 -070047 ASSERT_EQ(starting_count + 1, started_test_count());
Brian Silverman9809c5f2022-07-23 16:12:23 -070048 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 Silverman90221f82022-08-22 23:46:09 -070057TEST(EventLoopRustTest, TestApplicationOnce) {
58 MakeAndTestApplication(971, &make_test_application);
59}
Brian Silverman9809c5f2022-07-23 16:12:23 -070060
61TEST(EventLoopRustTest, TestApplicationTwice) {
Brian Silverman90221f82022-08-22 23:46:09 -070062 MakeAndTestApplication(971, &make_test_application);
63 MakeAndTestApplication(254, &make_test_application);
64}
65
66TEST(EventLoopRustTest, TestTypedApplicationTwice) {
67 MakeAndTestApplication(971, &make_typed_test_application);
68 MakeAndTestApplication(254, &make_typed_test_application);
Brian Silverman9809c5f2022-07-23 16:12:23 -070069}
70
Brian Silverman1431a772022-08-31 20:44:36 -070071TEST(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
81TEST(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 Silverman9809c5f2022-07-23 16:12:23 -070091} // namespace aos::events::testing