blob: c27a27f1f684133ab7676ec3f0224b706879c369 [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
12void MakeAndTestApplication(int value) {
13 const int32_t starting_count = completed_test_count();
14 const aos::FlatbufferDetachedBuffer<aos::Configuration> config =
15 aos::configuration::ReadConfig(
16 aos::testing::ArtifactPath("aos/events/pingpong_config.json"));
17 SimulatedEventLoopFactory factory{&config.message()};
18 const auto ping_event_loop = factory.MakeEventLoop("ping");
19 auto ping_sender = ping_event_loop->MakeSender<examples::Ping>("/test");
20 auto pong_fetcher = ping_event_loop->MakeFetcher<examples::Pong>("/test");
21 const auto rust_event_loop = factory.MakeEventLoop("pong");
22 {
23 auto test_application = make_test_application(rust_event_loop.get());
24 int iteration = 0;
25 ping_event_loop
26 ->AddTimer([&]() {
27 if (iteration++ > 0) {
28 test_application->after_sending();
29 factory.Exit();
30 return;
31 }
32 test_application->before_sending();
33 ASSERT_FALSE(pong_fetcher.Fetch());
34 {
35 auto builder = ping_sender.MakeBuilder();
36 examples::Ping::Builder ping(*builder.fbb());
37 ping.add_value(value);
38 builder.CheckOk(builder.Send(ping.Finish()));
39 }
40 })
41 ->Setup(
42 ping_event_loop->monotonic_now() + std::chrono::milliseconds(10),
43 std::chrono::milliseconds(10));
44 factory.Run();
45 EXPECT_EQ(2, iteration);
46 }
47 ASSERT_EQ(starting_count + 1, completed_test_count());
48 ASSERT_TRUE(pong_fetcher.Fetch());
49 ASSERT_EQ(value, pong_fetcher->value());
50}
51
52} // namespace
53
54TEST(EventLoopRustTest, TestApplicationOnce) { MakeAndTestApplication(971); }
55
56TEST(EventLoopRustTest, TestApplicationTwice) {
57 MakeAndTestApplication(971);
58 MakeAndTestApplication(254);
59}
60
61} // namespace aos::events::testing