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

Side by Side Diff: base/task_scheduler/task_scheduler_impl_unittest.cc

Issue 2857103005: Exempt the Service Thread from BLOCK_SHUTDOWN DCHECKs (Closed)
Patch Set: CR Feedback + Additional DCHECK_IS_ON due to the way DCHECKs are implemented Created 3 years, 7 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/task_scheduler/task_scheduler_impl.h" 5 #include "base/task_scheduler/task_scheduler_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/bind_helpers.h" 14 #include "base/bind_helpers.h"
15 #include "base/callback.h" 15 #include "base/callback.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "base/synchronization/waitable_event.h" 19 #include "base/synchronization/waitable_event.h"
20 #include "base/task_scheduler/scheduler_worker_pool_params.h" 20 #include "base/task_scheduler/scheduler_worker_pool_params.h"
21 #include "base/task_scheduler/task_traits.h" 21 #include "base/task_scheduler/task_traits.h"
22 #include "base/task_scheduler/test_task_factory.h" 22 #include "base/task_scheduler/test_task_factory.h"
23 #include "base/test/test_timeouts.h" 23 #include "base/test/test_timeouts.h"
24 #include "base/threading/platform_thread.h" 24 #include "base/threading/platform_thread.h"
25 #include "base/threading/simple_thread.h" 25 #include "base/threading/simple_thread.h"
26 #include "base/threading/thread.h" 26 #include "base/threading/thread.h"
27 #include "base/threading/thread_restrictions.h" 27 #include "base/threading/thread_restrictions.h"
28 #include "base/time/time.h" 28 #include "base/time/time.h"
29 #include "build/build_config.h"
29 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
30 31
32 #if defined(OS_POSIX)
33 #include <unistd.h>
34
35 #include "base/debug/leak_annotations.h"
36 #include "base/files/file_descriptor_watcher_posix.h"
37 #include "base/files/file_util.h"
38 #include "base/posix/eintr_wrapper.h"
39 #endif // defined(OS_POSIX)
40
31 #if defined(OS_WIN) 41 #if defined(OS_WIN)
32 #include <objbase.h> 42 #include <objbase.h>
33 #endif // defined(OS_WIN) 43 #endif // defined(OS_WIN)
34 44
35 namespace base { 45 namespace base {
36 namespace internal { 46 namespace internal {
37 47
38 namespace { 48 namespace {
39 49
40 struct TraitsExecutionModePair { 50 struct TraitsExecutionModePair {
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 ADD_FAILURE() << "COM STA was not initialized on this thread"; 450 ADD_FAILURE() << "COM STA was not initialized on this thread";
441 CoUninitialize(); 451 CoUninitialize();
442 } 452 }
443 task_ran->Signal(); 453 task_ran->Signal();
444 }, 454 },
445 com_sta_task_runner, Unretained(&task_ran))); 455 com_sta_task_runner, Unretained(&task_ran)));
446 task_ran.Wait(); 456 task_ran.Wait();
447 } 457 }
448 #endif // defined(OS_WIN) 458 #endif // defined(OS_WIN)
449 459
460 TEST_F(TaskSchedulerImplTest, DelayedTasksNotRunAfterShutdown) {
461 StartTaskScheduler();
462 // As with delayed tasks in general, this is racy. If the task does happen to
463 // run after Shutdown within the timeout, it will fail this test.
464 //
465 // The timeout should be set sufficiently long enough to ensure that the
466 // delayed task did not run. 2x is generally good enough.
467 scheduler_.PostDelayedTaskWithTraits(FROM_HERE, TaskTraits(),
468 BindOnce([]() { ADD_FAILURE(); }),
469 TestTimeouts::tiny_timeout());
470 scheduler_.Shutdown();
471 PlatformThread::Sleep(TestTimeouts::tiny_timeout() * 2);
472 }
473
474 #if defined(OS_POSIX)
475
476 TEST_F(TaskSchedulerImplTest, FileDescriptorWatcherNoOpsAfterShutdown) {
477 StartTaskScheduler();
478
479 int pipes[2];
480 ASSERT_EQ(0, pipe(pipes));
481
482 scoped_refptr<TaskRunner> blocking_task_runner =
483 scheduler_.CreateSequencedTaskRunnerWithTraits(
484 TaskTraits().WithShutdownBehavior(
485 TaskShutdownBehavior::BLOCK_SHUTDOWN));
486 blocking_task_runner->PostTask(
487 FROM_HERE,
488 BindOnce(
489 [](int read_fd) {
490 std::unique_ptr<FileDescriptorWatcher::Controller> controller =
491 FileDescriptorWatcher::WatchReadable(
492 read_fd, BindRepeating([]() { NOTREACHED(); }));
493
494 // This test is for components that intentionally leak their
495 // watchers at shutdown. We can't clean |controller| up because its
496 // destructor will assert that it's being called from the correct
497 // sequence. After the task scheduler is shutdown, it is not
498 // possible to run tasks on this sequence.
499 ANNOTATE_LEAKING_OBJECT_PTR(controller.get());
500 controller.release();
gab 2017/05/04 18:11:58 inline controller.release() in the leak macro abov
robliao 2017/05/04 19:01:49 Turns out this breaks the test :-( #define ANNOTAT
501 },
502 pipes[0]));
503
504 scheduler_.Shutdown();
505
506 constexpr char kByte = '!';
507 ASSERT_TRUE(WriteFileDescriptor(pipes[1], &kByte, sizeof(kByte)));
508
509 // Give a chance for the file watcher to fire before closing the handles.
510 PlatformThread::Sleep(TimeDelta::FromMilliseconds(100));
gab 2017/05/04 18:11:58 TestTimeouts::tiny_timeout()
robliao 2017/05/04 19:01:49 This seems to work, switched.
511
512 EXPECT_EQ(0, IGNORE_EINTR(close(pipes[0])));
513 EXPECT_EQ(0, IGNORE_EINTR(close(pipes[1])));
514 }
515 #endif // defined(OS_POSIX)
516
450 } // namespace internal 517 } // namespace internal
451 } // namespace base 518 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698