| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "media/base/bind_to_current_loop.h" | 5 #include "media/base/bind_to_current_loop.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/free_deleter.h" | 10 #include "base/memory/free_deleter.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/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
| 14 #include "base/threading/thread.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace media { | 17 namespace media { |
| 17 | 18 |
| 18 void BoundBoolSet(bool* var, bool val) { | 19 void BoundBoolSet(bool* var, bool val) { |
| 19 *var = val; | 20 *var = val; |
| 20 } | 21 } |
| 21 | 22 |
| 22 void BoundBoolSetFromScopedPtr(bool* var, std::unique_ptr<bool> val) { | 23 void BoundBoolSetFromScopedPtr(bool* var, std::unique_ptr<bool> val) { |
| 23 *var = *val; | 24 *var = *val; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 35 | 36 |
| 36 void BoundBoolSetFromConstRef(bool* var, const bool& val) { | 37 void BoundBoolSetFromConstRef(bool* var, const bool& val) { |
| 37 *var = val; | 38 *var = val; |
| 38 } | 39 } |
| 39 | 40 |
| 40 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { | 41 void BoundIntegersSet(int* a_var, int* b_var, int a_val, int b_val) { |
| 41 *a_var = a_val; | 42 *a_var = a_val; |
| 42 *b_var = b_val; | 43 *b_var = b_val; |
| 43 } | 44 } |
| 44 | 45 |
| 46 struct ThreadRestrictionChecker { |
| 47 ThreadRestrictionChecker() : bound_loop_(base::MessageLoop::current()) {} |
| 48 |
| 49 void Run() { EXPECT_EQ(bound_loop_, base::MessageLoop::current()); } |
| 50 |
| 51 ~ThreadRestrictionChecker() { |
| 52 EXPECT_EQ(bound_loop_, base::MessageLoop::current()); |
| 53 } |
| 54 |
| 55 base::MessageLoop* bound_loop_; |
| 56 }; |
| 57 |
| 58 void RunAndClearReference(base::Closure cb) { |
| 59 cb.Run(); |
| 60 } |
| 61 |
| 62 void ClearReference(base::Closure cb) {} |
| 63 |
| 45 // Various tests that check that the bound function is only actually executed | 64 // Various tests that check that the bound function is only actually executed |
| 46 // on the message loop, not during the original Run. | 65 // on the message loop, not during the original Run. |
| 47 class BindToCurrentLoopTest : public ::testing::Test { | 66 class BindToCurrentLoopTest : public ::testing::Test { |
| 48 protected: | 67 protected: |
| 49 base::MessageLoop loop_; | 68 base::MessageLoop loop_; |
| 50 }; | 69 }; |
| 51 | 70 |
| 52 TEST_F(BindToCurrentLoopTest, Closure) { | 71 TEST_F(BindToCurrentLoopTest, Closure) { |
| 53 // Test the closure is run inside the loop, not outside it. | 72 // Test the closure is run inside the loop, not outside it. |
| 54 base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 73 base::WaitableEvent waiter(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 base::Callback<void(int, int)> cb = BindToCurrentLoop(base::Bind( | 183 base::Callback<void(int, int)> cb = BindToCurrentLoop(base::Bind( |
| 165 &BoundIntegersSet, &a, &b)); | 184 &BoundIntegersSet, &a, &b)); |
| 166 cb.Run(1, -1); | 185 cb.Run(1, -1); |
| 167 EXPECT_EQ(a, 0); | 186 EXPECT_EQ(a, 0); |
| 168 EXPECT_EQ(b, 0); | 187 EXPECT_EQ(b, 0); |
| 169 base::RunLoop().RunUntilIdle(); | 188 base::RunLoop().RunUntilIdle(); |
| 170 EXPECT_EQ(a, 1); | 189 EXPECT_EQ(a, 1); |
| 171 EXPECT_EQ(b, -1); | 190 EXPECT_EQ(b, -1); |
| 172 } | 191 } |
| 173 | 192 |
| 193 TEST_F(BindToCurrentLoopTest, DestroyedOnBoundLoop) { |
| 194 base::Thread target_thread("testing"); |
| 195 ASSERT_TRUE(target_thread.Start()); |
| 196 |
| 197 // Ensure that the bound object is also destroyed on the correct thread even |
| 198 // if the last reference to the callback is dropped on the other thread. |
| 199 // TODO(tzik): Remove RunAndClearReference once TaskRunner migrates to |
| 200 // OnceClosure. RunAndCleareReference is needed to ensure no reference to |cb| |
| 201 // is left to the original thread. |
| 202 base::Closure cb = BindToCurrentLoop( |
| 203 base::Bind(&ThreadRestrictionChecker::Run, |
| 204 base::MakeUnique<ThreadRestrictionChecker>())); |
| 205 target_thread.task_runner()->PostTask( |
| 206 FROM_HERE, base::Bind(&RunAndClearReference, base::Passed(&cb))); |
| 207 ASSERT_FALSE(cb); |
| 208 target_thread.FlushForTesting(); |
| 209 base::RunLoop().RunUntilIdle(); |
| 210 |
| 211 // Ensure that the bound object is destroyed on the target thread even if |
| 212 // the callback is destroyed without invocation. |
| 213 cb = BindToCurrentLoop( |
| 214 base::Bind(&ThreadRestrictionChecker::Run, |
| 215 base::MakeUnique<ThreadRestrictionChecker>())); |
| 216 target_thread.task_runner()->PostTask( |
| 217 FROM_HERE, base::Bind(&ClearReference, base::Passed(&cb))); |
| 218 target_thread.FlushForTesting(); |
| 219 ASSERT_FALSE(cb); |
| 220 base::RunLoop().RunUntilIdle(); |
| 221 |
| 222 target_thread.Stop(); |
| 223 } |
| 224 |
| 174 } // namespace media | 225 } // namespace media |
| OLD | NEW |