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

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

Issue 2770153003: mojo: Support sync calls through ThreadSafeInterfacePtr (Closed)
Patch Set: Fix more tests Created 3 years, 9 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/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/threading/sequenced_task_runner_handle.h" 13 #include "base/threading/sequenced_task_runner_handle.h"
14 #include "base/threading/thread.h" 14 #include "base/threading/thread.h"
15 #include "mojo/public/cpp/bindings/binding.h" 15 #include "mojo/public/cpp/bindings/binding.h"
16 #include "mojo/public/cpp/bindings/strong_binding.h" 16 #include "mojo/public/cpp/bindings/strong_binding.h"
17 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" 17 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h"
18 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" 18 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h"
19 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" 19 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h"
20 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" 20 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h"
21 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h" 21 #include "mojo/public/interfaces/bindings/tests/scoping.mojom.h"
22 #include "mojo/public/interfaces/bindings/tests/test_sync_methods.mojom.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
24 namespace mojo { 25 namespace mojo {
25 namespace test { 26 namespace test {
26 namespace { 27 namespace {
27 28
28 typedef base::Callback<void(double)> CalcCallback; 29 typedef base::Callback<void(double)> CalcCallback;
29 30
30 class MathCalculatorImpl : public math::Calculator { 31 class MathCalculatorImpl : public math::Calculator {
31 public: 32 public:
(...skipping 893 matching lines...) Expand 10 before | Expand all | Expand 10 after
925 run_loop.Run(); 926 run_loop.Run();
926 } 927 }
927 928
928 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl); 929 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl);
929 930
930 // Reset the pointer now so the InterfacePtr associated resources can be 931 // Reset the pointer now so the InterfacePtr associated resources can be
931 // deleted before the background thread's message loop is invalidated. 932 // deleted before the background thread's message loop is invalidated.
932 thread_safe_ptr = nullptr; 933 thread_safe_ptr = nullptr;
933 } 934 }
934 935
936 class TestSyncImpl : public TestSync {
937 public:
938 explicit TestSyncImpl(TestSyncRequest request)
939 : binding_(this, std::move(request)) {}
940
941 // TestSync implementation:
942 void Ping(const PingCallback& callback) override { callback.Run(); }
943 void Echo(int32_t value, const EchoCallback& callback) override {
944 callback.Run(value);
945 }
946 void AsyncEcho(int32_t value, const AsyncEchoCallback& callback) override {
947 callback.Run(value);
948 }
949
950 Binding<TestSync>* binding() { return &binding_; }
951
952 private:
953 Binding<TestSync> binding_;
954
955 DISALLOW_COPY_AND_ASSIGN(TestSyncImpl);
956 };
957
958 TEST_F(InterfacePtrTest, ThreadSafeSyncCall) {
959 // Create and start the thread from where we'll bind the interface pointer.
960 base::Thread other_thread("service test thread");
961 other_thread.Start();
962 const scoped_refptr<base::SingleThreadTaskRunner>& other_thread_task_runner =
963 other_thread.message_loop()->task_runner();
964
965 TestSyncPtr ptr;
966 TestSyncRequest request(&ptr);
967
968 // Create a ThreadSafeInterfacePtr that we'll bind from a different thread.
969 scoped_refptr<ThreadSafeTestSyncPtr> thread_safe_ptr =
970 ThreadSafeTestSyncPtr::Create(ptr.PassInterface(),
971 other_thread_task_runner);
972 ASSERT_TRUE(thread_safe_ptr);
973
974 TestSyncImpl* test_sync_impl = nullptr;
975 {
976 base::RunLoop run_loop;
977 auto create_impl = base::Bind(
978 [](TestSyncRequest request, TestSyncImpl** test_sync_impl) {
979 // In real life, the implementation would have a legitimate owner.
980 *test_sync_impl = new TestSyncImpl(std::move(request));
981 },
982 base::Passed(&request), &test_sync_impl);
983 other_thread_task_runner->PostTaskAndReply(FROM_HERE, create_impl,
984 run_loop.QuitClosure());
985 run_loop.Run();
986 }
987
988 int32_t output = -1;
989 (*thread_safe_ptr)->Echo(123, &output);
990 ASSERT_EQ(123, output);
991
992 other_thread_task_runner->DeleteSoon(FROM_HERE, test_sync_impl);
993
994 // Reset the pointer now so the InterfacePtr associated resources can be
995 // deleted before the background thread's message loop is invalidated.
996 thread_safe_ptr = nullptr;
997 }
998
999 TEST_F(InterfacePtrTest, ThreadSafeSyncCallSameThread) {
1000 TestSyncPtr ptr;
1001 auto impl = base::MakeUnique<TestSyncImpl>(MakeRequest(&ptr));
1002 scoped_refptr<ThreadSafeTestSyncPtr> thread_safe_ptr =
1003 ThreadSafeTestSyncPtr::Create(std::move(ptr));
1004
1005 int32_t output = -1;
1006 (*thread_safe_ptr)->Echo(123, &output);
1007 ASSERT_EQ(123, output);
1008 }
1009
935 } // namespace 1010 } // namespace
936 } // namespace test 1011 } // namespace test
937 } // namespace mojo 1012 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698