blob: fca1867e4ab6340df6675647db1b02c227bf8458 [file] [log] [blame]
Austin Schuh6b9c4152019-11-29 12:45:24 -08001#include "aos/events/ping_lib.h"
2#include "aos/events/pong_lib.h"
3#include "aos/events/simulated_event_loop.h"
4#include "aos/json_to_flatbuffer.h"
5#include "glog/logging.h"
6#include "gtest/gtest.h"
7
8namespace aos {
9namespace testing {
10
11namespace chrono = std::chrono;
12
13class PingPongTest : public ::testing::Test {
14 public:
15 PingPongTest()
16 : config_(aos::configuration::ReadConfig(
17 "aos/events/pingpong_config.json")),
18 event_loop_factory_(&config_.message()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080019 ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")),
Austin Schuh6b9c4152019-11-29 12:45:24 -080020 ping_(ping_event_loop_.get()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080021 pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")),
Austin Schuh6b9c4152019-11-29 12:45:24 -080022 pong_(pong_event_loop_.get()) {}
23
24 // Config and factory.
25 // The factory is a factory for connected event loops. Each application needs
26 // a separate event loop (because you can't send a message to yourself in a
27 // single event loop). The created event loops can then send messages to each
28 // other and trigger callbacks to be called, or fetchers to receive data.
29 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
30 SimulatedEventLoopFactory event_loop_factory_;
31
32 // Event loop and app for Ping
33 std::unique_ptr<EventLoop> ping_event_loop_;
34 Ping ping_;
35
36 // Event loop and app for Pong
37 std::unique_ptr<EventLoop> pong_event_loop_;
38 Pong pong_;
39};
40
41// Tests that we can startup at all. This confirms that the channels are all in
42// the config.
43TEST_F(PingPongTest, Starts) {
44 // RunFor lives in the factory because all the loops need to run together, and
45 // the factory is the only object that conceptually knows about all of the
46 // loops at once.
47 event_loop_factory_.RunFor(chrono::seconds(10));
48}
49
50// Tests that the number of pong messages matches the number of ping messages.
51TEST_F(PingPongTest, AlwaysReplies) {
52 std::unique_ptr<EventLoop> test_event_loop =
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080053 event_loop_factory_.MakeEventLoop("test");
Austin Schuh6b9c4152019-11-29 12:45:24 -080054
55 int ping_count = 0;
56 int pong_count = 0;
57
58 // Confirm that the ping value matches.
59 test_event_loop->MakeWatcher("/test",
60 [&ping_count](const examples::Ping &ping) {
61 EXPECT_EQ(ping.value(), ping_count + 1);
62 ++ping_count;
63 });
64 // Confirm that the ping and pong counts both match, and the value also
65 // matches.
66 test_event_loop->MakeWatcher(
67 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
68 EXPECT_EQ(pong.value(), pong_count + 1);
69 ++pong_count;
70 EXPECT_EQ(ping_count, pong_count);
71 });
72
73 event_loop_factory_.RunFor(chrono::seconds(10));
74
75 // We run at t=0 and t=10 seconds, which means we run 1 extra time.
76 EXPECT_EQ(ping_count, 1001);
77}
78
79} // namespace testing
80} // namespace aos