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

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

Issue 2498743002: Mojo: introducing a thread safe interface pointer. (Closed)
Patch Set: Clean-up Created 4 years, 1 month 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/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/threading/sequenced_task_runner_handle.h"
13 #include "base/threading/thread.h"
12 #include "mojo/public/cpp/bindings/binding.h" 14 #include "mojo/public/cpp/bindings/binding.h"
13 #include "mojo/public/cpp/bindings/strong_binding.h" 15 #include "mojo/public/cpp/bindings/strong_binding.h"
14 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" 16 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h"
15 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" 17 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
16 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" 18 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h"
17 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" 19 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
20 namespace mojo { 22 namespace mojo {
21 namespace test { 23 namespace test {
(...skipping 780 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 raw_proxy->Ping( 804 raw_proxy->Ping(
803 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr))); 805 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr)));
804 806
805 // Trigger an error on |ptr|. This will ultimately lead to the proxy's 807 // Trigger an error on |ptr|. This will ultimately lead to the proxy's
806 // response callbacks being destroyed, which will in turn lead to the proxy 808 // response callbacks being destroyed, which will in turn lead to the proxy
807 // being destroyed. This should not crash. 809 // being destroyed. This should not crash.
808 request.PassMessagePipe(); 810 request.PassMessagePipe();
809 run_loop.Run(); 811 run_loop.Run();
810 } 812 }
811 813
814 TEST_F(InterfacePtrTest, TestThreadSafeInterfacePointer) {
815 math::CalculatorPtr ptr;
816 MathCalculatorImpl calc_impl(GetProxy(&ptr));
817
818 scoped_refptr<ThreadSafeInterfacePtr<math::Calculator>> thread_safe_ptr =
819 ptr.GetThreadSafePtr();
820
821 base::RunLoop run_loop;
822
823 // Create and start the thread from where we'll call the interface pointer.
824 base::Thread other_thread("service test thread");
825 other_thread.Start();
826 auto run_method = base::Bind(
827 [](const scoped_refptr<base::TaskRunner>& main_task_runner,
828 const base::Closure& quit_closure,
829 const scoped_refptr<ThreadSafeInterfacePtr<math::Calculator>>&
830 thread_safe_ptr) {
831 auto calc_callback = base::Bind(
832 [](const scoped_refptr<base::TaskRunner>& main_task_runner,
833 const base::Closure& quit_closure,
834 base::PlatformThreadId thread_id, double result) {
835 EXPECT_EQ(123, result);
836 // Validate the callback is invoked on the calling thread.
837 EXPECT_EQ(thread_id, base::PlatformThread::CurrentId());
838 // Notify the run_loop to quit.
839 main_task_runner->PostTask(FROM_HERE, quit_closure);
840 });
841 thread_safe_ptr->get_interface()->Add(
842 123, base::Bind(calc_callback, main_task_runner, quit_closure,
843 base::PlatformThread::CurrentId()));
844 },
845 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(),
846 thread_safe_ptr);
847 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method);
848 // Block until the method callback is called on the background thread.
849 run_loop.Run();
850 }
851
812 } // namespace 852 } // namespace
813 } // namespace test 853 } // namespace test
814 } // namespace mojo 854 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698