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

Side by Side Diff: base/threading/sequenced_worker_pool_unittest.cc

Issue 1414793009: Allow SequencedTaskRunnerHandle::Get() while running unsequenced tasks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove base:: Created 5 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 (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 "base/threading/sequenced_worker_pool.h" 5 #include "base/threading/sequenced_worker_pool.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/synchronization/condition_variable.h" 14 #include "base/synchronization/condition_variable.h"
15 #include "base/synchronization/lock.h" 15 #include "base/synchronization/lock.h"
16 #include "base/synchronization/waitable_event.h"
16 #include "base/test/sequenced_task_runner_test_template.h" 17 #include "base/test/sequenced_task_runner_test_template.h"
17 #include "base/test/sequenced_worker_pool_owner.h" 18 #include "base/test/sequenced_worker_pool_owner.h"
18 #include "base/test/task_runner_test_template.h" 19 #include "base/test/task_runner_test_template.h"
19 #include "base/test/test_timeouts.h" 20 #include "base/test/test_timeouts.h"
20 #include "base/threading/platform_thread.h" 21 #include "base/threading/platform_thread.h"
21 #include "base/time/time.h" 22 #include "base/time/time.h"
22 #include "base/tracked_objects.h" 23 #include "base/tracked_objects.h"
23 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
24 25
25 namespace base { 26 namespace base {
(...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 // Should be fine to call on an idle instance with all threads created, and 898 // Should be fine to call on an idle instance with all threads created, and
898 // spamming the method shouldn't deadlock or confuse the class. 899 // spamming the method shouldn't deadlock or confuse the class.
899 pool()->FlushForTesting(); 900 pool()->FlushForTesting();
900 pool()->FlushForTesting(); 901 pool()->FlushForTesting();
901 902
902 // Should be fine to call after shutdown too. 903 // Should be fine to call after shutdown too.
903 pool()->Shutdown(); 904 pool()->Shutdown();
904 pool()->FlushForTesting(); 905 pool()->FlushForTesting();
905 } 906 }
906 907
907 namespace { 908 // Helper method for VerifyCurrentSequencedTaskRunner().
908 909 void VerifySequencedTaskRunnerRunsOnCurrentThread(
909 void CheckWorkerPoolAndSequenceToken( 910 const scoped_refptr<SequencedTaskRunner>& task_runner,
danakj 2015/11/17 22:03:39 nit: take smart pointers by value if you want to h
Bernhard Bauer 2015/11/18 13:15:50 Done.
910 const scoped_refptr<SequencedWorkerPool>& expected_pool, 911 bool should_run_on_current_thread,
911 SequencedWorkerPool::SequenceToken expected_token) { 912 const Closure& callback) {
912 SequencedWorkerPool::SequenceToken token = 913 EXPECT_EQ(should_run_on_current_thread,
913 SequencedWorkerPool::GetSequenceTokenForCurrentThread(); 914 task_runner->RunsTasksOnCurrentThread());
914 EXPECT_EQ(expected_token.ToString(), token.ToString()); 915 callback.Run();
915
916 scoped_refptr<SequencedWorkerPool> pool =
917 SequencedWorkerPool::GetWorkerPoolForCurrentThread();
918 EXPECT_EQ(expected_pool, pool);
919 } 916 }
920 917
921 } // namespace 918 void VerifyCurrentSequencedTaskRunner(
919 scoped_refptr<SequencedTaskRunner> expected_task_runner,
danakj 2015/11/17 22:03:39 and here?
Bernhard Bauer 2015/11/18 13:15:50 But here I do want to take a reference (and then p
danakj 2015/11/20 19:18:43 You don't need a ref here to pass to the closure.
920 bool expected_equal,
921 const Closure& callback) {
922 scoped_refptr<SequencedTaskRunner> task_runner =
923 SequencedWorkerPool::GetSequencedTaskRunnerForCurrentThread();
922 924
923 TEST_F(SequencedWorkerPoolTest, GetWorkerPoolAndSequenceTokenForCurrentThread) { 925 // If the expected task runner is null, get another one for the current
926 // thread. They should be the same.
927 if (!expected_task_runner) {
928 expected_task_runner =
929 SequencedWorkerPool::GetSequencedTaskRunnerForCurrentThread();
930 }
931
932 // SequencedTaskRunner does not allow directly checking for equality, but we
933 // can post a task to one task runner and verify that the other task runner
934 // is on the same sequence.
935 task_runner->PostTask(FROM_HERE,
936 Bind(&VerifySequencedTaskRunnerRunsOnCurrentThread,
937 expected_task_runner, expected_equal, callback));
danakj 2015/11/17 22:03:39 nit: move() the task runner smart pointer, no need
Bernhard Bauer 2015/11/18 13:15:50 Moving the scoped_refptr (see above).
938 }
939
940 TEST_F(SequencedWorkerPoolTest, GetSequencedTaskRunnerForCurrentThread) {
924 EnsureAllWorkersCreated(); 941 EnsureAllWorkersCreated();
925 942
926 // The current thread should have neither a worker pool nor a sequence token. 943 // The current thread should not have a sequenced task runner from a
927 SequencedWorkerPool::SequenceToken local_token = 944 // worker pool.
928 SequencedWorkerPool::GetSequenceTokenForCurrentThread(); 945 scoped_refptr<SequencedTaskRunner> local_task_runner =
929 scoped_refptr<SequencedWorkerPool> local_pool = 946 SequencedWorkerPool::GetSequencedTaskRunnerForCurrentThread();
930 SequencedWorkerPool::GetWorkerPoolForCurrentThread(); 947 EXPECT_FALSE(local_task_runner);
931 EXPECT_FALSE(local_token.IsValid()) << local_token.ToString();
932 EXPECT_FALSE(local_pool);
933 948
934 SequencedWorkerPool::SequenceToken token1 = pool()->GetSequenceToken(); 949 WaitableEvent event(false, false);
935 SequencedWorkerPool::SequenceToken token2 = pool()->GetSequenceToken(); 950 Closure signal = Bind(&WaitableEvent::Signal, Unretained(&event));
936 pool()->PostSequencedWorkerTask( 951 scoped_refptr<SequencedTaskRunner> task_runner_1 =
937 token1, FROM_HERE, 952 pool()->GetSequencedTaskRunner(SequencedWorkerPool::GetSequenceToken());
938 base::Bind(&CheckWorkerPoolAndSequenceToken, pool(), token1)); 953 scoped_refptr<SequencedTaskRunner> task_runner_2 =
939 pool()->PostSequencedWorkerTask( 954 pool()->GetSequencedTaskRunner(SequencedWorkerPool::GetSequenceToken());
940 token2, FROM_HERE, 955 task_runner_1->PostTask(FROM_HERE, Bind(&VerifyCurrentSequencedTaskRunner,
941 base::Bind(&CheckWorkerPoolAndSequenceToken, pool(), token2)); 956 task_runner_1, true, signal));
danakj 2015/11/17 22:03:39 .get() on all these, to pass the runner pointer. w
Bernhard Bauer 2015/11/18 13:15:50 Apparently, if the first argument to a Closure is
danakj 2015/11/18 19:06:31 Unless you use base::Unretained, I thought?
gab 2015/11/18 22:37:33 Right.
Bernhard Bauer 2015/11/19 10:15:12 So... you want me to do that then? Done (although
danakj 2015/11/19 18:49:07 Yes please, it's not the overhead I'm worried abou
danakj 2015/11/20 19:18:43 I was wrong :( Didn't see the GetSTRForCurrentThre
957 event.Wait();
958 task_runner_2->PostTask(FROM_HERE, Bind(&VerifyCurrentSequencedTaskRunner,
959 task_runner_2, true, signal));
960 event.Wait();
942 961
943 pool()->PostWorkerTask(FROM_HERE, 962 task_runner_1->PostTask(FROM_HERE, Bind(&VerifyCurrentSequencedTaskRunner,
944 base::Bind(&CheckWorkerPoolAndSequenceToken, pool(), 963 task_runner_2, false, signal));
945 SequencedWorkerPool::SequenceToken())); 964 event.Wait();
946 965
947 pool()->FlushForTesting(); 966 pool()->PostWorkerTask(FROM_HERE, Bind(&VerifyCurrentSequencedTaskRunner,
967 nullptr, true, signal));
968 event.Wait();
948 } 969 }
949 970
950 TEST(SequencedWorkerPoolRefPtrTest, ShutsDownCleanWithContinueOnShutdown) { 971 TEST(SequencedWorkerPoolRefPtrTest, ShutsDownCleanWithContinueOnShutdown) {
951 MessageLoop loop; 972 MessageLoop loop;
952 scoped_refptr<SequencedWorkerPool> pool(new SequencedWorkerPool(3, "Pool")); 973 scoped_refptr<SequencedWorkerPool> pool(new SequencedWorkerPool(3, "Pool"));
953 scoped_refptr<SequencedTaskRunner> task_runner = 974 scoped_refptr<SequencedTaskRunner> task_runner =
954 pool->GetSequencedTaskRunnerWithShutdownBehavior( 975 pool->GetSequencedTaskRunnerWithShutdownBehavior(
955 pool->GetSequenceToken(), 976 pool->GetSequenceToken(),
956 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); 977 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
957 978
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1078 SequencedWorkerPoolSequencedTaskRunner, SequencedTaskRunnerTest, 1099 SequencedWorkerPoolSequencedTaskRunner, SequencedTaskRunnerTest,
1079 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); 1100 SequencedWorkerPoolSequencedTaskRunnerTestDelegate);
1080 INSTANTIATE_TYPED_TEST_CASE_P( 1101 INSTANTIATE_TYPED_TEST_CASE_P(
1081 SequencedWorkerPoolSequencedTaskRunner, 1102 SequencedWorkerPoolSequencedTaskRunner,
1082 SequencedTaskRunnerDelayedTest, 1103 SequencedTaskRunnerDelayedTest,
1083 SequencedWorkerPoolSequencedTaskRunnerTestDelegate); 1104 SequencedWorkerPoolSequencedTaskRunnerTestDelegate);
1084 1105
1085 } // namespace 1106 } // namespace
1086 1107
1087 } // namespace base 1108 } // namespace base
OLDNEW
« base/threading/sequenced_worker_pool.cc ('K') | « base/threading/sequenced_worker_pool.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698