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 service_endpoint_->CloneSession(tsid, {});
83 }
84
WaitForTracingDisabledWithError(const testing::Matcher<const std::string &> & error_matcher,uint32_t timeout_ms)85 void MockConsumer::WaitForTracingDisabledWithError(
86 const testing::Matcher<const std::string&>& error_matcher,
87 uint32_t timeout_ms) {
88 static int i = 0;
89 auto checkpoint_name = "on_tracing_disabled_consumer_" + std::to_string(i++);
90 auto on_tracing_disabled = task_runner_->CreateCheckpoint(checkpoint_name);
91 EXPECT_CALL(*this, OnTracingDisabled(error_matcher))
92 .WillOnce(testing::InvokeWithoutArgs(on_tracing_disabled));
93 task_runner_->RunUntilCheckpoint(checkpoint_name, timeout_ms);
94 }
95
Flush(uint32_t timeout_ms,FlushFlags flush_flags)96 MockConsumer::FlushRequest MockConsumer::Flush(uint32_t timeout_ms,
97 FlushFlags flush_flags) {
98 static int i = 0;
99 auto checkpoint_name = "on_consumer_flush_" + std::to_string(i++);
100 auto on_flush = task_runner_->CreateCheckpoint(checkpoint_name);
101 std::shared_ptr<bool> result(new bool());
102 service_endpoint_->Flush(
103 timeout_ms,
104 [result, on_flush](bool success) {
105 *result = success;
106 on_flush();
107 },
108 flush_flags);
109
110 base::TestTaskRunner* task_runner = task_runner_;
111 auto wait_for_flush_completion = [result, task_runner,
112 checkpoint_name]() -> bool {
113 task_runner->RunUntilCheckpoint(checkpoint_name);
114 return *result;
115 };
116
117 return FlushRequest(wait_for_flush_completion);
118 }
119
ReadBuffers()120 std::vector<protos::gen::TracePacket> MockConsumer::ReadBuffers() {
121 std::vector<protos::gen::TracePacket> decoded_packets;
122 static int i = 0;
123 std::string checkpoint_name = "on_read_buffers_" + std::to_string(i++);
124 auto on_read_buffers = task_runner_->CreateCheckpoint(checkpoint_name);
125 EXPECT_CALL(*this, OnTraceData(_, _))
126 .WillRepeatedly(Invoke([&decoded_packets, on_read_buffers](
127 std::vector<TracePacket>* packets,
128 bool has_more) {
129 for (TracePacket& packet : *packets) {
130 decoded_packets.emplace_back();
131 protos::gen::TracePacket* decoded_packet = &decoded_packets.back();
132 decoded_packet->ParseFromString(packet.GetRawBytesForTesting());
133 }
134 if (!has_more)
135 on_read_buffers();
136 }));
137 service_endpoint_->ReadBuffers();
138 task_runner_->RunUntilCheckpoint(checkpoint_name);
139 return decoded_packets;
140 }
141
GetTraceStats()142 void MockConsumer::GetTraceStats() {
143 service_endpoint_->GetTraceStats();
144 }
145
WaitForTraceStats(bool success)146 TraceStats MockConsumer::WaitForTraceStats(bool success) {
147 static int i = 0;
148 auto checkpoint_name = "on_trace_stats_" + std::to_string(i++);
149 auto on_trace_stats = task_runner_->CreateCheckpoint(checkpoint_name);
150 TraceStats stats;
151 auto result_callback = [on_trace_stats, &stats](bool, const TraceStats& s) {
152 stats = s;
153 on_trace_stats();
154 };
155 if (success) {
156 EXPECT_CALL(*this,
157 OnTraceStats(true, testing::Property(&TraceStats::total_buffers,
158 testing::Gt(0u))))
159 .WillOnce(Invoke(result_callback));
160 } else {
161 EXPECT_CALL(*this, OnTraceStats(false, _))
162 .WillOnce(Invoke(result_callback));
163 }
164 task_runner_->RunUntilCheckpoint(checkpoint_name);
165 return stats;
166 }
167
ObserveEvents(uint32_t enabled_event_types)168 void MockConsumer::ObserveEvents(uint32_t enabled_event_types) {
169 service_endpoint_->ObserveEvents(enabled_event_types);
170 }
171
WaitForObservableEvents()172 ObservableEvents MockConsumer::WaitForObservableEvents() {
173 ObservableEvents events;
174 static int i = 0;
175 std::string checkpoint_name = "on_observable_events_" + std::to_string(i++);
176 auto on_observable_events = task_runner_->CreateCheckpoint(checkpoint_name);
177 EXPECT_CALL(*this, OnObservableEvents(_))
178 .WillOnce(Invoke([&events, on_observable_events](
179 const ObservableEvents& observable_events) {
180 events = observable_events;
181 on_observable_events();
182 }));
183 task_runner_->RunUntilCheckpoint(checkpoint_name);
184 return events;
185 }
186
QueryServiceState()187 TracingServiceState MockConsumer::QueryServiceState() {
188 static int i = 0;
189 TracingServiceState res;
190 std::string checkpoint_name = "query_service_state_" + std::to_string(i++);
191 auto checkpoint = task_runner_->CreateCheckpoint(checkpoint_name);
192 auto callback = [checkpoint, &res](bool success,
193 const TracingServiceState& svc_state) {
194 EXPECT_TRUE(success);
195 res = svc_state;
196 checkpoint();
197 };
198 service_endpoint_->QueryServiceState({}, callback);
199 task_runner_->RunUntilCheckpoint(checkpoint_name);
200 return res;
201 }
202
203 } // namespace perfetto
204