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

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

Issue 2506383002: Mojo: adding a thread safe associated interface ptr. (Closed)
Patch Set: Fixed BUILD.gn 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 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"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/callback_helpers.h" 12 #include "base/callback_helpers.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h" 14 #include "base/run_loop.h"
15 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
16 #include "base/threading/sequenced_task_runner_handle.h"
16 #include "base/threading/thread.h" 17 #include "base/threading/thread.h"
17 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
18 #include "mojo/public/cpp/bindings/associated_binding.h" 19 #include "mojo/public/cpp/bindings/associated_binding.h"
19 #include "mojo/public/cpp/bindings/associated_group.h" 20 #include "mojo/public/cpp/bindings/associated_group.h"
20 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" 21 #include "mojo/public/cpp/bindings/associated_interface_ptr.h"
21 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" 22 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h"
22 #include "mojo/public/cpp/bindings/associated_interface_request.h" 23 #include "mojo/public/cpp/bindings/associated_interface_request.h"
23 #include "mojo/public/cpp/bindings/binding.h" 24 #include "mojo/public/cpp/bindings/binding.h"
24 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" 25 #include "mojo/public/cpp/bindings/lib/multiplex_router.h"
25 #include "mojo/public/cpp/bindings/strong_binding.h" 26 #include "mojo/public/cpp/bindings/strong_binding.h"
(...skipping 960 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 EXPECT_EQ("long time no see", description); 987 EXPECT_EQ("long time no see", description);
987 quit_closure.Run(); 988 quit_closure.Run();
988 }, 989 },
989 run_loop.QuitClosure())); 990 run_loop.QuitClosure()));
990 991
991 request.ResetWithReason(789u, "long time no see"); 992 request.ResetWithReason(789u, "long time no see");
992 993
993 run_loop.Run(); 994 run_loop.Run();
994 } 995 }
995 996
997 TEST_F(AssociatedInterfaceTest, ThreadSafeAssociatedInterfacePtr) {
998 IntegerSenderConnectionPtr connection_ptr;
999 IntegerSenderConnectionImpl connection(GetProxy(&connection_ptr));
1000
1001 IntegerSenderAssociatedPtr sender;
1002 connection_ptr->GetSender(
1003 GetProxy(&sender, connection_ptr.associated_group()));
1004
1005 scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr> thread_safe_sender =
1006 ThreadSafeIntegerSenderAssociatedPtr::Create(std::move(sender));
1007
1008 {
1009 // Test the thread safe pointer can be used from the interface ptr thread.
1010 int32_t echoed_value = 0;
1011 base::RunLoop run_loop;
1012 (*thread_safe_sender)->Echo(123, base::Bind(&CaptureInt32, &echoed_value,
1013 run_loop.QuitClosure()));
1014 run_loop.Run();
1015 EXPECT_EQ(123, echoed_value);
1016 }
1017
1018 // Test the thread safe pointer can be used from another thread.
1019 base::RunLoop run_loop;
1020 base::Thread other_thread("service test thread");
1021 other_thread.Start();
1022
1023 auto run_method = base::Bind(
1024 [](const scoped_refptr<base::TaskRunner>& main_task_runner,
1025 const base::Closure& quit_closure,
1026 const scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr>&
1027 thread_safe_sender) {
1028 auto done_callback = base::Bind(
1029 [](const scoped_refptr<base::TaskRunner>& main_task_runner,
1030 const base::Closure& quit_closure,
1031 base::PlatformThreadId thread_id,
1032 int32_t result) {
1033 EXPECT_EQ(123, result);
1034 // Validate the callback is invoked on the calling thread.
1035 EXPECT_EQ(thread_id, base::PlatformThread::CurrentId());
1036 // Notify the run_loop to quit.
1037 main_task_runner->PostTask(FROM_HERE, quit_closure);
1038 });
1039 (*thread_safe_sender)->Echo(
1040 123, base::Bind(done_callback, main_task_runner, quit_closure,
1041 base::PlatformThread::CurrentId()));
1042 },
1043 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(),
1044 thread_safe_sender);
1045 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method);
1046
1047 // Block until the method callback is called on the background thread.
1048 run_loop.Run();
1049 }
1050
996 } // namespace 1051 } // namespace
997 } // namespace test 1052 } // namespace test
998 } // namespace mojo 1053 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698