| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/task_scheduler/task_tracker_posix.h" |
| 6 |
| 7 #include <unistd.h> |
| 8 |
| 9 #include <utility> |
| 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/files/file_descriptor_watcher_posix.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/posix/eintr_wrapper.h" |
| 16 #include "base/run_loop.h" |
| 17 #include "base/sequence_token.h" |
| 18 #include "base/task_scheduler/task.h" |
| 19 #include "base/task_scheduler/task_traits.h" |
| 20 #include "base/time/time.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace base { |
| 24 namespace internal { |
| 25 |
| 26 // Verify that TaskTrackerPosix runs a Task it receives. |
| 27 TEST(TaskSchedulerTaskTrackerPosixTest, RunTask) { |
| 28 MessageLoopForIO message_loop; |
| 29 bool did_run = false; |
| 30 auto task = MakeUnique<Task>( |
| 31 FROM_HERE, |
| 32 Bind([](bool* did_run) { *did_run = true; }, Unretained(&did_run)), |
| 33 TaskTraits(), TimeDelta()); |
| 34 TaskTrackerPosix tracker(&message_loop); |
| 35 |
| 36 EXPECT_TRUE(tracker.WillPostTask(task.get())); |
| 37 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); |
| 38 EXPECT_TRUE(did_run); |
| 39 } |
| 40 |
| 41 // Verify that FileDescriptorWatcher::WatchReadable() can be called from a task |
| 42 // running in TaskTrackerPosix without a crash. |
| 43 TEST(TaskSchedulerTaskTrackerPosixTest, FileDescriptorWatcher) { |
| 44 MessageLoopForIO message_loop; |
| 45 int fds[2]; |
| 46 ASSERT_EQ(0, pipe(fds)); |
| 47 auto task = MakeUnique<Task>( |
| 48 FROM_HERE, Bind(IgnoreResult(&FileDescriptorWatcher::WatchReadable), |
| 49 fds[0], Bind(&DoNothing)), |
| 50 TaskTraits(), TimeDelta()); |
| 51 TaskTrackerPosix tracker(&message_loop); |
| 52 |
| 53 EXPECT_TRUE(tracker.WillPostTask(task.get())); |
| 54 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); |
| 55 |
| 56 // Run the MessageLoop to allow the read watch to be registered and |
| 57 // unregistered. This prevents a memory leak. |
| 58 RunLoop().RunUntilIdle(); |
| 59 |
| 60 EXPECT_EQ(0, IGNORE_EINTR(close(fds[0]))); |
| 61 EXPECT_EQ(0, IGNORE_EINTR(close(fds[1]))); |
| 62 } |
| 63 |
| 64 } // namespace internal |
| 65 } // namespace base |
| OLD | NEW |