OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 1037 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1048 thread_safe_sender); | 1048 thread_safe_sender); |
1049 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); | 1049 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); |
1050 | 1050 |
1051 // Block until the method callback is called on the background thread. | 1051 // Block until the method callback is called on the background thread. |
1052 run_loop.Run(); | 1052 run_loop.Run(); |
1053 } | 1053 } |
1054 | 1054 |
1055 struct ForwarderTestContext { | 1055 struct ForwarderTestContext { |
1056 IntegerSenderConnectionPtr connection_ptr; | 1056 IntegerSenderConnectionPtr connection_ptr; |
1057 std::unique_ptr<IntegerSenderConnectionImpl> interface_impl; | 1057 std::unique_ptr<IntegerSenderConnectionImpl> interface_impl; |
1058 IntegerSenderAssociatedRequest sender_request; | |
1058 }; | 1059 }; |
1059 | 1060 |
1060 TEST_F(AssociatedInterfaceTest, BindLaterThreadSafeAssociatedInterfacePtr) { | 1061 TEST_F(AssociatedInterfaceTest, |
1062 ThreadSafeAssociatedInterfacePtrWithTaskRunner) { | |
1061 // Start the thread from where we'll bind the interface pointer. | 1063 // Start the thread from where we'll bind the interface pointer. |
1062 base::Thread other_thread("service test thread"); | 1064 base::Thread other_thread("service test thread"); |
1063 other_thread.Start(); | 1065 other_thread.Start(); |
1064 const scoped_refptr<base::SingleThreadTaskRunner>& other_thread_task_runner = | 1066 const scoped_refptr<base::SingleThreadTaskRunner>& other_thread_task_runner = |
1065 other_thread.message_loop()->task_runner(); | 1067 other_thread.message_loop()->task_runner(); |
1068 | |
1066 ForwarderTestContext* context = new ForwarderTestContext(); | 1069 ForwarderTestContext* context = new ForwarderTestContext(); |
1067 | 1070 IntegerSenderAssociatedPtrInfo sender_info; |
1071 base::WaitableEvent sender_info_bound_event( | |
1072 base::WaitableEvent::ResetPolicy::MANUAL, | |
1073 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
1068 base::WaitableEvent echo_called_event( | 1074 base::WaitableEvent echo_called_event( |
1069 base::WaitableEvent::ResetPolicy::MANUAL, | 1075 base::WaitableEvent::ResetPolicy::MANUAL, |
1070 base::WaitableEvent::InitialState::NOT_SIGNALED); | 1076 base::WaitableEvent::InitialState::NOT_SIGNALED); |
1077 auto setup = [](base::WaitableEvent* sender_info_bound_event, | |
1078 base::WaitableEvent* echo_called_event, | |
1079 IntegerSenderAssociatedPtrInfo* sender_info, | |
1080 ForwarderTestContext* context) { | |
1081 context->interface_impl = base::MakeUnique<IntegerSenderConnectionImpl>( | |
1082 MakeRequest(&context->connection_ptr)); | |
1071 | 1083 |
1072 // Create a ThreadSafeAssociatedPtr that we'll bind from a different thread. | 1084 IntegerSenderAssociatedPtr sender; |
1085 IntegerSenderAssociatedRequest sender_request = | |
1086 MakeRequest(&sender, context->connection_ptr.associated_group()); | |
1087 *sender_info = sender.PassInterface(); | |
1088 | |
1089 // Unblock the main thread as soon as |sender_info| is set. | |
1090 sender_info_bound_event->Signal(); | |
1091 | |
1092 // Block this thread until the main thread calls Echo. | |
1093 echo_called_event->Wait(); | |
1094 | |
1095 context->connection_ptr->GetSender(std::move(sender_request)); | |
yzshen1
2017/02/07 00:06:27
Does it make sense to move this line above 1090 an
Ken Rockot(use gerrit already)
2017/02/07 01:25:32
Done
| |
1096 }; | |
1097 other_thread_task_runner->PostTask( | |
1098 FROM_HERE, | |
1099 base::Bind(setup, &sender_info_bound_event, &echo_called_event, | |
1100 &sender_info, context)); | |
1101 sender_info_bound_event.Wait(); | |
1102 | |
1103 // Create a ThreadSafeAssociatedPtr that binds on the background thread and is | |
1104 // associated with |connection_ptr| there. | |
1073 scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr> thread_safe_ptr = | 1105 scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr> thread_safe_ptr = |
1074 ThreadSafeIntegerSenderAssociatedPtr::CreateUnbound( | 1106 ThreadSafeIntegerSenderAssociatedPtr::Create( |
1075 other_thread_task_runner); | 1107 std::move(sender_info), other_thread_task_runner); |
1076 | 1108 |
1077 base::RunLoop bind_run_loop; | 1109 // Issue a call on the thread-safe ptr immediately. Note that this may happen |
1078 auto run_method = base::Bind( | 1110 // before the interface is bound on the background thread, and that must be |
1079 [](base::WaitableEvent* echo_called_event, | 1111 // OK. |
1080 const scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr>& | |
1081 thread_safe_ptr, | |
1082 ForwarderTestContext* context) { | |
1083 // Wait for echo to be called on the main thread so the interface method | |
1084 // call happens before the bind. | |
1085 echo_called_event->Wait(); | |
1086 | |
1087 // We are on the background thread, create the interface ptr. | |
1088 context->interface_impl = | |
1089 base::MakeUnique<IntegerSenderConnectionImpl>( | |
1090 MakeRequest(&(context->connection_ptr))); | |
1091 IntegerSenderAssociatedPtr sender; | |
1092 context->connection_ptr->GetSender( | |
1093 MakeRequest(&sender, context->connection_ptr.associated_group())); | |
1094 thread_safe_ptr->Bind(std::move(sender)); | |
1095 }, | |
1096 &echo_called_event, thread_safe_ptr, context); | |
1097 | |
1098 other_thread_task_runner->PostTask(FROM_HERE, run_method); | |
1099 | |
1100 { | 1112 { |
1101 // Now we can call methods on the interface from the main thread. | |
1102 auto echo_callback = | 1113 auto echo_callback = |
1103 base::Bind([](const base::Closure& quit_closure, int32_t result) { | 1114 base::Bind([](const base::Closure& quit_closure, int32_t result) { |
1104 EXPECT_EQ(123, result); | 1115 EXPECT_EQ(123, result); |
1105 quit_closure.Run(); | 1116 quit_closure.Run(); |
1106 }); | 1117 }); |
1107 base::RunLoop run_loop; | 1118 base::RunLoop run_loop; |
1108 (*thread_safe_ptr) | 1119 (*thread_safe_ptr) |
1109 ->Echo(123, base::Bind(echo_callback, run_loop.QuitClosure())); | 1120 ->Echo(123, base::Bind(echo_callback, run_loop.QuitClosure())); |
1110 | 1121 |
1111 // Let the bind happen on the background thread. | 1122 // Unblock the background thread so that it can actually bind the |
1123 // underlying AssociatedInterfacePtr and send its request. | |
1112 echo_called_event.Signal(); | 1124 echo_called_event.Signal(); |
1113 | 1125 |
1114 // Block until the method callback is called. | 1126 // Block until the method callback is called. |
1115 run_loop.Run(); | 1127 run_loop.Run(); |
1116 } | 1128 } |
1117 | 1129 |
1118 other_thread_task_runner->DeleteSoon(FROM_HERE, context); | 1130 other_thread_task_runner->DeleteSoon(FROM_HERE, context); |
1119 | 1131 |
1120 // Reset the pointer now so the InterfacePtr associated resources can be | 1132 // Reset the pointer now so the InterfacePtr associated resources can be |
1121 // deleted before the background thread's message loop is invalidated. | 1133 // deleted before the background thread's message loop is invalidated. |
(...skipping 19 matching lines...) Expand all Loading... | |
1141 provider->GetPing( | 1153 provider->GetPing( |
1142 mojo::MakeRequest(&ping, provider.associated_group())); | 1154 mojo::MakeRequest(&ping, provider.associated_group())); |
1143 base::RunLoop run_loop; | 1155 base::RunLoop run_loop; |
1144 ping.set_connection_error_handler(run_loop.QuitClosure()); | 1156 ping.set_connection_error_handler(run_loop.QuitClosure()); |
1145 run_loop.Run(); | 1157 run_loop.Run(); |
1146 } | 1158 } |
1147 | 1159 |
1148 } // namespace | 1160 } // namespace |
1149 } // namespace test | 1161 } // namespace test |
1150 } // namespace mojo | 1162 } // namespace mojo |
OLD | NEW |