blob: 8dd1d518d0201e34c54f5dcb864bc82ec4fa3518 [file] [log] [blame]
Lalit Maganti79f2d7b2018-01-23 18:27:33 +00001/*
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#ifndef TEST_FAKE_PRODUCER_H_
18#define TEST_FAKE_PRODUCER_H_
19
20#include <memory>
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000021#include <random>
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000022#include <string>
23
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000024#include "perfetto/base/thread_checker.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000025#include "perfetto/tracing/core/data_source_descriptor.h"
26#include "perfetto/tracing/core/producer.h"
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000027#include "perfetto/tracing/core/trace_config.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000028#include "perfetto/tracing/ipc/producer_ipc_client.h"
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000029#include "src/base/test/test_task_runner.h"
30
31namespace perfetto {
32
33class FakeProducer : public Producer {
34 public:
35 explicit FakeProducer(const std::string& name);
36 ~FakeProducer() override;
37
Sami Kyostila32e0b542018-02-14 08:55:43 +000038 void Connect(const char* socket_name,
39 base::TaskRunner* task_runner,
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000040 std::function<void()> on_create_data_source_instance);
41
42 // Produces a batch of events (as configured in the DataSourceConfig) and
43 // posts a callback when the service acknowledges the commit.
44 void ProduceEventBatch(std::function<void()> callback);
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000045
46 // Producer implementation.
47 void OnConnect() override;
48 void OnDisconnect() override;
49 void CreateDataSourceInstance(DataSourceInstanceID,
50 const DataSourceConfig& source_config) override;
51 void TearDownDataSourceInstance(DataSourceInstanceID) override;
Isabelle Taylor69faa902018-03-21 15:42:03 +000052 void OnTracingStart() override;
53 void OnTracingStop() override;
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000054
55 private:
56 void Shutdown();
57
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000058 base::ThreadChecker thread_checker_;
59 base::TaskRunner* task_runner_ = nullptr;
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000060 std::string name_;
61 DataSourceID id_ = 0;
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000062 std::minstd_rand0 rnd_engine_;
63 size_t message_count_ = 0;
64 std::function<void()> on_create_data_source_instance_;
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000065 std::unique_ptr<Service::ProducerEndpoint> endpoint_;
Lalit Magantibfc3d3e2018-03-22 20:28:38 +000066 std::unique_ptr<TraceWriter> trace_writer_;
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000067};
68
69} // namespace perfetto
70
71#endif // TEST_FAKE_PRODUCER_H_