aboutsummaryrefslogtreecommitdiff
path: root/test/task_runner_thread_delegates.h
blob: 8eecbbae7621ffd07ca5477f46355839865e0c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TEST_TASK_RUNNER_THREAD_DELEGATES_H_
#define TEST_TASK_RUNNER_THREAD_DELEGATES_H_

#include "perfetto/tracing/ipc/service_ipc_host.h"
#include "src/traced/probes/probes_producer.h"
#include "test/fake_producer.h"
#include "test/task_runner_thread.h"

namespace perfetto {
// This is used only in daemon starting integrations tests.
class ServiceDelegate : public ThreadDelegate {
 public:
  ServiceDelegate(const std::string& producer_socket,
                  const std::string& consumer_socket)
      : producer_socket_(producer_socket), consumer_socket_(consumer_socket) {}
  ~ServiceDelegate() override = default;

  void Initialize(base::TaskRunner* task_runner) override {
    svc_ = ServiceIPCHost::CreateInstance(task_runner);
    unlink(producer_socket_.c_str());
    unlink(consumer_socket_.c_str());
    svc_->Start(producer_socket_.c_str(), consumer_socket_.c_str());
  }

 private:
  std::string producer_socket_;
  std::string consumer_socket_;
  std::unique_ptr<ServiceIPCHost> svc_;
};

// This is used only in daemon starting integrations tests.
class ProbesProducerDelegate : public ThreadDelegate {
 public:
  ProbesProducerDelegate(const std::string& producer_socket)
      : producer_socket_(producer_socket) {}
  ~ProbesProducerDelegate() override = default;

  void Initialize(base::TaskRunner* task_runner) override {
    producer_.reset(new ProbesProducer);
    producer_->ConnectWithRetries(producer_socket_.c_str(), task_runner);
  }

 private:
  std::string producer_socket_;
  std::unique_ptr<ProbesProducer> producer_;
};

class FakeProducerDelegate : public ThreadDelegate {
 public:
  FakeProducerDelegate(const std::string& producer_socket,
                       std::function<void()> setup_callback,
                       std::function<void()> connect_callback)
      : producer_socket_(producer_socket),
        setup_callback_(std::move(setup_callback)),
        connect_callback_(std::move(connect_callback)) {}
  ~FakeProducerDelegate() override = default;

  void Initialize(base::TaskRunner* task_runner) override {
    producer_.reset(new FakeProducer("android.perfetto.FakeProducer"));
    producer_->Connect(producer_socket_.c_str(), task_runner,
                       std::move(setup_callback_),
                       std::move(connect_callback_));
  }

  FakeProducer* producer() { return producer_.get(); }

 private:
  std::string producer_socket_;
  std::unique_ptr<FakeProducer> producer_;
  std::function<void()> setup_callback_;
  std::function<void()> connect_callback_;
};
}  // namespace perfetto

#endif  // TEST_TASK_RUNNER_THREAD_DELEGATES_H_