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 #ifndef TEST_TASK_RUNNER_THREAD_DELEGATES_H_ 18 #define TEST_TASK_RUNNER_THREAD_DELEGATES_H_ 19 20 #include "perfetto/tracing/ipc/service_ipc_host.h" 21 #include "src/traced/probes/probes_producer.h" 22 #include "test/fake_producer.h" 23 #include "test/task_runner_thread.h" 24 25 namespace perfetto { 26 // This is used only in daemon starting integrations tests. 27 class ServiceDelegate : public ThreadDelegate { 28 public: ServiceDelegate(const std::string & producer_socket,const std::string & consumer_socket)29 ServiceDelegate(const std::string& producer_socket, 30 const std::string& consumer_socket) 31 : producer_socket_(producer_socket), consumer_socket_(consumer_socket) {} 32 ~ServiceDelegate() override = default; 33 Initialize(base::TaskRunner * task_runner)34 void Initialize(base::TaskRunner* task_runner) override { 35 svc_ = ServiceIPCHost::CreateInstance(task_runner); 36 unlink(producer_socket_.c_str()); 37 unlink(consumer_socket_.c_str()); 38 svc_->Start(producer_socket_.c_str(), consumer_socket_.c_str()); 39 } 40 41 private: 42 std::string producer_socket_; 43 std::string consumer_socket_; 44 std::unique_ptr<ServiceIPCHost> svc_; 45 }; 46 47 // This is used only in daemon starting integrations tests. 48 class ProbesProducerDelegate : public ThreadDelegate { 49 public: ProbesProducerDelegate(const std::string & producer_socket)50 ProbesProducerDelegate(const std::string& producer_socket) 51 : producer_socket_(producer_socket) {} 52 ~ProbesProducerDelegate() override = default; 53 Initialize(base::TaskRunner * task_runner)54 void Initialize(base::TaskRunner* task_runner) override { 55 producer_.reset(new ProbesProducer); 56 producer_->ConnectWithRetries(producer_socket_.c_str(), task_runner); 57 } 58 59 private: 60 std::string producer_socket_; 61 std::unique_ptr<ProbesProducer> producer_; 62 }; 63 64 class FakeProducerDelegate : public ThreadDelegate { 65 public: FakeProducerDelegate(const std::string & producer_socket,std::function<void ()> setup_callback,std::function<void ()> connect_callback)66 FakeProducerDelegate(const std::string& producer_socket, 67 std::function<void()> setup_callback, 68 std::function<void()> connect_callback) 69 : producer_socket_(producer_socket), 70 setup_callback_(std::move(setup_callback)), 71 connect_callback_(std::move(connect_callback)) {} 72 ~FakeProducerDelegate() override = default; 73 Initialize(base::TaskRunner * task_runner)74 void Initialize(base::TaskRunner* task_runner) override { 75 producer_.reset(new FakeProducer("android.perfetto.FakeProducer")); 76 producer_->Connect(producer_socket_.c_str(), task_runner, 77 std::move(setup_callback_), 78 std::move(connect_callback_)); 79 } 80 producer()81 FakeProducer* producer() { return producer_.get(); } 82 83 private: 84 std::string producer_socket_; 85 std::unique_ptr<FakeProducer> producer_; 86 std::function<void()> setup_callback_; 87 std::function<void()> connect_callback_; 88 }; 89 } // namespace perfetto 90 91 #endif // TEST_TASK_RUNNER_THREAD_DELEGATES_H_ 92