xref: /aosp_15_r20/external/cronet/base/debug/dump_without_crashing_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 The Chromium Authors
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/debug/dump_without_crashing.h"
6 
7 #include "base/hash/hash.h"
8 #include "base/location.h"
9 #include "base/test/metrics/histogram_tester.h"
10 #include "base/test/scoped_mock_clock_override.h"
11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 
14 namespace base {
15 namespace debug {
16 
17 class DumpWithoutCrashingTest : public testing::Test {
18  public:
DummyDumpWithoutCrashing()19   static void DummyDumpWithoutCrashing() { number_of_dump_calls_++; }
20 
number_of_dump_calls()21   static int number_of_dump_calls() { return number_of_dump_calls_; }
22 
23  protected:
SetUp()24   void SetUp() override {
25     SetDumpWithoutCrashingFunction(
26         &DumpWithoutCrashingTest::DummyDumpWithoutCrashing);
27     number_of_dump_calls_ = 0;
28   }
29 
TearDown()30   void TearDown() override {
31     SetDumpWithoutCrashingFunction(nullptr);
32     ClearMapsForTesting();
33   }
34 
35   // Set override.
36   ScopedMockClockOverride clock_;
37 
38   const base::HistogramTester histogram_tester_;
39   const base::Location location1_ = FROM_HERE;
40   const base::Location location2_ = FROM_HERE;
41   const size_t unique_identifier1_ =
42       base::FastHash("DumpWithoutCrashingFirstTest");
43   const size_t unique_identifier2_ =
44       base::FastHash("DumpWithoutCrashingSecondTest");
45 
46  private:
47   static int number_of_dump_calls_;
48 };
49 
50 int DumpWithoutCrashingTest::number_of_dump_calls_ = 0;
51 
TEST_F(DumpWithoutCrashingTest,DumpWithoutCrashingWithLocation)52 TEST_F(DumpWithoutCrashingTest, DumpWithoutCrashingWithLocation) {
53   EXPECT_EQ(0, DumpWithoutCrashingTest::number_of_dump_calls());
54 
55   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
56                                       DumpWithoutCrashingStatus::kThrottled, 0);
57   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
58                                       DumpWithoutCrashingStatus::kUploaded, 0);
59 
60   // The first call to DumpWithoutCrashing will always capture the dump and
61   // will return true
62   EXPECT_TRUE(DumpWithoutCrashing(location1_, base::Seconds(1)));
63   EXPECT_EQ(1, DumpWithoutCrashingTest::number_of_dump_calls());
64 
65   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
66                                       DumpWithoutCrashingStatus::kThrottled, 0);
67   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
68                                       DumpWithoutCrashingStatus::kUploaded, 1);
69 
70   // If DumpWithoutCrashing is called within 1 second, expected result is false.
71   EXPECT_FALSE(DumpWithoutCrashing(location1_, base::Seconds(1)));
72   EXPECT_EQ(1, DumpWithoutCrashingTest::number_of_dump_calls());
73 
74   // For testing, the time for capturing dump again is 1 second and if the
75   // function is called after 1 second, it will return true.
76   clock_.Advance(Seconds(2));
77   EXPECT_TRUE(DumpWithoutCrashing(location1_, base::Seconds(1)));
78   EXPECT_EQ(2, DumpWithoutCrashingTest::number_of_dump_calls());
79 
80   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
81                                       DumpWithoutCrashingStatus::kThrottled, 1);
82   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
83                                       DumpWithoutCrashingStatus::kUploaded, 2);
84 
85   EXPECT_TRUE(DumpWithoutCrashing(location2_, base::Seconds(1)));
86   EXPECT_EQ(3, DumpWithoutCrashingTest::number_of_dump_calls());
87 
88   EXPECT_FALSE(DumpWithoutCrashing(location2_, base::Seconds(1)));
89   EXPECT_EQ(3, DumpWithoutCrashingTest::number_of_dump_calls());
90 
91   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
92                                       DumpWithoutCrashingStatus::kThrottled, 2);
93   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
94                                       DumpWithoutCrashingStatus::kUploaded, 3);
95 }
96 
TEST_F(DumpWithoutCrashingTest,DumpWithoutCrashingWithLocationAndUniqueId)97 TEST_F(DumpWithoutCrashingTest, DumpWithoutCrashingWithLocationAndUniqueId) {
98   EXPECT_EQ(0, DumpWithoutCrashingTest::number_of_dump_calls());
99 
100   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
101                                       DumpWithoutCrashingStatus::kThrottled, 0);
102   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
103                                       DumpWithoutCrashingStatus::kUploaded, 0);
104 
105   // Test the variant of DumpWithoutCrashingWithUniqueId where the function
106   // takes a location and unique id.
107   EXPECT_TRUE(DumpWithoutCrashingWithUniqueId(unique_identifier1_, location1_,
108                                               base::Seconds(1)));
109   EXPECT_EQ(1, DumpWithoutCrashingTest::number_of_dump_calls());
110 
111   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
112                                       DumpWithoutCrashingStatus::kThrottled, 0);
113   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
114                                       DumpWithoutCrashingStatus::kUploaded, 1);
115 
116   // If DumpWithoutCrashingWithUniqueId called within 1 second, expected result
117   // is false.
118   EXPECT_FALSE(DumpWithoutCrashingWithUniqueId(unique_identifier1_, location1_,
119                                                base::Seconds(1)));
120   EXPECT_EQ(1, DumpWithoutCrashingTest::number_of_dump_calls());
121 
122   // For testing, the time for capturing dump again is 1 second and if the
123   // function is called after 1 second, it will return true.
124   clock_.Advance(Seconds(2));
125 
126   EXPECT_TRUE(DumpWithoutCrashingWithUniqueId(unique_identifier1_, location1_,
127                                               base::Seconds(1)));
128   EXPECT_EQ(2, DumpWithoutCrashingTest::number_of_dump_calls());
129 
130   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
131                                       DumpWithoutCrashingStatus::kThrottled, 1);
132   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
133                                       DumpWithoutCrashingStatus::kUploaded, 2);
134 
135   EXPECT_TRUE(DumpWithoutCrashingWithUniqueId(unique_identifier2_, location2_,
136                                               base::Seconds(1)));
137   EXPECT_EQ(3, DumpWithoutCrashingTest::number_of_dump_calls());
138   EXPECT_FALSE(DumpWithoutCrashingWithUniqueId(unique_identifier2_, location2_,
139                                                base::Seconds(1)));
140   EXPECT_EQ(3, DumpWithoutCrashingTest::number_of_dump_calls());
141   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
142                                       DumpWithoutCrashingStatus::kThrottled, 2);
143   histogram_tester_.ExpectBucketCount("Stability.DumpWithoutCrashingStatus",
144                                       DumpWithoutCrashingStatus::kUploaded, 3);
145 }
146 
147 }  // namespace debug
148 }  // namespace base
149