blob: b83afc43d62e3b6f5d3e32cd22591631e1f49b1c [file] [log] [blame]
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -07001// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "mojo/edk/system/channel_endpoint.h"
6
Viet-Trung Luu266a79f2015-09-18 08:27:48 -07007#include <memory>
8#include <utility>
9
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070010#include "base/synchronization/waitable_event.h"
Viet-Trung Luub7cd10b2015-10-30 09:47:47 -070011#include "base/time/time.h"
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070012#include "mojo/edk/system/channel_test_base.h"
13#include "mojo/edk/system/message_in_transit_queue.h"
Viet-Trung Luu5e139d62015-05-26 12:29:02 -070014#include "mojo/edk/system/message_in_transit_test_utils.h"
Viet-Trung Luu272228d2015-10-14 13:16:21 -070015#include "mojo/edk/system/ref_ptr.h"
Viet-Trung Luub7cd10b2015-10-30 09:47:47 -070016#include "mojo/edk/system/test/timeouts.h"
Viet-Trung Luu5abda572015-05-27 11:14:42 -070017#include "mojo/edk/system/test_channel_endpoint_client.h"
Viet-Trung Luu7d5ced22015-06-24 15:21:39 -070018#include "mojo/public/cpp/system/macros.h"
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070019
20namespace mojo {
21namespace system {
22namespace {
23
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070024class ChannelEndpointTest : public test::ChannelTestBase {
25 public:
26 ChannelEndpointTest() {}
27 ~ChannelEndpointTest() override {}
28
29 void SetUp() override {
30 test::ChannelTestBase::SetUp();
31
32 PostMethodToIOThreadAndWait(
Viet-Trung Luu4af4c9b2015-10-19 22:27:25 -070033 &ChannelEndpointTest::CreateAndInitChannelOnIOThread, 0);
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070034 PostMethodToIOThreadAndWait(
Viet-Trung Luu4af4c9b2015-10-19 22:27:25 -070035 &ChannelEndpointTest::CreateAndInitChannelOnIOThread, 1);
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070036 }
37
38 void TearDown() override {
Viet-Trung Luu4af4c9b2015-10-19 22:27:25 -070039 PostMethodToIOThreadAndWait(&ChannelEndpointTest::ShutdownChannelOnIOThread,
40 0);
41 PostMethodToIOThreadAndWait(&ChannelEndpointTest::ShutdownChannelOnIOThread,
42 1);
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070043
44 test::ChannelTestBase::TearDown();
45 }
46
47 private:
Viet-Trung Luu7d5ced22015-06-24 15:21:39 -070048 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelEndpointTest);
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070049};
50
51TEST_F(ChannelEndpointTest, Basic) {
Viet-Trung Luue1116132015-10-15 12:22:40 -070052 auto client0 = MakeRefCounted<test::TestChannelEndpointClient>();
53 auto endpoint0 = MakeRefCounted<ChannelEndpoint>(client0.Clone(), 0);
Viet-Trung Luu272228d2015-10-14 13:16:21 -070054 client0->Init(0, endpoint0.Clone());
55 channel(0)->SetBootstrapEndpoint(std::move(endpoint0));
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070056
Viet-Trung Luue1116132015-10-15 12:22:40 -070057 auto client1 = MakeRefCounted<test::TestChannelEndpointClient>();
58 auto endpoint1 = MakeRefCounted<ChannelEndpoint>(client1.Clone(), 1);
Viet-Trung Luu272228d2015-10-14 13:16:21 -070059 client1->Init(1, endpoint1.Clone());
60 channel(1)->SetBootstrapEndpoint(endpoint1.Clone());
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070061
62 // We'll receive a message on channel/client 0.
63 base::WaitableEvent read_event(true, false);
64 client0->SetReadEvent(&read_event);
65
66 // Make a test message.
67 unsigned message_id = 0x12345678;
Viet-Trung Luu266a79f2015-09-18 08:27:48 -070068 std::unique_ptr<MessageInTransit> send_message =
69 test::MakeTestMessage(message_id);
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070070 // Check that our test utility works (at least in one direction).
Viet-Trung Luu5e139d62015-05-26 12:29:02 -070071 test::VerifyTestMessage(send_message.get(), message_id);
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070072
73 // Event shouldn't be signalled yet.
74 EXPECT_FALSE(read_event.IsSignaled());
75
76 // Send it through channel/endpoint 1.
Viet-Trung Luu266a79f2015-09-18 08:27:48 -070077 EXPECT_TRUE(endpoint1->EnqueueMessage(std::move(send_message)));
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070078
79 // Wait to receive it.
Viet-Trung Luub7cd10b2015-10-30 09:47:47 -070080 EXPECT_TRUE(read_event.TimedWait(base::TimeDelta::FromMicroseconds(
81 static_cast<int64_t>(test::TinyTimeout()))));
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070082 client0->SetReadEvent(nullptr);
83
84 // Check the received message.
85 ASSERT_EQ(1u, client0->NumMessages());
Viet-Trung Luu266a79f2015-09-18 08:27:48 -070086 std::unique_ptr<MessageInTransit> read_message = client0->PopMessage();
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070087 ASSERT_TRUE(read_message);
Viet-Trung Luu5e139d62015-05-26 12:29:02 -070088 test::VerifyTestMessage(read_message.get(), message_id);
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070089}
90
91// Checks that prequeued messages and messages sent at various stages later on
92// are all sent/received (and in the correct order). (Note: Due to the way
93// bootstrap endpoints work, the receiving side has to be set up first.)
94TEST_F(ChannelEndpointTest, Prequeued) {
Viet-Trung Luue1116132015-10-15 12:22:40 -070095 auto client0 = MakeRefCounted<test::TestChannelEndpointClient>();
96 auto endpoint0 = MakeRefCounted<ChannelEndpoint>(client0.Clone(), 0);
Viet-Trung Luu272228d2015-10-14 13:16:21 -070097 client0->Init(0, endpoint0.Clone());
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -070098
Viet-Trung Luu272228d2015-10-14 13:16:21 -070099 channel(0)->SetBootstrapEndpoint(std::move(endpoint0));
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700100 MessageInTransitQueue prequeued_messages;
Viet-Trung Luu5e139d62015-05-26 12:29:02 -0700101 prequeued_messages.AddMessage(test::MakeTestMessage(1));
102 prequeued_messages.AddMessage(test::MakeTestMessage(2));
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700103
Viet-Trung Luue1116132015-10-15 12:22:40 -0700104 auto client1 = MakeRefCounted<test::TestChannelEndpointClient>();
Viet-Trung Luu272228d2015-10-14 13:16:21 -0700105 auto endpoint1 =
Viet-Trung Luue1116132015-10-15 12:22:40 -0700106 MakeRefCounted<ChannelEndpoint>(client1.Clone(), 1, &prequeued_messages);
Viet-Trung Luu272228d2015-10-14 13:16:21 -0700107 client1->Init(1, endpoint1.Clone());
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700108
Viet-Trung Luu5e139d62015-05-26 12:29:02 -0700109 EXPECT_TRUE(endpoint1->EnqueueMessage(test::MakeTestMessage(3)));
110 EXPECT_TRUE(endpoint1->EnqueueMessage(test::MakeTestMessage(4)));
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700111
Viet-Trung Luu272228d2015-10-14 13:16:21 -0700112 channel(1)->SetBootstrapEndpoint(endpoint1.Clone());
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700113
Viet-Trung Luu5e139d62015-05-26 12:29:02 -0700114 EXPECT_TRUE(endpoint1->EnqueueMessage(test::MakeTestMessage(5)));
115 EXPECT_TRUE(endpoint1->EnqueueMessage(test::MakeTestMessage(6)));
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700116
117 // Wait for the messages.
118 base::WaitableEvent read_event(true, false);
119 client0->SetReadEvent(&read_event);
120 for (size_t i = 0; client0->NumMessages() < 6 && i < 6; i++) {
Viet-Trung Luub7cd10b2015-10-30 09:47:47 -0700121 EXPECT_TRUE(read_event.TimedWait(base::TimeDelta::FromMicroseconds(
122 static_cast<int64_t>(test::TinyTimeout()))));
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700123 read_event.Reset();
124 }
125 client0->SetReadEvent(nullptr);
126
127 // Check the received messages.
128 ASSERT_EQ(6u, client0->NumMessages());
129 for (unsigned message_id = 1; message_id <= 6; message_id++) {
Viet-Trung Luu266a79f2015-09-18 08:27:48 -0700130 std::unique_ptr<MessageInTransit> read_message = client0->PopMessage();
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700131 ASSERT_TRUE(read_message);
Viet-Trung Luu5e139d62015-05-26 12:29:02 -0700132 test::VerifyTestMessage(read_message.get(), message_id);
Viet-Trung Luub6d3b3c2015-05-26 10:36:20 -0700133 }
134}
135
136} // namespace
137} // namespace system
138} // namespace mojo