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

Side by Side Diff: mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc

Issue 2608163003: Change single-interface mojo bindings to use SequencedTaskRunner. (Closed)
Patch Set: Created 3 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <stdint.h> 5 #include <stdint.h>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/callback_helpers.h"
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
13 #include "base/threading/sequenced_task_runner_handle.h" 14 #include "base/threading/sequenced_task_runner_handle.h"
14 #include "base/threading/thread.h" 15 #include "base/threading/thread.h"
16 #include "base/threading/thread_task_runner_handle.h"
15 #include "mojo/public/cpp/bindings/binding.h" 17 #include "mojo/public/cpp/bindings/binding.h"
16 #include "mojo/public/cpp/bindings/strong_binding.h" 18 #include "mojo/public/cpp/bindings/strong_binding.h"
19 #include "mojo/public/cpp/bindings/tests/thread_per_task_sequenced_task_runner.h "
17 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" 20 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
18 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" 21 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h"
19 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" 22 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
20 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" 23 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h"
21 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" 24 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h"
22 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
23 26
24 namespace mojo { 27 namespace mojo {
25 namespace test { 28 namespace test {
26 namespace { 29 namespace {
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 closure.Run(); 222 closure.Run();
220 } 223 }
221 224
222 TEST_F(InterfacePtrTest, IsBound) { 225 TEST_F(InterfacePtrTest, IsBound) {
223 math::CalculatorPtr calc; 226 math::CalculatorPtr calc;
224 EXPECT_FALSE(calc.is_bound()); 227 EXPECT_FALSE(calc.is_bound());
225 MathCalculatorImpl calc_impl(MakeRequest(&calc)); 228 MathCalculatorImpl calc_impl(MakeRequest(&calc));
226 EXPECT_TRUE(calc.is_bound()); 229 EXPECT_TRUE(calc.is_bound());
227 } 230 }
228 231
229 TEST_F(InterfacePtrTest, EndToEnd) { 232 class EndToEndInterfacePtrTest : public InterfacePtrTest {
230 math::CalculatorPtr calc; 233 public:
231 MathCalculatorImpl calc_impl(MakeRequest(&calc)); 234 void RunTest(const scoped_refptr<base::SequencedTaskRunner> runner) {
235 base::RunLoop run_loop;
236 done_closure_ = run_loop.QuitClosure();
237 done_runner_ = base::ThreadTaskRunnerHandle::Get();
238 runner->PostTask(FROM_HERE,
239 base::Bind(&EndToEndInterfacePtrTest::RunTestImpl,
240 base::Unretained(this)));
241 run_loop.Run();
242 }
232 243
233 // Suppose this is instantiated in a process that has pipe1_. 244 private:
234 MathCalculatorUI calculator_ui(std::move(calc)); 245 void RunTestImpl() {
246 math::CalculatorPtr calc;
247 calc_impl_ = base::MakeUnique<MathCalculatorImpl>(MakeRequest(&calc));
248 calculator_ui_ = base::MakeUnique<MathCalculatorUI>(std::move(calc));
249 calculator_ui_->Add(2.0, base::Bind(&EndToEndInterfacePtrTest::AddDone,
250 base::Unretained(this)));
251 calculator_ui_->Multiply(5.0,
252 base::Bind(&EndToEndInterfacePtrTest::MultiplyDone,
253 base::Unretained(this)));
254 EXPECT_EQ(0.0, calculator_ui_->GetOutput());
255 }
235 256
236 base::RunLoop run_loop, run_loop2; 257 void AddDone() { EXPECT_EQ(2.0, calculator_ui_->GetOutput()); }
237 calculator_ui.Add(2.0, run_loop.QuitClosure());
238 calculator_ui.Multiply(5.0, run_loop2.QuitClosure());
239 run_loop.Run();
240 run_loop2.Run();
241 258
242 EXPECT_EQ(10.0, calculator_ui.GetOutput()); 259 void MultiplyDone() {
260 EXPECT_EQ(10.0, calculator_ui_->GetOutput());
261 calculator_ui_.reset();
262 calc_impl_.reset();
263 done_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&done_closure_));
264 }
265
266 base::Closure done_closure_;
267 scoped_refptr<base::SingleThreadTaskRunner> done_runner_;
268 std::unique_ptr<MathCalculatorUI> calculator_ui_;
269 std::unique_ptr<MathCalculatorImpl> calc_impl_;
270 };
271
272 TEST_F(EndToEndInterfacePtrTest, EndToEnd) {
273 RunTest(base::ThreadTaskRunnerHandle::Get());
243 } 274 }
244 275
245 TEST_F(InterfacePtrTest, EndToEnd_Synchronous) { 276 TEST_F(EndToEndInterfacePtrTest, EndToEndOnSequence) {
246 math::CalculatorPtr calc; 277 ThreadPerTaskSequencedTaskRunnerOwner runner;
247 MathCalculatorImpl calc_impl(MakeRequest(&calc)); 278 RunTest(runner.GetSequencedTaskRunner());
248
249 // Suppose this is instantiated in a process that has pipe1_.
250 MathCalculatorUI calculator_ui(std::move(calc));
251
252 EXPECT_EQ(0.0, calculator_ui.GetOutput());
253
254 base::RunLoop run_loop;
255 calculator_ui.Add(2.0, run_loop.QuitClosure());
256 EXPECT_EQ(0.0, calculator_ui.GetOutput());
257 calc_impl.binding()->WaitForIncomingMethodCall();
258 run_loop.Run();
259 EXPECT_EQ(2.0, calculator_ui.GetOutput());
260
261 base::RunLoop run_loop2;
262 calculator_ui.Multiply(5.0, run_loop2.QuitClosure());
263 EXPECT_EQ(2.0, calculator_ui.GetOutput());
264 calc_impl.binding()->WaitForIncomingMethodCall();
265 run_loop2.Run();
266 EXPECT_EQ(10.0, calculator_ui.GetOutput());
267 } 279 }
268 280
269 TEST_F(InterfacePtrTest, Movable) { 281 TEST_F(InterfacePtrTest, Movable) {
270 math::CalculatorPtr a; 282 math::CalculatorPtr a;
271 math::CalculatorPtr b; 283 math::CalculatorPtr b;
272 MathCalculatorImpl calc_impl(MakeRequest(&b)); 284 MathCalculatorImpl calc_impl(MakeRequest(&b));
273 285
274 EXPECT_TRUE(!a); 286 EXPECT_TRUE(!a);
275 EXPECT_FALSE(!b); 287 EXPECT_FALSE(!b);
276 288
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after
902 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl); 914 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl);
903 915
904 // Reset the pointer now so the InterfacePtr associated resources can be 916 // Reset the pointer now so the InterfacePtr associated resources can be
905 // deleted before the background thread's message loop is invalidated. 917 // deleted before the background thread's message loop is invalidated.
906 thread_safe_ptr = nullptr; 918 thread_safe_ptr = nullptr;
907 } 919 }
908 920
909 } // namespace 921 } // namespace
910 } // namespace test 922 } // namespace test
911 } // namespace mojo 923 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/e2e_perftest.cc ('k') | mojo/public/cpp/bindings/tests/thread_per_task_sequenced_task_runner.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698