blob: 0878e1292cd46e7d42628aa1dd86887e7f1b4cbd [file] [log] [blame]
Parker Schuhe4a70d62017-12-27 20:10:20 -08001#include "aos/events/shm-event-loop.h"
2
3#include "aos/events/event-loop_param_test.h"
4#include "aos/testing/test_shm.h"
5#include "gtest/gtest.h"
6
7namespace aos {
8namespace testing {
9namespace {
10
11class ShmEventLoopTestFactory : public EventLoopTestFactory {
12 public:
Austin Schuh44019f92019-05-19 19:58:27 -070013 ::std::unique_ptr<EventLoop> Make() override {
14 return ::std::unique_ptr<EventLoop>(new ShmEventLoop());
Parker Schuhe4a70d62017-12-27 20:10:20 -080015 }
16
Austin Schuh44019f92019-05-19 19:58:27 -070017 ::std::unique_ptr<EventLoop> MakePrimary() override {
18 ::std::unique_ptr<ShmEventLoop> loop =
19 ::std::unique_ptr<ShmEventLoop>(new ShmEventLoop());
20 primary_event_loop_ = loop.get();
21 return ::std::move(loop);
22 }
23
24 void Run() override { CHECK_NOTNULL(primary_event_loop_)->Run(); }
25
26 private:
Parker Schuhe4a70d62017-12-27 20:10:20 -080027 ::aos::testing::TestSharedMemory my_shm_;
Austin Schuh44019f92019-05-19 19:58:27 -070028
29 ::aos::ShmEventLoop *primary_event_loop_;
Parker Schuhe4a70d62017-12-27 20:10:20 -080030};
31
32INSTANTIATE_TEST_CASE_P(ShmEventLoopTest, AbstractEventLoopTest,
33 ::testing::Values([]() {
34 return new ShmEventLoopTestFactory();
35 }));
36
James Kuszmaulc79768b2019-02-18 15:08:44 -080037struct TestMessage : public ::aos::Message {
38 enum { kQueueLength = 100, kHash = 0x696c0cdc };
39 int msg_value;
40
41 void Zero() { msg_value = 0; }
42 static size_t Size() { return 1 + ::aos::Message::Size(); }
43 size_t Print(char *buffer, size_t length) const;
44 TestMessage() { Zero(); }
45};
46
Parker Schuhe4a70d62017-12-27 20:10:20 -080047} // namespace
James Kuszmaulc79768b2019-02-18 15:08:44 -080048
49// Tests that FetchNext behaves correctly when we get two messages in the queue
50// but don't consume the first until after the second has been sent.
51// This cannot be abstracted to AbstractEventLoopTest because not all
52// event loops currently support FetchNext().
53TEST(ShmEventLoopTest, FetchNextTest) {
54 ::aos::testing::TestSharedMemory my_shm;
55
56 ShmEventLoop send_loop;
57 ShmEventLoop fetch_loop;
58 auto sender = send_loop.MakeSender<TestMessage>("/test");
59 Fetcher<TestMessage> fetcher = fetch_loop.MakeFetcher<TestMessage>("/test");
60
61 {
62 auto msg = sender.MakeMessage();
63 msg->msg_value = 100;
64 ASSERT_TRUE(msg.Send());
65 }
66
67 {
68 auto msg = sender.MakeMessage();
69 msg->msg_value = 200;
70 ASSERT_TRUE(msg.Send());
71 }
72
73 ASSERT_TRUE(fetcher.FetchNext());
74 ASSERT_NE(nullptr, fetcher.get());
75 EXPECT_EQ(100, fetcher->msg_value);
76
77 ASSERT_TRUE(fetcher.FetchNext());
78 ASSERT_NE(nullptr, fetcher.get());
79 EXPECT_EQ(200, fetcher->msg_value);
80
81 // When we run off the end of the queue, expect to still have the old message:
82 ASSERT_FALSE(fetcher.FetchNext());
83 ASSERT_NE(nullptr, fetcher.get());
84 EXPECT_EQ(200, fetcher->msg_value);
85}
Parker Schuhe4a70d62017-12-27 20:10:20 -080086} // namespace testing
87} // namespace aos