summaryrefslogtreecommitdiff
path: root/chromium/media/midi/task_service_unittest.cc
blob: f2bc8fb3cd89ee8463849b4bbe94d5f53d2052d1 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/midi/task_service.h"

#include <memory>

#include "base/bind.h"
#include "base/callback.h"
#include "base/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/synchronization/lock.h"
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace midi {

namespace {

enum {
  kDefaultRunner = TaskService::kDefaultRunnerId,
  kFirstRunner,
  kSecondRunner
};

base::WaitableEvent* GetEvent() {
  static base::WaitableEvent* event =
      new base::WaitableEvent(base::WaitableEvent::ResetPolicy::MANUAL,
                              base::WaitableEvent::InitialState::NOT_SIGNALED);
  return event;
}

void SignalEvent() {
  GetEvent()->Signal();
}

void WaitEvent() {
  GetEvent()->Wait();
}

void ResetEvent() {
  GetEvent()->Reset();
}

class TaskServiceClient {
 public:
  TaskServiceClient(TaskService* task_service)
      : task_service_(task_service),
        wait_task_event_(std::make_unique<base::WaitableEvent>(
            base::WaitableEvent::ResetPolicy::MANUAL,
            base::WaitableEvent::InitialState::NOT_SIGNALED)),
        count_(0u) {
    DCHECK(task_service);
  }

  bool Bind() { return task_service()->BindInstance(); }

  bool Unbind() { return task_service()->UnbindInstance(); }

  void PostBoundTask(TaskService::RunnerId runner_id) {
    task_service()->PostBoundTask(
        runner_id, base::BindOnce(&TaskServiceClient::IncrementCount,
                                  base::Unretained(this)));
  }

  void PostBoundSignalTask(TaskService::RunnerId runner_id) {
    task_service()->PostBoundTask(
        runner_id, base::BindOnce(&TaskServiceClient::SignalEvent,
                                  base::Unretained(this)));
  }

  void PostBoundWaitTask(TaskService::RunnerId runner_id) {
    wait_task_event_->Reset();
    task_service()->PostBoundTask(
        runner_id,
        base::BindOnce(&TaskServiceClient::WaitEvent, base::Unretained(this)));
  }

  void PostBoundDelayedSignalTask(TaskService::RunnerId runner_id) {
    task_service()->PostBoundDelayedTask(
        runner_id,
        base::BindOnce(&TaskServiceClient::SignalEvent, base::Unretained(this)),
        base::TimeDelta::FromMilliseconds(100));
  }

  void WaitTask() { wait_task_event_->Wait(); }

  size_t count() {
    base::AutoLock lock(lock_);
    return count_;
  }

 private:
  TaskService* task_service() { return task_service_; }

  void IncrementCount() {
    base::AutoLock lock(lock_);
    count_++;
  }

  void SignalEvent() {
    IncrementCount();
    midi::SignalEvent();
  }

  void WaitEvent() {
    IncrementCount();
    wait_task_event_->Signal();
    midi::WaitEvent();
  }

  base::Lock lock_;
  TaskService* task_service_;
  std::unique_ptr<base::WaitableEvent> wait_task_event_;
  size_t count_;

  DISALLOW_COPY_AND_ASSIGN(TaskServiceClient);
};

class MidiTaskServiceTest : public ::testing::Test {
 public:
  MidiTaskServiceTest() = default;

 protected:
  TaskService* task_service() { return &task_service_; }
  void RunUntilIdle() { task_runner_->RunUntilIdle(); }

 private:
  void SetUp() override {
    ResetEvent();
    task_runner_ = new base::TestSimpleTaskRunner();
    thread_task_runner_handle_ =
        std::make_unique<base::ThreadTaskRunnerHandle>(task_runner_);
  }

  void TearDown() override {
    thread_task_runner_handle_.reset();
    task_runner_.reset();
  }

  scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
  std::unique_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_;
  TaskService task_service_;

  DISALLOW_COPY_AND_ASSIGN(MidiTaskServiceTest);
};

// Tests if posted tasks without calling BindInstance() are ignored.
TEST_F(MidiTaskServiceTest, RunUnauthorizedBoundTask) {
  std::unique_ptr<TaskServiceClient> client =
      std::make_unique<TaskServiceClient>(task_service());

  client->PostBoundTask(kFirstRunner);

  // Destruct |client| immediately, then see if the posted task is just ignored.
  // If it isn't, another thread will touch the destructed instance and will
  // cause a crash due to a use-after-free.
  client = nullptr;
}

// Tests if invalid BindInstance() calls are correctly rejected, and it does not
// make the service insanity.
TEST_F(MidiTaskServiceTest, BindTwice) {
  std::unique_ptr<TaskServiceClient> client =
      std::make_unique<TaskServiceClient>(task_service());

  EXPECT_TRUE(client->Bind());

  // Should not be able to call BindInstance() twice before unbinding current
  // bound instance.
  EXPECT_FALSE(client->Bind());

  // Should be able to unbind only the first instance.
  EXPECT_TRUE(client->Unbind());
  EXPECT_FALSE(client->Unbind());
}

// Tests if posted static tasks can be processed correctly.
TEST_F(MidiTaskServiceTest, RunStaticTask) {
  std::unique_ptr<TaskServiceClient> client =
      std::make_unique<TaskServiceClient>(task_service());

  EXPECT_TRUE(client->Bind());
  // Should be able to post a static task while an instance is bound.
  task_service()->PostStaticTask(kFirstRunner, base::BindOnce(&SignalEvent));
  WaitEvent();
  EXPECT_TRUE(client->Unbind());

  ResetEvent();

  EXPECT_TRUE(client->Bind());
  task_service()->PostStaticTask(kFirstRunner, base::BindOnce(&SignalEvent));
  // Should be able to unbind the instance to process a static task.
  EXPECT_TRUE(client->Unbind());
  WaitEvent();

  ResetEvent();

  // Should be able to post a static task without a bound instance.
  task_service()->PostStaticTask(kFirstRunner, base::BindOnce(&SignalEvent));
  WaitEvent();
}

// Tests functionalities to run bound tasks.
TEST_F(MidiTaskServiceTest, RunBoundTasks) {
  std::unique_ptr<TaskServiceClient> client =
      std::make_unique<TaskServiceClient>(task_service());

  EXPECT_TRUE(client->Bind());

  // Tests if a post task run.
  EXPECT_EQ(0u, client->count());
  client->PostBoundSignalTask(kFirstRunner);
  WaitEvent();
  EXPECT_EQ(1u, client->count());

  // Tests if another posted task is handled correctly even if the instance is
  // unbound immediately. The posted task should run safely if it starts before
  // UnboundInstance() is call. Otherwise, it should be ignored. It completely
  // depends on timing.
  client->PostBoundTask(kFirstRunner);
  EXPECT_TRUE(client->Unbind());
  client = std::make_unique<TaskServiceClient>(task_service());

  // Tests if an immediate call of another BindInstance() works correctly.
  EXPECT_TRUE(client->Bind());

  // Runs two tasks in two runners.
  ResetEvent();
  client->PostBoundSignalTask(kFirstRunner);
  client->PostBoundTask(kSecondRunner);

  // Waits only the first runner completion to see if the second runner handles
  // the task correctly even if the bound instance is destructed.
  WaitEvent();
  EXPECT_TRUE(client->Unbind());
  client = nullptr;
}

// Tests if a blocking task does not block other task runners.
TEST_F(MidiTaskServiceTest, RunBlockingTask) {
  std::unique_ptr<TaskServiceClient> client =
      std::make_unique<TaskServiceClient>(task_service());

  EXPECT_TRUE(client->Bind());

  // Posts a task that waits until the event is signaled.
  client->PostBoundWaitTask(kFirstRunner);
  // Confirms if the posted task starts. Now, the task should block in the task
  // until the second task is invoked.
  client->WaitTask();

  // Posts another task to the second runner. The task should be able to run
  // even though another posted task is blocking inside a critical section that
  // protects running tasks from an instance unbinding.
  client->PostBoundSignalTask(kSecondRunner);

  // Wait until the second task runs.
  WaitEvent();

  // UnbindInstance() should wait until any running task finishes so that the
  // instance can be destructed safely.
  EXPECT_TRUE(client->Unbind());
  EXPECT_EQ(2u, client->count());
  client = nullptr;
}

// Tests if a bound delayed task runs correctly.
TEST_F(MidiTaskServiceTest, RunBoundDelayedTask) {
  std::unique_ptr<TaskServiceClient> client =
      std::make_unique<TaskServiceClient>(task_service());

  EXPECT_TRUE(client->Bind());

  // Posts a delayed task that signals after 100msec.
  client->PostBoundDelayedSignalTask(kFirstRunner);

  // Wait until the delayed task runs.
  WaitEvent();

  EXPECT_TRUE(client->Unbind());
  EXPECT_EQ(1u, client->count());
  client = nullptr;
}

// Tests if a bound task runs on the thread that bound the instance.
TEST_F(MidiTaskServiceTest, RunBoundTaskOnDefaultRunner) {
  std::unique_ptr<TaskServiceClient> client =
      std::make_unique<TaskServiceClient>(task_service());

  EXPECT_TRUE(client->Bind());

  // Posts a task that increments the count on the caller thread.
  client->PostBoundTask(kDefaultRunner);

  // The posted task should not run until the current message loop is processed.
  EXPECT_EQ(0u, client->count());
  RunUntilIdle();
  EXPECT_EQ(1u, client->count());

  EXPECT_TRUE(client->Unbind());
}

}  // namespace

}  // namespace midi