blob: 81f3be31b8e1404b46f47804b7c7e6a4d1f9efe3 [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));
45 factory.Run();
46 EXPECT_EQ(2, iteration);
47 }
48 ASSERT_EQ(starting_count + 1, completed_test_count());
49 ASSERT_TRUE(pong_fetcher.Fetch());
50 ASSERT_EQ(value, pong_fetcher->value());
51}
52
53} // namespace
54
Brian Silverman90221f82022-08-22 23:46:09 -070055TEST(EventLoopRustTest, TestApplicationOnce) {
56 MakeAndTestApplication(971, &make_test_application);
57}
Brian Silverman9809c5f2022-07-23 16:12:23 -070058
59TEST(EventLoopRustTest, TestApplicationTwice) {
Brian Silverman90221f82022-08-22 23:46:09 -070060 MakeAndTestApplication(971, &make_test_application);
61 MakeAndTestApplication(254, &make_test_application);
62}
63
64TEST(EventLoopRustTest, TestTypedApplicationTwice) {
65 MakeAndTestApplication(971, &make_typed_test_application);
66 MakeAndTestApplication(254, &make_typed_test_application);
Brian Silverman9809c5f2022-07-23 16:12:23 -070067}
68
69} // namespace aos::events::testing