// Copyright 2016 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/task_manager/sampling/task_group.h" #include <memory> #include <string> #include "base/functional/bind.h" #include "base/functional/callback_helpers.h" #include "base/memory/ref_counted.h" #include "base/run_loop.h" #include "base/task/sequenced_task_runner.h" #include "base/test/gtest_util.h" #include "chrome/browser/task_manager/sampling/shared_sampler.h" #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "content/public/test/browser_task_environment.h" #include "gpu/ipc/common/memory_stats.h" #include "testing/gtest/include/gtest/gtest.h" namespace task_manager { namespace { class FakeTask : public Task { … }; } // namespace class TaskGroupTest : public testing::Test { … }; // Verify that calling TaskGroup::Refresh() without specifying any fields to // refresh trivially completes, without crashing or leaving things in a weird // state. TEST_F(TaskGroupTest, NullRefresh) { … } // Ensure that refreshing an empty TaskGroup causes a DCHECK (if enabled). TEST_F(TaskGroupTest, RefreshZeroTasksDeathTest) { … } // Verify that Refresh() for a field which can be refreshed synchronously // completes immediately, without leaving any background calculations pending. TEST_F(TaskGroupTest, SyncRefresh) { … } // Some fields are refreshed on a per-TaskGroup basis, but require asynchronous // work (e.g. on another thread) to complete. Cpu is such a field, so verify // that it is correctly reported as requiring background calculations. TEST_F(TaskGroupTest, AsyncRefresh) { … } // Some fields are refreshed system-wide, via a SharedSampler, which completes // asynchronously. Idle wakeups are reported via SharedSampler on some systems // and via asynchronous refresh on others, so we just test that that field // requires background calculations, similarly to the AsyncRefresh test above. TEST_F(TaskGroupTest, SharedAsyncRefresh) { … } // Ensure that if NaCl is enabled then calling Refresh with a NaCl Task active // results in asynchronous completion. Also verifies that if NaCl is disabled // then completion is synchronous. TEST_F(TaskGroupTest, NaclRefreshWithTask) { … } // Test the task has correct network usage rate when zero bytes read and sent. TEST_F(TaskGroupTest, NetworkBytesSentReadZero) { … } // Test the task has correct network usage rate when only having read bytes. TEST_F(TaskGroupTest, NetworkBytesRead) { … } // Test the task has correct network usage rate when only having sent bytes. TEST_F(TaskGroupTest, NetworkBytesSent) { … } // Test the task has correct network usage rate when only having read bytes and // having a non 1s refresh time. TEST_F(TaskGroupTest, NetworkBytesRead2SecRefresh) { … } // Test the task has correct network usage rate when only having sent bytes and // having a non 1s refresh time. TEST_F(TaskGroupTest, NetworkBytesSent2SecRefresh) { … } // Tests the task has correct usage on receiving and then sending bytes. TEST_F(TaskGroupTest, NetworkBytesReadThenSent) { … } // Tests the task has correct usage rate on sending and then receiving bytes. TEST_F(TaskGroupTest, NetworkBytesSentThenRead) { … } // Tests that the network usage rate goes to 0 after reading bytes then a // refresh with no traffic and that cumulative is still correct. TEST_F(TaskGroupTest, NetworkBytesReadRefreshNone) { … } // Tests that the network usage rate goes to 0 after sending bytes then a // refresh with no traffic and that cumulative is still correct. TEST_F(TaskGroupTest, NetworkBytesSentRefreshNone) { … } // Tests that the network usage rate goes to 0 after a refresh with no traffic // and that cumulative is still correct. TEST_F(TaskGroupTest, NetworkBytesTransferredRefreshNone) { … } // Tests that 2 tasks in 1 task group that both read bytes have correct usage // rates and correct cumulative network usage. TEST_F(TaskGroupTest, NetworkBytesReadAsGroup) { … } // Tests that the network usage rate does not get affected until a refresh is // called and that the cumulative is as up to date as possible. TEST_F(TaskGroupTest, NetworkBytesTransferredRefreshOutOfOrder) { … } // Tests that 2 tasks in 1 task group that both sent bytes have correct usage // rates and correct cumulative network usage. TEST_F(TaskGroupTest, NetworkBytesSentAsGroup) { … } // Tests that 2 tasks in 1 task group that have one sending and one reading // have correct usage rates for the group and correct cumulative network usage. TEST_F(TaskGroupTest, NetworkBytesTransferredAsGroup) { … } // Tests that after two tasks in a task group read bytes that a refresh will // zero out network usage rate while maintaining the correct cumulative network // usage. TEST_F(TaskGroupTest, NetworkBytesReadAsGroupThenNone) { … } // Tests that after two tasks in a task group send bytes that a refresh will // zero out network usage rate while maintaining the correct cumulative network // usage. TEST_F(TaskGroupTest, NetworkBytesSentAsGroupThenNone) { … } // Tests that after two tasks in a task group transferred bytes that a refresh // will zero out network usage rate while maintaining the correct cumulative // network usage. TEST_F(TaskGroupTest, NetworkBytesTransferredAsGroupThenNone) { … } // Test the task can't be killed with a PID of base::kNullProcessId. TEST_F(TaskGroupTest, TaskWithPidZero) { … } // Verify that calling TaskGroup::Refresh() on a VM task group with no supported // refresh flags trivially completes. TEST_F(TaskGroupTest, UnsupportedVMRefreshFlags) { … } } // namespace task_manager