xref: /aosp_15_r20/external/perfetto/src/tracing/test/mock_consumer.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/tracing/test/mock_consumer.h"
18 
19 #include "perfetto/ext/tracing/core/trace_stats.h"
20 #include "perfetto/tracing/core/trace_config.h"
21 #include "src/base/test/test_task_runner.h"
22 
23 using ::testing::_;
24 using ::testing::Invoke;
25 
26 namespace perfetto {
27 
MockConsumer(base::TestTaskRunner * task_runner)28 MockConsumer::MockConsumer(base::TestTaskRunner* task_runner)
29     : task_runner_(task_runner) {}
30 
~MockConsumer()31 MockConsumer::~MockConsumer() {
32   if (!service_endpoint_)
33     return;
34   static int i = 0;
35   auto checkpoint_name = "on_consumer_disconnect_" + std::to_string(i++);
36   auto on_disconnect = task_runner_->CreateCheckpoint(checkpoint_name);
37   EXPECT_CALL(*this, OnDisconnect()).WillOnce(Invoke(on_disconnect));
38   service_endpoint_.reset();
39   task_runner_->RunUntilCheckpoint(checkpoint_name);
40 }
41 
Connect(std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint)42 void MockConsumer::Connect(
43     std::unique_ptr<TracingService::ConsumerEndpoint> service_endpoint) {
44   service_endpoint_ = std::move(service_endpoint);
45   static int i = 0;
46   auto checkpoint_name = "on_consumer_connect_" + std::to_string(i++);
47   auto on_connect = task_runner_->CreateCheckpoint(checkpoint_name);
48   EXPECT_CALL(*this, OnConnect()).WillOnce(Invoke(on_connect));
49   task_runner_->RunUntilCheckpoint(checkpoint_name);
50 }
51 
Connect(TracingService * svc,uid_t uid)52 void MockConsumer::Connect(TracingService* svc, uid_t uid) {
53   Connect(svc->ConnectConsumer(this, uid));
54 }
55 
ForceDisconnect()56 void MockConsumer::ForceDisconnect() {
57   service_endpoint_.reset();
58 }
59 
EnableTracing(const TraceConfig & trace_config,base::ScopedFile write_into_file)60 void MockConsumer::EnableTracing(const TraceConfig& trace_config,
61                                  base::ScopedFile write_into_file) {
62   service_endpoint_->EnableTracing(trace_config, std::move(write_into_file));
63 }
64 
StartTracing()65 void MockConsumer::StartTracing() {
66   service_endpoint_->StartTracing();
67 }
68 
ChangeTraceConfig(const TraceConfig & trace_config)69 void MockConsumer::ChangeTraceConfig(const TraceConfig& trace_config) {
70   service_endpoint_->ChangeTraceConfig(trace_config);
71 }
72 
DisableTracing()73 void MockConsumer::DisableTracing() {
74   service_endpoint_->DisableTracing();
75 }
76 
FreeBuffers()77 void MockConsumer::FreeBuffers() {
78   service_endpoint_->FreeBuffers();
79 }
80 
CloneSession(TracingSessionID tsid)81 void MockConsumer::CloneSession(TracingSessionID tsid) {
82   ConsumerEndpoint::CloneSessionArgs args;
83   args.tsid = tsid;
84   service_endpoint_->CloneSession(args);
85 }
86 
WaitForTracingDisabledWithError(const testing::Matcher<const std::string &> & error_matcher,uint32_t timeout_ms)87 void MockConsumer::WaitForTracingDisabledWithError(
88     const testing::Matcher<const std::string&>& error_matcher,
89     uint32_t timeout_ms) {
90   static int i = 0;
91   auto checkpoint_name = "on_tracing_disabled_consumer_" + std::to_string(i++);
92   auto on_tracing_disabled = task_runner_->CreateCheckpoint(checkpoint_name);
93   EXPECT_CALL(*this, OnTracingDisabled(error_matcher))
94       .WillOnce(testing::InvokeWithoutArgs(on_tracing_disabled));
95   task_runner_->RunUntilCheckpoint(checkpoint_name, timeout_ms);
96 }
97 
Flush(uint32_t timeout_ms,FlushFlags flush_flags)98 MockConsumer::FlushRequest MockConsumer::Flush(uint32_t timeout_ms,
99                                                FlushFlags flush_flags) {
100   static int i = 0;
101   auto checkpoint_name = "on_consumer_flush_" + std::to_string(i++);
102   auto on_flush = task_runner_->CreateCheckpoint(checkpoint_name);
103   std::shared_ptr<bool> result(new bool());
104   service_endpoint_->Flush(
105       timeout_ms,
106       [result, on_flush](bool success) {
107         *result = success;
108         on_flush();
109       },
110       flush_flags);
111 
112   base::TestTaskRunner* task_runner = task_runner_;
113   auto wait_for_flush_completion = [result, task_runner,
114                                     checkpoint_name]() -> bool {
115     task_runner->RunUntilCheckpoint(checkpoint_name);
116     return *result;
117   };
118 
119   return FlushRequest(wait_for_flush_completion);
120 }
121 
ReadBuffers()122 std::vector<protos::gen::TracePacket> MockConsumer::ReadBuffers() {
123   std::vector<protos::gen::TracePacket> decoded_packets;
124   static int i = 0;
125   std::string checkpoint_name = "on_read_buffers_" + std::to_string(i++);
126   auto on_read_buffers = task_runner_->CreateCheckpoint(checkpoint_name);
127   EXPECT_CALL(*this, OnTraceData(_, _))
128       .WillRepeatedly(Invoke([&decoded_packets, on_read_buffers](
129                                  std::vector<TracePacket>* packets,
130                                  bool has_more) {
131         for (TracePacket& packet : *packets) {
132           decoded_packets.emplace_back();
133           protos::gen::TracePacket* decoded_packet = &decoded_packets.back();
134           decoded_packet->ParseFromString(packet.GetRawBytesForTesting());
135         }
136         if (!has_more)
137           on_read_buffers();
138       }));
139   service_endpoint_->ReadBuffers();
140   task_runner_->RunUntilCheckpoint(checkpoint_name);
141   return decoded_packets;
142 }
143 
GetTraceStats()144 void MockConsumer::GetTraceStats() {
145   service_endpoint_->GetTraceStats();
146 }
147 
WaitForTraceStats(bool success)148 TraceStats MockConsumer::WaitForTraceStats(bool success) {
149   static int i = 0;
150   auto checkpoint_name = "on_trace_stats_" + std::to_string(i++);
151   auto on_trace_stats = task_runner_->CreateCheckpoint(checkpoint_name);
152   TraceStats stats;
153   auto result_callback = [on_trace_stats, &stats](bool, const TraceStats& s) {
154     stats = s;
155     on_trace_stats();
156   };
157   if (success) {
158     EXPECT_CALL(*this,
159                 OnTraceStats(true, testing::Property(&TraceStats::total_buffers,
160                                                      testing::Gt(0u))))
161         .WillOnce(Invoke(result_callback));
162   } else {
163     EXPECT_CALL(*this, OnTraceStats(false, _))
164         .WillOnce(Invoke(result_callback));
165   }
166   task_runner_->RunUntilCheckpoint(checkpoint_name);
167   return stats;
168 }
169 
ObserveEvents(uint32_t enabled_event_types)170 void MockConsumer::ObserveEvents(uint32_t enabled_event_types) {
171   service_endpoint_->ObserveEvents(enabled_event_types);
172 }
173 
WaitForObservableEvents()174 ObservableEvents MockConsumer::WaitForObservableEvents() {
175   ObservableEvents events;
176   static int i = 0;
177   std::string checkpoint_name = "on_observable_events_" + std::to_string(i++);
178   auto on_observable_events = task_runner_->CreateCheckpoint(checkpoint_name);
179   EXPECT_CALL(*this, OnObservableEvents(_))
180       .WillOnce(Invoke([&events, on_observable_events](
181                            const ObservableEvents& observable_events) {
182         events = observable_events;
183         on_observable_events();
184       }));
185   task_runner_->RunUntilCheckpoint(checkpoint_name);
186   return events;
187 }
188 
QueryServiceState()189 TracingServiceState MockConsumer::QueryServiceState() {
190   static int i = 0;
191   TracingServiceState res;
192   std::string checkpoint_name = "query_service_state_" + std::to_string(i++);
193   auto checkpoint = task_runner_->CreateCheckpoint(checkpoint_name);
194   auto callback = [checkpoint, &res](bool success,
195                                      const TracingServiceState& svc_state) {
196     EXPECT_TRUE(success);
197     res = svc_state;
198     checkpoint();
199   };
200   service_endpoint_->QueryServiceState({}, callback);
201   task_runner_->RunUntilCheckpoint(checkpoint_name);
202   return res;
203 }
204 
Detach(std::string key)205 void MockConsumer::Detach(std::string key) {
206   service_endpoint_->Detach(key);
207 }
208 
Attach(std::string key)209 void MockConsumer::Attach(std::string key) {
210   service_endpoint_->Attach(key);
211 }
212 
213 }  // namespace perfetto
214