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(TracingService * svc,uid_t uid)42 void MockConsumer::Connect(TracingService* svc, uid_t uid) {
43 service_endpoint_ = svc->ConnectConsumer(this, uid);
44 static int i = 0;
45 auto checkpoint_name = "on_consumer_connect_" + std::to_string(i++);
46 auto on_connect = task_runner_->CreateCheckpoint(checkpoint_name);
47 EXPECT_CALL(*this, OnConnect()).WillOnce(Invoke(on_connect));
48 task_runner_->RunUntilCheckpoint(checkpoint_name);
49 }
50
EnableTracing(const TraceConfig & trace_config,base::ScopedFile write_into_file)51 void MockConsumer::EnableTracing(const TraceConfig& trace_config,
52 base::ScopedFile write_into_file) {
53 service_endpoint_->EnableTracing(trace_config, std::move(write_into_file));
54 }
55
StartTracing()56 void MockConsumer::StartTracing() {
57 service_endpoint_->StartTracing();
58 }
59
ChangeTraceConfig(const TraceConfig & trace_config)60 void MockConsumer::ChangeTraceConfig(const TraceConfig& trace_config) {
61 service_endpoint_->ChangeTraceConfig(trace_config);
62 }
63
DisableTracing()64 void MockConsumer::DisableTracing() {
65 service_endpoint_->DisableTracing();
66 }
67
FreeBuffers()68 void MockConsumer::FreeBuffers() {
69 service_endpoint_->FreeBuffers();
70 }
71
WaitForTracingDisabled(uint32_t timeout_ms)72 void MockConsumer::WaitForTracingDisabled(uint32_t timeout_ms) {
73 static int i = 0;
74 auto checkpoint_name = "on_tracing_disabled_consumer_" + std::to_string(i++);
75 auto on_tracing_disabled = task_runner_->CreateCheckpoint(checkpoint_name);
76 EXPECT_CALL(*this, OnTracingDisabled(_))
77 .WillOnce(testing::InvokeWithoutArgs(on_tracing_disabled));
78 task_runner_->RunUntilCheckpoint(checkpoint_name, timeout_ms);
79 }
80
Flush(uint32_t timeout_ms)81 MockConsumer::FlushRequest MockConsumer::Flush(uint32_t timeout_ms) {
82 static int i = 0;
83 auto checkpoint_name = "on_consumer_flush_" + std::to_string(i++);
84 auto on_flush = task_runner_->CreateCheckpoint(checkpoint_name);
85 std::shared_ptr<bool> result(new bool());
86 service_endpoint_->Flush(timeout_ms, [result, on_flush](bool success) {
87 *result = success;
88 on_flush();
89 });
90
91 base::TestTaskRunner* task_runner = task_runner_;
92 auto wait_for_flush_completion = [result, task_runner,
93 checkpoint_name]() -> bool {
94 task_runner->RunUntilCheckpoint(checkpoint_name);
95 return *result;
96 };
97
98 return FlushRequest(wait_for_flush_completion);
99 }
100
ReadBuffers()101 std::vector<protos::gen::TracePacket> MockConsumer::ReadBuffers() {
102 std::vector<protos::gen::TracePacket> decoded_packets;
103 static int i = 0;
104 std::string checkpoint_name = "on_read_buffers_" + std::to_string(i++);
105 auto on_read_buffers = task_runner_->CreateCheckpoint(checkpoint_name);
106 EXPECT_CALL(*this, OnTraceData(_, _))
107 .WillRepeatedly(
108 Invoke([&decoded_packets, on_read_buffers](
109 std::vector<TracePacket>* packets, bool has_more) {
110 for (TracePacket& packet : *packets) {
111 decoded_packets.emplace_back();
112 protos::gen::TracePacket* decoded_packet =
113 &decoded_packets.back();
114 decoded_packet->ParseFromString(packet.GetRawBytesForTesting());
115 }
116 if (!has_more)
117 on_read_buffers();
118 }));
119 service_endpoint_->ReadBuffers();
120 task_runner_->RunUntilCheckpoint(checkpoint_name);
121 return decoded_packets;
122 }
123
GetTraceStats()124 void MockConsumer::GetTraceStats() {
125 service_endpoint_->GetTraceStats();
126 }
127
WaitForTraceStats(bool success)128 void MockConsumer::WaitForTraceStats(bool success) {
129 static int i = 0;
130 auto checkpoint_name = "on_trace_stats_" + std::to_string(i++);
131 auto on_trace_stats = task_runner_->CreateCheckpoint(checkpoint_name);
132 auto result_callback = [on_trace_stats](bool, const TraceStats&) {
133 on_trace_stats();
134 };
135 if (success) {
136 EXPECT_CALL(*this,
137 OnTraceStats(true, testing::Property(&TraceStats::total_buffers,
138 testing::Gt(0u))))
139 .WillOnce(Invoke(result_callback));
140 } else {
141 EXPECT_CALL(*this, OnTraceStats(false, _))
142 .WillOnce(Invoke(result_callback));
143 }
144 task_runner_->RunUntilCheckpoint(checkpoint_name);
145 }
146
ObserveEvents(uint32_t enabled_event_types)147 void MockConsumer::ObserveEvents(uint32_t enabled_event_types) {
148 service_endpoint_->ObserveEvents(enabled_event_types);
149 }
150
WaitForObservableEvents()151 ObservableEvents MockConsumer::WaitForObservableEvents() {
152 ObservableEvents events;
153 static int i = 0;
154 std::string checkpoint_name = "on_observable_events_" + std::to_string(i++);
155 auto on_observable_events = task_runner_->CreateCheckpoint(checkpoint_name);
156 EXPECT_CALL(*this, OnObservableEvents(_))
157 .WillOnce(Invoke([&events, on_observable_events](
158 const ObservableEvents& observable_events) {
159 events = observable_events;
160 on_observable_events();
161 }));
162 task_runner_->RunUntilCheckpoint(checkpoint_name);
163 return events;
164 }
165
QueryServiceState()166 TracingServiceState MockConsumer::QueryServiceState() {
167 static int i = 0;
168 TracingServiceState res;
169 std::string checkpoint_name = "query_service_state_" + std::to_string(i++);
170 auto checkpoint = task_runner_->CreateCheckpoint(checkpoint_name);
171 auto callback = [checkpoint, &res](bool success,
172 const TracingServiceState& svc_state) {
173 EXPECT_TRUE(success);
174 res = svc_state;
175 checkpoint();
176 };
177 service_endpoint_->QueryServiceState(callback);
178 task_runner_->RunUntilCheckpoint(checkpoint_name);
179 return res;
180 }
181
182 } // namespace perfetto
183