tracing_service_impl_unittest: Avoid accessing trigger_window_ns_
We can use the mock clock instead.
Change-Id: I74a498bfe865179fb7c6706572d988f18f31ede3
diff --git a/src/tracing/service/tracing_service_impl.cc b/src/tracing/service/tracing_service_impl.cc
index 487a34d..ed9a2f0 100644
--- a/src/tracing/service/tracing_service_impl.cc
+++ b/src/tracing/service/tracing_service_impl.cc
@@ -3919,12 +3919,13 @@
size_t TracingServiceImpl::PurgeExpiredAndCountTriggerInWindow(
int64_t now_ns,
uint64_t trigger_name_hash) {
+ constexpr int64_t kOneDayInNs = 24ll * 60 * 60 * 1000 * 1000 * 1000;
PERFETTO_DCHECK(
std::is_sorted(trigger_history_.begin(), trigger_history_.end()));
size_t remove_count = 0;
size_t trigger_count = 0;
for (const TriggerHistory& h : trigger_history_) {
- if (h.timestamp_ns < now_ns - trigger_window_ns_) {
+ if (h.timestamp_ns < now_ns - kOneDayInNs) {
remove_count++;
} else if (h.name_hash == trigger_name_hash) {
trigger_count++;
diff --git a/src/tracing/service/tracing_service_impl.h b/src/tracing/service/tracing_service_impl.h
index 346f6de..dae4df4 100644
--- a/src/tracing/service/tracing_service_impl.h
+++ b/src/tracing/service/tracing_service_impl.h
@@ -422,8 +422,6 @@
friend class TracingServiceImplTest;
friend class TracingIntegrationTest;
- static constexpr int64_t kOneDayInNs = 24ll * 60 * 60 * 1000 * 1000 * 1000;
-
struct TriggerHistory {
int64_t timestamp_ns;
uint64_t name_hash;
@@ -906,13 +904,12 @@
std::map<std::string, int64_t> session_to_last_trace_s_;
// Contains timestamps of triggers.
- // The queue is sorted by timestamp and invocations older than
- // |trigger_window_ns_| are purged when a trigger happens.
+ // The queue is sorted by timestamp and invocations older than 24 hours are
+ // purged when a trigger happens.
base::CircularQueue<TriggerHistory> trigger_history_;
bool smb_scraping_enabled_ = false;
bool lockdown_mode_ = false;
- int64_t trigger_window_ns_ = kOneDayInNs; // Overridable for testing.
std::minstd_rand trigger_probability_rand_;
std::uniform_real_distribution<> trigger_probability_dist_;
diff --git a/src/tracing/service/tracing_service_impl_unittest.cc b/src/tracing/service/tracing_service_impl_unittest.cc
index 1f5fa50..e4cf3e1 100644
--- a/src/tracing/service/tracing_service_impl_unittest.cc
+++ b/src/tracing/service/tracing_service_impl_unittest.cc
@@ -261,10 +261,6 @@
return ret;
}
- void SetTriggerWindowNs(int64_t window_ns) {
- svc->trigger_window_ns_ = window_ns;
- }
-
void AdvanceTimeAndRunUntilIdle(uint32_t ms) {
mock_clock_displacement_ += base::TimeMillis(ms);
task_runner.AdvanceTimeAndRunUntilIdle(ms);
@@ -1210,6 +1206,8 @@
EXPECT_THAT(GetReceivedTriggers(packets), ElementsAre("trigger_name"));
}
+ AdvanceTimeAndRunUntilIdle(23 * 60 * 60 * 1000); // 23h
+
// Second session.
{
std::unique_ptr<MockProducer> producer = CreateMockProducer();
@@ -1256,10 +1254,6 @@
auto* ds = trace_config.add_data_sources()->mutable_config();
- // Set the trigger window size to something really small so the second
- // session is still allowed through.
- SetTriggerWindowNs(1);
-
// First session.
{
std::unique_ptr<MockProducer> producer = CreateMockProducer();
@@ -1292,8 +1286,7 @@
EXPECT_THAT(GetReceivedTriggers(packets), ElementsAre("trigger_name"));
}
- // Sleep 1 micro so that we're sure that the window time would have elapsed.
- base::SleepMicroseconds(1);
+ AdvanceTimeAndRunUntilIdle(24 * 60 * 60 * 1000); // 24h
// Second session.
{