Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: extensions/browser/mojo/stash_backend_unittest.cc

Issue 648853007: Add handle waiting to StashBackend. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h" 7 #include "base/run_loop.h"
8 #include "extensions/browser/mojo/stash_backend.h" 8 #include "extensions/browser/mojo/stash_backend.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/mojo/src/mojo/public/interfaces/application/service_provid er.mojom.h"
10 11
11 namespace extensions { 12 namespace extensions {
13 namespace {
14
15 // Create a data pipe, write some data to the producer handle and return the
16 // consumer handle.
17 mojo::ScopedHandle CreateReadableHandle() {
18 mojo::ScopedDataPipeConsumerHandle consumer_handle;
19 mojo::ScopedDataPipeProducerHandle producer_handle;
20 MojoCreateDataPipeOptions options = {
21 sizeof(options), MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 1, 1,
22 };
23 MojoResult result =
24 mojo::CreateDataPipe(&options, &producer_handle, &consumer_handle);
25 EXPECT_EQ(MOJO_RESULT_OK, result);
26 uint32_t num_bytes = 1;
27 result = mojo::WriteDataRaw(producer_handle.get(), "a", &num_bytes,
28 MOJO_WRITE_DATA_FLAG_NONE);
29 EXPECT_EQ(MOJO_RESULT_OK, result);
30 EXPECT_EQ(1u, num_bytes);
31 return mojo::ScopedHandle::From(consumer_handle.Pass());
32 }
33
34 } // namespace
12 35
13 class StashServiceTest : public testing::Test, public mojo::ErrorHandler { 36 class StashServiceTest : public testing::Test, public mojo::ErrorHandler {
14 public: 37 public:
15 enum Event { 38 enum Event {
16 EVENT_NONE, 39 EVENT_NONE,
17 EVENT_STASH_RETRIEVED, 40 EVENT_STASH_RETRIEVED,
41 EVENT_HANDLE_READY,
18 }; 42 };
19 43
20 StashServiceTest() {} 44 StashServiceTest() {}
21 45
22 void SetUp() override { 46 void SetUp() override {
23 expecting_error_ = false; 47 expecting_error_ = false;
24 expected_event_ = EVENT_NONE; 48 expected_event_ = EVENT_NONE;
25 stash_backend_.reset(new StashBackend); 49 stash_backend_.reset(new StashBackend(base::Bind(
50 &StashServiceTest::OnHandleReadyToRead, base::Unretained(this))));
26 stash_backend_->BindToRequest(mojo::GetProxy(&stash_service_)); 51 stash_backend_->BindToRequest(mojo::GetProxy(&stash_service_));
27 stash_service_.set_error_handler(this); 52 stash_service_.set_error_handler(this);
53 handles_ready_ = 0;
28 } 54 }
29 55
30 void OnConnectionError() override { FAIL() << "Unexpected connection error"; } 56 void OnConnectionError() override { FAIL() << "Unexpected connection error"; }
31 57
32 mojo::Array<StashedObjectPtr> RetrieveStash() { 58 mojo::Array<StashedObjectPtr> RetrieveStash() {
33 mojo::Array<StashedObjectPtr> stash; 59 mojo::Array<StashedObjectPtr> stash;
34 stash_service_->RetrieveStash(base::Bind( 60 stash_service_->RetrieveStash(base::Bind(
35 &StashServiceTest::StashRetrieved, base::Unretained(this), &stash)); 61 &StashServiceTest::StashRetrieved, base::Unretained(this), &stash));
36 WaitForEvent(EVENT_STASH_RETRIEVED); 62 WaitForEvent(EVENT_STASH_RETRIEVED);
37 return stash.Pass(); 63 return stash.Pass();
(...skipping 10 matching lines...) Expand all
48 base::RunLoop run_loop; 74 base::RunLoop run_loop;
49 stop_run_loop_ = run_loop.QuitClosure(); 75 stop_run_loop_ = run_loop.QuitClosure();
50 run_loop.Run(); 76 run_loop.Run();
51 } 77 }
52 78
53 void EventReceived(Event event) { 79 void EventReceived(Event event) {
54 if (event == expected_event_ && !stop_run_loop_.is_null()) 80 if (event == expected_event_ && !stop_run_loop_.is_null())
55 stop_run_loop_.Run(); 81 stop_run_loop_.Run();
56 } 82 }
57 83
84 void OnHandleReadyToRead() {
85 handles_ready_++;
86 EventReceived(EVENT_HANDLE_READY);
87 }
88
58 protected: 89 protected:
59 base::MessageLoop message_loop_; 90 base::MessageLoop message_loop_;
60 base::Closure stop_run_loop_; 91 base::Closure stop_run_loop_;
61 scoped_ptr<StashBackend> stash_backend_; 92 scoped_ptr<StashBackend> stash_backend_;
62 Event expected_event_; 93 Event expected_event_;
63 bool expecting_error_; 94 bool expecting_error_;
64 mojo::InterfacePtr<StashService> stash_service_; 95 mojo::InterfacePtr<StashService> stash_service_;
96 int handles_ready_;
65 97
66 private: 98 private:
67 DISALLOW_COPY_AND_ASSIGN(StashServiceTest); 99 DISALLOW_COPY_AND_ASSIGN(StashServiceTest);
68 }; 100 };
69 101
70 // Test that adding stashed objects in multiple calls can all be retrieved by a 102 // Test that adding stashed objects in multiple calls can all be retrieved by a
71 // Retrieve call. 103 // Retrieve call.
72 TEST_F(StashServiceTest, AddTwiceAndRetrieve) { 104 TEST_F(StashServiceTest, AddTwiceAndRetrieve) {
73 mojo::Array<StashedObjectPtr> stashed_objects; 105 mojo::Array<StashedObjectPtr> stashed_objects;
74 StashedObjectPtr stashed_object(StashedObject::New()); 106 StashedObjectPtr stashed_object(StashedObject::New());
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 ASSERT_EQ(1u, num_bytes); 172 ASSERT_EQ(1u, num_bytes);
141 EXPECT_EQ('1', data); 173 EXPECT_EQ('1', data);
142 } 174 }
143 175
144 TEST_F(StashServiceTest, RetrieveWithoutStashing) { 176 TEST_F(StashServiceTest, RetrieveWithoutStashing) {
145 mojo::Array<StashedObjectPtr> stashed_objects = RetrieveStash(); 177 mojo::Array<StashedObjectPtr> stashed_objects = RetrieveStash();
146 ASSERT_TRUE(!stashed_objects.is_null()); 178 ASSERT_TRUE(!stashed_objects.is_null());
147 EXPECT_EQ(0u, stashed_objects.size()); 179 EXPECT_EQ(0u, stashed_objects.size());
148 } 180 }
149 181
182 TEST_F(StashServiceTest, NotifyOnReadableHandle) {
183 mojo::Array<StashedObjectPtr> stash_entries;
184 StashedObjectPtr stashed_object(StashedObject::New());
185 stashed_object->id = "test type";
186 stashed_object->data.push_back(0);
187 stashed_object->monitor_handles = true;
188 mojo::ServiceProviderPtr service_provider;
189
190 // Stash the ServiceProvider request. When we make a call on
191 // |service_provider|, the stashed handle will become readable.
192 stashed_object->stashed_handles.push_back(mojo::ScopedHandle::From(
193 mojo::GetProxy(&service_provider).PassMessagePipe()));
194
195 stash_entries.push_back(stashed_object.Pass());
196 stash_service_->AddToStash(stash_entries.Pass());
197
198 mojo::MessagePipe pipe;
199 service_provider->ConnectToService("", pipe.handle0.Pass());
200
201 WaitForEvent(EVENT_HANDLE_READY);
202 EXPECT_EQ(1, handles_ready_);
203 }
204
205 TEST_F(StashServiceTest, NotifyOnReadableDataPipeHandle) {
206 mojo::Array<StashedObjectPtr> stash_entries;
207 StashedObjectPtr stashed_object(StashedObject::New());
208 stashed_object->id = "test type";
209 stashed_object->monitor_handles = true;
210
211 MojoCreateDataPipeOptions options = {
212 sizeof(options), MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 1, 1,
213 };
214 mojo::ScopedDataPipeConsumerHandle consumer_handle;
215 mojo::ScopedDataPipeProducerHandle producer_handle;
216 uint32_t num_bytes = 1;
217 MojoResult result =
218 mojo::CreateDataPipe(&options, &producer_handle, &consumer_handle);
219 ASSERT_EQ(MOJO_RESULT_OK, result);
220 result = mojo::WriteDataRaw(producer_handle.get(), "a", &num_bytes,
221 MOJO_WRITE_DATA_FLAG_NONE);
222 ASSERT_EQ(MOJO_RESULT_OK, result);
223 ASSERT_EQ(1u, num_bytes);
224 stashed_object->stashed_handles.push_back(
225 mojo::ScopedHandle::From(producer_handle.Pass()));
226 stashed_object->stashed_handles.push_back(
227 mojo::ScopedHandle::From(consumer_handle.Pass()));
228 stashed_object->data.push_back(1);
229
230 stash_entries.push_back(stashed_object.Pass());
231 stash_service_->AddToStash(stash_entries.Pass());
232 WaitForEvent(EVENT_HANDLE_READY);
233 EXPECT_EQ(1, handles_ready_);
234 }
235
236 TEST_F(StashServiceTest, NotifyOncePerStashOnReadableHandles) {
237 mojo::Array<StashedObjectPtr> stash_entries;
238 StashedObjectPtr stashed_object(StashedObject::New());
239 stashed_object->id = "test type";
240 stashed_object->data.push_back(1);
241 stashed_object->monitor_handles = true;
242 stashed_object->stashed_handles.push_back(CreateReadableHandle());
243 stashed_object->stashed_handles.push_back(CreateReadableHandle());
244 stash_entries.push_back(stashed_object.Pass());
245 stashed_object = StashedObject::New();
246 stashed_object->id = "another test type";
247 stashed_object->data.push_back(2);
248 stashed_object->monitor_handles = true;
249 stashed_object->stashed_handles.push_back(CreateReadableHandle());
250 stashed_object->stashed_handles.push_back(CreateReadableHandle());
251 stash_entries.push_back(stashed_object.Pass());
252 stash_service_->AddToStash(stash_entries.Pass());
253 WaitForEvent(EVENT_HANDLE_READY);
254 EXPECT_EQ(1, handles_ready_);
255
256 stashed_object = StashedObject::New();
257 stashed_object->id = "yet another test type";
258 stashed_object->data.push_back(3);
259 stashed_object->monitor_handles = true;
260 stashed_object->stashed_handles.push_back(CreateReadableHandle());
261 stashed_object->stashed_handles.push_back(CreateReadableHandle());
262 stash_entries.push_back(stashed_object.Pass());
263 stash_service_->AddToStash(stash_entries.Pass());
264
265 stash_service_->AddToStash(RetrieveStash());
266 WaitForEvent(EVENT_HANDLE_READY);
267 EXPECT_EQ(2, handles_ready_);
268 }
269
150 // Test that a stash service discards stashed objects when the backend no longer 270 // Test that a stash service discards stashed objects when the backend no longer
151 // exists. 271 // exists.
152 TEST_F(StashServiceTest, ServiceWithDeletedBackend) { 272 TEST_F(StashServiceTest, ServiceWithDeletedBackend) {
153 stash_backend_.reset(); 273 stash_backend_.reset();
154 stash_service_.set_error_handler(this); 274 stash_service_.set_error_handler(this);
155 275
156 mojo::Array<StashedObjectPtr> stashed_objects; 276 mojo::Array<StashedObjectPtr> stashed_objects;
157 StashedObjectPtr stashed_object(StashedObject::New()); 277 StashedObjectPtr stashed_object(StashedObject::New());
158 stashed_object->id = "test type"; 278 stashed_object->id = "test type";
159 stashed_object->data.push_back(1); 279 stashed_object->data.push_back(1);
160 mojo::MessagePipe message_pipe; 280 mojo::MessagePipe message_pipe;
161 stashed_object->stashed_handles.push_back( 281 stashed_object->stashed_handles.push_back(
162 mojo::ScopedHandle::From(message_pipe.handle0.Pass())); 282 mojo::ScopedHandle::From(message_pipe.handle0.Pass()));
163 stashed_objects.push_back(stashed_object.Pass()); 283 stashed_objects.push_back(stashed_object.Pass());
164 stash_service_->AddToStash(stashed_objects.Pass()); 284 stash_service_->AddToStash(stashed_objects.Pass());
165 stashed_objects = RetrieveStash(); 285 stashed_objects = RetrieveStash();
166 ASSERT_EQ(0u, stashed_objects.size()); 286 ASSERT_EQ(0u, stashed_objects.size());
167 // Check that the stashed handle has been closed. 287 // Check that the stashed handle has been closed.
168 MojoResult result = 288 MojoResult result =
169 mojo::Wait(message_pipe.handle1.get(), 289 mojo::Wait(message_pipe.handle1.get(),
170 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_READABLE, 290 MOJO_HANDLE_SIGNAL_READABLE | MOJO_HANDLE_SIGNAL_READABLE,
171 MOJO_DEADLINE_INDEFINITE, nullptr); 291 MOJO_DEADLINE_INDEFINITE, nullptr);
172 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result); 292 EXPECT_EQ(MOJO_RESULT_FAILED_PRECONDITION, result);
173 } 293 }
174 294
175 } // namespace extensions 295 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698