File: system_node_impl_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (165 lines) | stat: -rw-r--r-- 6,232 bytes parent folder | download | duplicates (6)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/graph/system_node_impl.h"

#include "base/memory/memory_pressure_listener.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "components/memory_pressure/fake_memory_pressure_monitor.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/page_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/test_support/graph/mock_system_node_observer.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "components/performance_manager/test_support/mock_graphs.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace performance_manager {

namespace {

using SystemNodeImplTest = GraphTestHarness;

}  // namespace

TEST_F(SystemNodeImplTest, SafeDowncast) {
  MockMultiplePagesWithMultipleProcessesGraph mock_graph(graph());
  auto& sys = mock_graph.system;
  SystemNode* node = sys.get();
  EXPECT_EQ(sys.get(), SystemNodeImpl::FromNode(node));
  NodeBase* base = sys.get();
  EXPECT_EQ(base, NodeBase::FromNode(node));
  EXPECT_EQ(static_cast<Node*>(node), base->ToNode());
}

using SystemNodeImplDeathTest = SystemNodeImplTest;

TEST_F(SystemNodeImplDeathTest, SafeDowncast) {
  const NodeBase* system = NodeBase::FromNode(graph()->GetSystemNodeImpl());
  ASSERT_DEATH_IF_SUPPORTED(PageNodeImpl::FromNodeBase(system), "");
}

namespace {

using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;
using testing::_;
using testing::Invoke;
using testing::InvokeWithoutArgs;

class MockObserver : public MockSystemNodeObserver {
 public:
  explicit MockObserver(Graph* graph = nullptr) {
    // If a `graph` is passed, automatically start observing it.
    if (graph) {
      scoped_observation_.Observe(graph);
    }
  }

  void SetNotifiedSystemNode(const SystemNode* system_node) {
    notified_system_node_ = system_node;
  }

  const SystemNode* TakeNotifiedSystemNode() {
    const SystemNode* node = notified_system_node_;
    notified_system_node_ = nullptr;
    return node;
  }

 private:
  base::ScopedObservation<Graph, SystemNodeObserver> scoped_observation_{this};
  raw_ptr<const SystemNode> notified_system_node_ = nullptr;
};

}  // namespace

TEST_F(SystemNodeImplTest, ObserverWorks) {
  MockObserver head_obs;
  MockObserver obs;
  MockObserver tail_obs;
  graph()->AddSystemNodeObserver(&head_obs);
  graph()->AddSystemNodeObserver(&obs);
  graph()->AddSystemNodeObserver(&tail_obs);

  const SystemNode* system_node = graph()->GetSystemNode();

  // Remove observers at the head and tail of the list inside a callback, and
  // expect that `obs` is still notified correctly.
  EXPECT_CALL(head_obs, OnProcessMemoryMetricsAvailable(_))
      .WillOnce(InvokeWithoutArgs([&] {
        graph()->RemoveSystemNodeObserver(&head_obs);
        graph()->RemoveSystemNodeObserver(&tail_obs);
      }));
  // `tail_obs` should not be notified as it was removed.
  EXPECT_CALL(tail_obs, OnProcessMemoryMetricsAvailable(_)).Times(0);

  EXPECT_CALL(obs, OnProcessMemoryMetricsAvailable(_))
      .WillOnce(Invoke(&obs, &MockObserver::SetNotifiedSystemNode));
  SystemNodeImpl::FromNode(system_node)->OnProcessMemoryMetricsAvailable();
  EXPECT_EQ(system_node, obs.TakeNotifiedSystemNode());

  EXPECT_CALL(obs, OnBeforeMemoryPressure(
                       MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL));
  EXPECT_CALL(obs, OnMemoryPressure(
                       MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL));
  SystemNodeImpl::FromNode(system_node)
      ->OnMemoryPressureForTesting(
          MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL);

  // Re-entrant iteration should work.
  EXPECT_CALL(obs, OnProcessMemoryMetricsAvailable(system_node))
      .WillOnce(InvokeWithoutArgs([&] {
        SystemNodeImpl::FromNode(system_node)
            ->OnMemoryPressureForTesting(
                MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
      }));
  EXPECT_CALL(obs, OnBeforeMemoryPressure(
                       MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE));
  EXPECT_CALL(obs, OnMemoryPressure(
                       MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE));
  SystemNodeImpl::FromNode(system_node)->OnProcessMemoryMetricsAvailable();

  graph()->RemoveSystemNodeObserver(&obs);
}

TEST_F(SystemNodeImplTest, MemoryPressureNotification) {
  MockObserver obs(graph());
  memory_pressure::test::FakeMemoryPressureMonitor mem_pressure_monitor;

  {
    base::RunLoop run_loop;
    auto quit_closure = run_loop.QuitClosure();
    EXPECT_CALL(obs, OnBeforeMemoryPressure(
                         base::MemoryPressureListener::MemoryPressureLevel::
                             MEMORY_PRESSURE_LEVEL_CRITICAL))
        .WillOnce(InvokeWithoutArgs([&]() { std::move(quit_closure).Run(); }));
    EXPECT_CALL(obs, OnMemoryPressure(
                         base::MemoryPressureListener::MemoryPressureLevel::
                             MEMORY_PRESSURE_LEVEL_CRITICAL));
    mem_pressure_monitor.SetAndNotifyMemoryPressure(
        base::MemoryPressureListener::MemoryPressureLevel::
            MEMORY_PRESSURE_LEVEL_CRITICAL);
    run_loop.Run();
  }

  {
    base::RunLoop run_loop;
    auto quit_closure = run_loop.QuitClosure();
    EXPECT_CALL(obs, OnBeforeMemoryPressure(
                         base::MemoryPressureListener::MemoryPressureLevel::
                             MEMORY_PRESSURE_LEVEL_MODERATE))
        .WillOnce(InvokeWithoutArgs([&]() { std::move(quit_closure).Run(); }));
    EXPECT_CALL(obs, OnMemoryPressure(
                         base::MemoryPressureListener::MemoryPressureLevel::
                             MEMORY_PRESSURE_LEVEL_MODERATE));
    mem_pressure_monitor.SetAndNotifyMemoryPressure(
        base::MemoryPressureListener::MemoryPressureLevel::
            MEMORY_PRESSURE_LEVEL_MODERATE);
    run_loop.Run();
  }
}

}  // namespace performance_manager