OLD | NEW |
| (Empty) |
1 // Copyright 2014 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.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/test/test_io_thread.h" | |
11 #include "mojo/edk/embedder/platform_channel_pair.h" | |
12 #include "mojo/edk/embedder/simple_platform_support.h" | |
13 #include "mojo/edk/system/channel_endpoint.h" | |
14 #include "mojo/edk/system/channel_endpoint_id.h" | |
15 #include "mojo/edk/system/message_pipe.h" | |
16 #include "mojo/edk/system/raw_channel.h" | |
17 #include "mojo/edk/system/test_utils.h" | |
18 #include "mojo/edk/system/waiter.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 namespace mojo { | |
22 namespace system { | |
23 namespace { | |
24 | |
25 class ChannelTest : public testing::Test { | |
26 public: | |
27 ChannelTest() : io_thread_(base::TestIOThread::kAutoStart) {} | |
28 ~ChannelTest() override {} | |
29 | |
30 void SetUp() override { | |
31 io_thread_.PostTaskAndWait( | |
32 FROM_HERE, | |
33 base::Bind(&ChannelTest::SetUpOnIOThread, base::Unretained(this))); | |
34 } | |
35 | |
36 void CreateChannelOnIOThread() { | |
37 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); | |
38 channel_ = new Channel(&platform_support_); | |
39 } | |
40 | |
41 void InitChannelOnIOThread() { | |
42 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); | |
43 | |
44 CHECK(raw_channel_); | |
45 CHECK(channel_); | |
46 channel_->Init(raw_channel_.Pass()); | |
47 } | |
48 | |
49 void ShutdownChannelOnIOThread() { | |
50 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); | |
51 | |
52 CHECK(channel_); | |
53 channel_->Shutdown(); | |
54 } | |
55 | |
56 base::TestIOThread* io_thread() { return &io_thread_; } | |
57 RawChannel* raw_channel() { return raw_channel_.get(); } | |
58 scoped_ptr<RawChannel>* mutable_raw_channel() { return &raw_channel_; } | |
59 Channel* channel() { return channel_.get(); } | |
60 scoped_refptr<Channel>* mutable_channel() { return &channel_; } | |
61 | |
62 private: | |
63 void SetUpOnIOThread() { | |
64 CHECK_EQ(base::MessageLoop::current(), io_thread()->message_loop()); | |
65 | |
66 embedder::PlatformChannelPair channel_pair; | |
67 raw_channel_ = RawChannel::Create(channel_pair.PassServerHandle()).Pass(); | |
68 other_platform_handle_ = channel_pair.PassClientHandle(); | |
69 } | |
70 | |
71 embedder::SimplePlatformSupport platform_support_; | |
72 base::TestIOThread io_thread_; | |
73 scoped_ptr<RawChannel> raw_channel_; | |
74 embedder::ScopedPlatformHandle other_platform_handle_; | |
75 scoped_refptr<Channel> channel_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(ChannelTest); | |
78 }; | |
79 | |
80 // ChannelTest.InitShutdown ---------------------------------------------------- | |
81 | |
82 TEST_F(ChannelTest, InitShutdown) { | |
83 io_thread()->PostTaskAndWait(FROM_HERE, | |
84 base::Bind(&ChannelTest::CreateChannelOnIOThread, | |
85 base::Unretained(this))); | |
86 ASSERT_TRUE(channel()); | |
87 | |
88 io_thread()->PostTaskAndWait( | |
89 FROM_HERE, | |
90 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); | |
91 | |
92 io_thread()->PostTaskAndWait( | |
93 FROM_HERE, base::Bind(&ChannelTest::ShutdownChannelOnIOThread, | |
94 base::Unretained(this))); | |
95 | |
96 // Okay to destroy |Channel| on not-the-I/O-thread. | |
97 EXPECT_TRUE(channel()->HasOneRef()); | |
98 *mutable_channel() = nullptr; | |
99 } | |
100 | |
101 // ChannelTest.CloseBeforeAttachAndRun ----------------------------------------- | |
102 | |
103 TEST_F(ChannelTest, CloseBeforeRun) { | |
104 io_thread()->PostTaskAndWait(FROM_HERE, | |
105 base::Bind(&ChannelTest::CreateChannelOnIOThread, | |
106 base::Unretained(this))); | |
107 ASSERT_TRUE(channel()); | |
108 | |
109 io_thread()->PostTaskAndWait( | |
110 FROM_HERE, | |
111 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); | |
112 | |
113 scoped_refptr<ChannelEndpoint> channel_endpoint; | |
114 scoped_refptr<MessagePipe> mp( | |
115 MessagePipe::CreateLocalProxy(&channel_endpoint)); | |
116 | |
117 mp->Close(0); | |
118 | |
119 channel()->SetBootstrapEndpoint(channel_endpoint); | |
120 | |
121 io_thread()->PostTaskAndWait( | |
122 FROM_HERE, base::Bind(&ChannelTest::ShutdownChannelOnIOThread, | |
123 base::Unretained(this))); | |
124 | |
125 EXPECT_TRUE(channel()->HasOneRef()); | |
126 } | |
127 | |
128 // ChannelTest.ShutdownAfterAttachAndRun --------------------------------------- | |
129 | |
130 TEST_F(ChannelTest, ShutdownAfterAttach) { | |
131 io_thread()->PostTaskAndWait(FROM_HERE, | |
132 base::Bind(&ChannelTest::CreateChannelOnIOThread, | |
133 base::Unretained(this))); | |
134 ASSERT_TRUE(channel()); | |
135 | |
136 io_thread()->PostTaskAndWait( | |
137 FROM_HERE, | |
138 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); | |
139 | |
140 scoped_refptr<ChannelEndpoint> channel_endpoint; | |
141 scoped_refptr<MessagePipe> mp( | |
142 MessagePipe::CreateLocalProxy(&channel_endpoint)); | |
143 | |
144 channel()->SetBootstrapEndpoint(channel_endpoint); | |
145 | |
146 Waiter waiter; | |
147 waiter.Init(); | |
148 ASSERT_EQ( | |
149 MOJO_RESULT_OK, | |
150 mp->AddAwakable(0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, nullptr)); | |
151 | |
152 // Don't wait for the shutdown to run ... | |
153 io_thread()->PostTask(FROM_HERE, | |
154 base::Bind(&ChannelTest::ShutdownChannelOnIOThread, | |
155 base::Unretained(this))); | |
156 | |
157 // ... since this |Wait()| should fail once the channel is shut down. | |
158 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, | |
159 waiter.Wait(MOJO_DEADLINE_INDEFINITE, nullptr)); | |
160 HandleSignalsState hss; | |
161 mp->RemoveAwakable(0, &waiter, &hss); | |
162 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | |
163 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | |
164 | |
165 mp->Close(0); | |
166 | |
167 EXPECT_TRUE(channel()->HasOneRef()); | |
168 } | |
169 | |
170 // ChannelTest.WaitAfterAttachRunAndShutdown ----------------------------------- | |
171 | |
172 TEST_F(ChannelTest, WaitAfterAttachRunAndShutdown) { | |
173 io_thread()->PostTaskAndWait(FROM_HERE, | |
174 base::Bind(&ChannelTest::CreateChannelOnIOThread, | |
175 base::Unretained(this))); | |
176 ASSERT_TRUE(channel()); | |
177 | |
178 io_thread()->PostTaskAndWait( | |
179 FROM_HERE, | |
180 base::Bind(&ChannelTest::InitChannelOnIOThread, base::Unretained(this))); | |
181 | |
182 scoped_refptr<ChannelEndpoint> channel_endpoint; | |
183 scoped_refptr<MessagePipe> mp( | |
184 MessagePipe::CreateLocalProxy(&channel_endpoint)); | |
185 | |
186 channel()->SetBootstrapEndpoint(channel_endpoint); | |
187 | |
188 io_thread()->PostTaskAndWait( | |
189 FROM_HERE, base::Bind(&ChannelTest::ShutdownChannelOnIOThread, | |
190 base::Unretained(this))); | |
191 | |
192 Waiter waiter; | |
193 waiter.Init(); | |
194 HandleSignalsState hss; | |
195 EXPECT_EQ( | |
196 MOJO_RESULT_FAILED_PRECONDITION, | |
197 mp->AddAwakable(0, &waiter, MOJO_HANDLE_SIGNAL_READABLE, 123, &hss)); | |
198 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfied_signals); | |
199 EXPECT_EQ(MOJO_HANDLE_SIGNAL_PEER_CLOSED, hss.satisfiable_signals); | |
200 | |
201 mp->Close(0); | |
202 | |
203 EXPECT_TRUE(channel()->HasOneRef()); | |
204 } | |
205 | |
206 // TODO(vtl): More. ------------------------------------------------------------ | |
207 | |
208 } // namespace | |
209 } // namespace system | |
210 } // namespace mojo | |
OLD | NEW |