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

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

Issue 2729133004: Mojo C++ bindings: keep connection error handler on the stack while running it. (Closed)
Patch Set: 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
« no previous file with comments | « mojo/public/cpp/bindings/lib/interface_endpoint_client.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 EXPECT_EQ("greetings", description); 788 EXPECT_EQ("greetings", description);
789 quit_closure.Run(); 789 quit_closure.Run();
790 }, 790 },
791 run_loop.QuitClosure())); 791 run_loop.QuitClosure()));
792 792
793 request.ResetWithReason(88u, "greetings"); 793 request.ResetWithReason(88u, "greetings");
794 794
795 run_loop.Run(); 795 run_loop.Run();
796 } 796 }
797 797
798 TEST_F(InterfacePtrTest, CallbackOwnsInterfacePtr) { 798 TEST_F(InterfacePtrTest, CallbackIsPassedInterfacePtr) {
799 sample::PingTestPtr ptr; 799 sample::PingTestPtr ptr;
800 sample::PingTestRequest request(&ptr); 800 sample::PingTestRequest request(&ptr);
801 801
802 base::RunLoop run_loop; 802 base::RunLoop run_loop;
803 803
804 // Make a call with the proxy's lifetime bound to the response callback. 804 // Make a call with the proxy's lifetime bound to the response callback.
805 sample::PingTest* raw_proxy = ptr.get(); 805 sample::PingTest* raw_proxy = ptr.get();
806 ptr.set_connection_error_handler(run_loop.QuitClosure()); 806 ptr.set_connection_error_handler(run_loop.QuitClosure());
807 raw_proxy->Ping( 807 raw_proxy->Ping(
808 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr))); 808 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr)));
809 809
810 // Trigger an error on |ptr|. This will ultimately lead to the proxy's 810 // Trigger an error on |ptr|. This will ultimately lead to the proxy's
811 // response callbacks being destroyed, which will in turn lead to the proxy 811 // response callbacks being destroyed, which will in turn lead to the proxy
812 // being destroyed. This should not crash. 812 // being destroyed. This should not crash.
813 request.PassMessagePipe(); 813 request.PassMessagePipe();
814 run_loop.Run(); 814 run_loop.Run();
815 } 815 }
816 816
817 TEST_F(InterfacePtrTest, ConnectionErrorHandlerOwnsInterfacePtr) {
818 sample::PingTestPtr* ptr = new sample::PingTestPtr;
819 sample::PingTestRequest request(ptr);
820
821 base::RunLoop run_loop;
822
823 // Make a call with |ptr|'s lifetime bound to the connection error handler
824 // callback.
825 ptr->set_connection_error_handler(base::Bind(
826 [](const base::Closure& quit, sample::PingTestPtr* ptr) {
827 ptr->reset();
828 quit.Run();
829 },
830 run_loop.QuitClosure(), base::Owned(ptr)));
831
832 // Trigger an error on |ptr|. In the error handler |ptr| is reset. This
833 // shouldn't immediately destroy the callback (and |ptr| that it owns), before
834 // the callback is completed.
835 request.PassMessagePipe();
836 run_loop.Run();
837 }
838
817 TEST_F(InterfacePtrTest, ThreadSafeInterfacePointer) { 839 TEST_F(InterfacePtrTest, ThreadSafeInterfacePointer) {
818 math::CalculatorPtr ptr; 840 math::CalculatorPtr ptr;
819 MathCalculatorImpl calc_impl(MakeRequest(&ptr)); 841 MathCalculatorImpl calc_impl(MakeRequest(&ptr));
820 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr = 842 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr =
821 math::ThreadSafeCalculatorPtr::Create(std::move(ptr)); 843 math::ThreadSafeCalculatorPtr::Create(std::move(ptr));
822 844
823 base::RunLoop run_loop; 845 base::RunLoop run_loop;
824 846
825 // Create and start the thread from where we'll call the interface pointer. 847 // Create and start the thread from where we'll call the interface pointer.
826 base::Thread other_thread("service test thread"); 848 base::Thread other_thread("service test thread");
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl); 928 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl);
907 929
908 // Reset the pointer now so the InterfacePtr associated resources can be 930 // Reset the pointer now so the InterfacePtr associated resources can be
909 // deleted before the background thread's message loop is invalidated. 931 // deleted before the background thread's message loop is invalidated.
910 thread_safe_ptr = nullptr; 932 thread_safe_ptr = nullptr;
911 } 933 }
912 934
913 } // namespace 935 } // namespace
914 } // namespace test 936 } // namespace test
915 } // namespace mojo 937 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/interface_endpoint_client.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698