File: pressure_test_support.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (100 lines) | stat: -rw-r--r-- 3,090 bytes parent folder | download | duplicates (3)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SYSTEM_CPU_PRESSURE_TEST_SUPPORT_H_
#define COMPONENTS_SYSTEM_CPU_PRESSURE_TEST_SUPPORT_H_

#include <stdint.h>

#include <optional>
#include <type_traits>
#include <utility>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "base/test/test_future.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "components/system_cpu/cpu_probe.h"
#include "components/system_cpu/cpu_sample.h"

namespace system_cpu {

// Test double for platform specific CpuProbe that stores the CpuSample in
// a TestFuture.
template <typename T>
  requires(std::is_base_of_v<CpuProbe, T>)
class FakePlatformCpuProbe : public T {
 public:
  template <typename... Args>
  explicit FakePlatformCpuProbe(Args&&... args)
      : T(std::forward<Args>(args)...) {}
  ~FakePlatformCpuProbe() override = default;

  // Tests the internals of each platform CPU probe by calling Update() directly
  // instead of using the public interface.
  std::optional<CpuSample> UpdateAndWaitForSample() {
    T::Update(sample_.GetCallback());
    // Blocks until the sample callback is invoked.
    return sample_.Take();
  }

 private:
  base::test::TestFuture<std::optional<CpuSample>> sample_;
};

// Test double for CpuProbe that always returns a predetermined value.
class FakeCpuProbe final : public CpuProbe {
 public:
  // Creates a FakeCpuProbe that delays Update() responses by `response_delay`.
  // Setting this to >0 can mimic production CpuProbes that take samples on
  // background threads.
  explicit FakeCpuProbe(base::TimeDelta response_delay = base::TimeDelta());

  ~FakeCpuProbe() final;

  // CpuProbe implementation.
  void Update(SampleCallback callback) final;
  base::WeakPtr<CpuProbe> GetWeakPtr() final;

  // Can be called from any thread.
  void SetLastSample(std::optional<CpuSample> sample);

 private:
  const base::TimeDelta response_delay_;

  base::Lock lock_;
  std::optional<CpuSample> last_sample_ GUARDED_BY_CONTEXT(lock_);

  base::WeakPtrFactory<FakeCpuProbe> weak_factory_{this};
};

// Test double for CpuProbe that produces a different value after every
// Update().
class StreamingCpuProbe final : public CpuProbe {
 public:
  StreamingCpuProbe(std::vector<CpuSample>, base::OnceClosure);

  ~StreamingCpuProbe() final;

  // CpuProbe implementation.
  void Update(SampleCallback callback) final;
  base::WeakPtr<CpuProbe> GetWeakPtr() final;

 private:
  std::vector<CpuSample> samples_ GUARDED_BY_CONTEXT(sequence_checker_);
  size_t sample_index_ GUARDED_BY_CONTEXT(sequence_checker_) = 0;

  // This closure is called on an Update() call after the expected number of
  // samples has been taken by CpuSampler.
  base::OnceClosure done_callback_;

  base::WeakPtrFactory<StreamingCpuProbe> weak_factory_{this};
};

}  // namespace system_cpu

#endif  // COMPONENTS_SYSTEM_CPU_PRESSURE_TEST_SUPPORT_H_