1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/process_snapshot/process_snapshot_server.h"
#include <memory>
#include "base/run_loop.h"
#include "base/time/time.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
// -----------------------------------------------------------------------------
// TestObserver:
class TestObserver : public ProcessSnapshotServer::Observer {
public:
explicit TestObserver(base::TimeDelta desired_refresh_time)
: ProcessSnapshotServer::Observer(desired_refresh_time) {}
~TestObserver() override = default;
base::Time last_refresh_time() const { return last_refresh_time_; }
void WaitForRefresh() {
if (!refresh_received_) {
run_loop_ = std::make_unique<base::RunLoop>();
run_loop_->Run();
run_loop_.reset();
}
refresh_received_ = false;
}
// ProcessSnapshotServer::Observer:
void OnProcessSnapshotRefreshed(
const base::ProcessIterator::ProcessEntries& snapshot) override {
last_refresh_time_ = base::Time::Now();
refresh_received_ = true;
if (run_loop_)
run_loop_->Quit();
}
private:
base::Time last_refresh_time_;
std::unique_ptr<base::RunLoop> run_loop_;
bool refresh_received_ = false;
};
// -----------------------------------------------------------------------------
// ProcessSnapshotServerTest:
class ProcessSnapshotServerTest : public testing::Test {
public:
ProcessSnapshotServerTest() = default;
ProcessSnapshotServerTest(const ProcessSnapshotServerTest&) = delete;
ProcessSnapshotServerTest& operator=(const ProcessSnapshotServerTest&) =
delete;
~ProcessSnapshotServerTest() override = default;
ProcessSnapshotServer* server() const { return ProcessSnapshotServer::Get(); }
private:
content::BrowserTaskEnvironment task_environment_;
};
TEST_F(ProcessSnapshotServerTest, FirstObserverTriggersImmediateRefresh) {
constexpr base::TimeDelta kDesiredDelay = base::Seconds(20);
TestObserver observer(kDesiredDelay);
server()->AddObserver(&observer);
observer.WaitForRefresh();
EXPECT_LT(base::Time::Now() - observer.last_refresh_time(), kDesiredDelay);
server()->RemoveObserver(&observer);
}
TEST_F(ProcessSnapshotServerTest, AddRemoveObservers) {
constexpr base::TimeDelta kDelay1 = base::Seconds(10);
constexpr base::TimeDelta kDelay2 = base::Seconds(5);
constexpr base::TimeDelta kDelay3 = base::Seconds(20);
TestObserver observer1(kDelay1);
TestObserver observer2(kDelay2);
TestObserver observer3(kDelay3);
server()->AddObserver(&observer1);
const auto& timer = server()->GetTimerForTesting();
EXPECT_TRUE(timer.IsRunning());
EXPECT_EQ(kDelay1, timer.GetCurrentDelay());
// Adding an observer with a smaller delay updates the timer.
server()->AddObserver(&observer2);
EXPECT_EQ(kDelay2, timer.GetCurrentDelay());
// Adding an observer with a larger delay should not change the timer delay.
server()->AddObserver(&observer3);
EXPECT_EQ(kDelay2, timer.GetCurrentDelay());
// Removing observers with delay larger than the minimum one shouldn't change
// the timer delay.
server()->RemoveObserver(&observer3);
EXPECT_EQ(kDelay2, timer.GetCurrentDelay());
// Removing the observer with the minimum delay will pick the next minimum.
server()->RemoveObserver(&observer2);
EXPECT_EQ(kDelay1, timer.GetCurrentDelay());
// Removing the last observer should stop the timer.
server()->RemoveObserver(&observer1);
EXPECT_FALSE(timer.IsRunning());
}
} // namespace
} // namespace ash
|