2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #if defined(WEBRTC_POSIX)
13 #endif // WEBRTC_POSIX
15 // TODO: Remove this once the cause of sporadic failures in these
16 // tests is tracked down.
19 #if defined(WEBRTC_WIN)
20 #include "webrtc/base/win32.h"
23 #include "webrtc/base/common.h"
24 #include "webrtc/base/gunit.h"
25 #include "webrtc/base/logging.h"
26 #include "webrtc/base/task.h"
27 #include "webrtc/base/taskrunner.h"
28 #include "webrtc/base/thread.h"
29 #include "webrtc/base/timeutils.h"
33 static int64 GetCurrentTime() {
34 return static_cast<int64>(Time()) * 10000;
37 // feel free to change these numbers. Note that '0' won't work, though
38 #define STUCK_TASK_COUNT 5
39 #define HAPPY_TASK_COUNT 20
41 // this is a generic timeout task which, when it signals timeout, will
42 // include the unique ID of the task in the signal (we don't use this
43 // in production code because we haven't yet had occasion to generate
44 // an array of the same types of task)
46 class IdTimeoutTask : public Task, public sigslot::has_slots<> {
48 explicit IdTimeoutTask(TaskParent *parent) : Task(parent) {
49 SignalTimeout.connect(this, &IdTimeoutTask::OnLocalTimeout);
52 sigslot::signal1<const int> SignalTimeoutId;
53 sigslot::signal1<const int> SignalDoneId;
55 virtual int ProcessStart() {
56 return STATE_RESPONSE;
59 void OnLocalTimeout() {
60 SignalTimeoutId(unique_id());
65 SignalDoneId(unique_id());
70 class StuckTask : public IdTimeoutTask {
72 explicit StuckTask(TaskParent *parent) : IdTimeoutTask(parent) {}
73 virtual int ProcessStart() {
78 class HappyTask : public IdTimeoutTask {
80 explicit HappyTask(TaskParent *parent) : IdTimeoutTask(parent) {
81 time_to_perform_ = rand() % (STUCK_TASK_COUNT / 2);
83 virtual int ProcessStart() {
84 if (ElapsedTime() > (time_to_perform_ * 1000 * 10000))
85 return STATE_RESPONSE;
94 // simple implementation of a task runner which uses Windows'
95 // GetSystemTimeAsFileTime() to get the current clock ticks
97 class MyTaskRunner : public TaskRunner {
99 virtual void WakeTasks() { RunTasks(); }
100 virtual int64 CurrentTime() {
101 return GetCurrentTime();
104 bool timeout_change() const {
105 return timeout_change_;
108 void clear_timeout_change() {
109 timeout_change_ = false;
112 virtual void OnTimeoutChange() {
113 timeout_change_ = true;
115 bool timeout_change_;
119 // this unit test is primarily concerned (for now) with the timeout
120 // functionality in tasks. It works as follows:
122 // * Create a bunch of tasks, some "stuck" (ie., guaranteed to timeout)
123 // and some "happy" (will immediately finish).
124 // * Set the timeout on the "stuck" tasks to some number of seconds between
125 // 1 and the number of stuck tasks
126 // * Start all the stuck & happy tasks in random order
127 // * Wait "number of stuck tasks" seconds and make sure everything timed out
129 class TaskTest : public sigslot::has_slots<> {
133 // no need to delete any tasks; the task runner owns them
137 // create and configure tasks
138 for (int i = 0; i < STUCK_TASK_COUNT; ++i) {
139 stuck_[i].task_ = new StuckTask(&task_runner_);
140 stuck_[i].task_->SignalTimeoutId.connect(this,
141 &TaskTest::OnTimeoutStuck);
142 stuck_[i].timed_out_ = false;
143 stuck_[i].xlat_ = stuck_[i].task_->unique_id();
144 stuck_[i].task_->set_timeout_seconds(i + 1);
145 LOG(LS_INFO) << "Task " << stuck_[i].xlat_ << " created with timeout "
146 << stuck_[i].task_->timeout_seconds();
149 for (int i = 0; i < HAPPY_TASK_COUNT; ++i) {
150 happy_[i].task_ = new HappyTask(&task_runner_);
151 happy_[i].task_->SignalTimeoutId.connect(this,
152 &TaskTest::OnTimeoutHappy);
153 happy_[i].task_->SignalDoneId.connect(this,
154 &TaskTest::OnDoneHappy);
155 happy_[i].timed_out_ = false;
156 happy_[i].xlat_ = happy_[i].task_->unique_id();
159 // start all the tasks in random order
162 for (int i = 0; i < STUCK_TASK_COUNT + HAPPY_TASK_COUNT; ++i) {
163 if ((stuck_index < STUCK_TASK_COUNT) &&
164 (happy_index < HAPPY_TASK_COUNT)) {
165 if (rand() % 2 == 1) {
166 stuck_[stuck_index++].task_->Start();
168 happy_[happy_index++].task_->Start();
170 } else if (stuck_index < STUCK_TASK_COUNT) {
171 stuck_[stuck_index++].task_->Start();
173 happy_[happy_index++].task_->Start();
177 for (int i = 0; i < STUCK_TASK_COUNT; ++i) {
178 std::cout << "Stuck task #" << i << " timeout is " <<
179 stuck_[i].task_->timeout_seconds() << " at " <<
180 stuck_[i].task_->timeout_time() << std::endl;
183 // just a little self-check to make sure we started all the tasks
184 ASSERT_EQ(STUCK_TASK_COUNT, stuck_index);
185 ASSERT_EQ(HAPPY_TASK_COUNT, happy_index);
187 // run the unblocked tasks
188 LOG(LS_INFO) << "Running tasks";
189 task_runner_.RunTasks();
191 std::cout << "Start time is " << GetCurrentTime() << std::endl;
193 // give all the stuck tasks time to timeout
194 for (int i = 0; !task_runner_.AllChildrenDone() && i < STUCK_TASK_COUNT;
196 Thread::Current()->ProcessMessages(1000);
197 for (int j = 0; j < HAPPY_TASK_COUNT; ++j) {
198 if (happy_[j].task_) {
199 happy_[j].task_->Wake();
202 LOG(LS_INFO) << "Polling tasks";
203 task_runner_.PollTasks();
206 // We see occasional test failures here due to the stuck tasks not having
207 // timed-out yet, which seems like it should be impossible. To help track
208 // this down we have added logging of the timing information, which we send
209 // directly to stdout so that we get it in opt builds too.
210 std::cout << "End time is " << GetCurrentTime() << std::endl;
213 void OnTimeoutStuck(const int id) {
214 LOG(LS_INFO) << "Timed out task " << id;
217 for (i = 0; i < STUCK_TASK_COUNT; ++i) {
218 if (stuck_[i].xlat_ == id) {
219 stuck_[i].timed_out_ = true;
220 stuck_[i].task_ = NULL;
225 // getting a bad ID here is a failure, but let's continue
226 // running to see what else might go wrong
227 EXPECT_LT(i, STUCK_TASK_COUNT);
230 void OnTimeoutHappy(const int id) {
232 for (i = 0; i < HAPPY_TASK_COUNT; ++i) {
233 if (happy_[i].xlat_ == id) {
234 happy_[i].timed_out_ = true;
235 happy_[i].task_ = NULL;
240 // getting a bad ID here is a failure, but let's continue
241 // running to see what else might go wrong
242 EXPECT_LT(i, HAPPY_TASK_COUNT);
245 void OnDoneHappy(const int id) {
247 for (i = 0; i < HAPPY_TASK_COUNT; ++i) {
248 if (happy_[i].xlat_ == id) {
249 happy_[i].task_ = NULL;
254 // getting a bad ID here is a failure, but let's continue
255 // running to see what else might go wrong
256 EXPECT_LT(i, HAPPY_TASK_COUNT);
259 void check_passed() {
260 EXPECT_TRUE(task_runner_.AllChildrenDone());
262 // make sure none of our happy tasks timed out
263 for (int i = 0; i < HAPPY_TASK_COUNT; ++i) {
264 EXPECT_FALSE(happy_[i].timed_out_);
267 // make sure all of our stuck tasks timed out
268 for (int i = 0; i < STUCK_TASK_COUNT; ++i) {
269 EXPECT_TRUE(stuck_[i].timed_out_);
270 if (!stuck_[i].timed_out_) {
271 std::cout << "Stuck task #" << i << " timeout is at "
272 << stuck_[i].task_->timeout_time() << std::endl;
281 IdTimeoutTask *task_;
286 MyTaskRunner task_runner_;
287 TaskInfo stuck_[STUCK_TASK_COUNT];
288 TaskInfo happy_[HAPPY_TASK_COUNT];
291 TEST(start_task_test, Timeout) {
294 task_test.check_passed();
297 // Test for aborting the task while it is running
299 class AbortTask : public Task {
301 explicit AbortTask(TaskParent *parent) : Task(parent) {
302 set_timeout_seconds(1);
305 virtual int ProcessStart() {
310 DISALLOW_EVIL_CONSTRUCTORS(AbortTask);
313 class TaskAbortTest : public sigslot::has_slots<> {
317 // no need to delete any tasks; the task runner owns them
321 Task *abort_task = new AbortTask(&task_runner_);
322 abort_task->SignalTimeout.connect(this, &TaskAbortTest::OnTimeout);
326 task_runner_.RunTasks();
331 FAIL() << "Task timed out instead of aborting.";
334 MyTaskRunner task_runner_;
335 DISALLOW_EVIL_CONSTRUCTORS(TaskAbortTest);
338 TEST(start_task_test, Abort) {
339 TaskAbortTest abort_test;
343 // Test for aborting a task to verify that it does the Wake operation
344 // which gets it deleted.
346 class SetBoolOnDeleteTask : public Task {
348 SetBoolOnDeleteTask(TaskParent *parent, bool *set_when_deleted)
350 set_when_deleted_(set_when_deleted) {
351 EXPECT_TRUE(NULL != set_when_deleted);
352 EXPECT_FALSE(*set_when_deleted);
355 virtual ~SetBoolOnDeleteTask() {
356 *set_when_deleted_ = true;
359 virtual int ProcessStart() {
360 return STATE_BLOCKED;
364 bool* set_when_deleted_;
365 DISALLOW_EVIL_CONSTRUCTORS(SetBoolOnDeleteTask);
368 class AbortShouldWakeTest : public sigslot::has_slots<> {
370 AbortShouldWakeTest() {}
372 // no need to delete any tasks; the task runner owns them
373 ~AbortShouldWakeTest() {}
376 bool task_deleted = false;
377 Task *task_to_abort = new SetBoolOnDeleteTask(&task_runner_, &task_deleted);
378 task_to_abort->Start();
380 // Task::Abort() should call TaskRunner::WakeTasks(). WakeTasks calls
381 // TaskRunner::RunTasks() immediately which should delete the task.
382 task_to_abort->Abort();
383 EXPECT_TRUE(task_deleted);
386 // avoid a crash (due to referencing a local variable)
387 // if the test fails.
388 task_runner_.RunTasks();
394 FAIL() << "Task timed out instead of aborting.";
397 MyTaskRunner task_runner_;
398 DISALLOW_EVIL_CONSTRUCTORS(AbortShouldWakeTest);
401 TEST(start_task_test, AbortShouldWake) {
402 AbortShouldWakeTest abort_should_wake_test;
403 abort_should_wake_test.Start();
406 // Validate that TaskRunner's OnTimeoutChange gets called appropriately
407 // * When a task calls UpdateTaskTimeout
408 // * When the next timeout task time, times out
409 class TimeoutChangeTest : public sigslot::has_slots<> {
412 : task_count_(ARRAY_SIZE(stuck_tasks_)) {}
414 // no need to delete any tasks; the task runner owns them
415 ~TimeoutChangeTest() {}
418 for (int i = 0; i < task_count_; ++i) {
419 stuck_tasks_[i] = new StuckTask(&task_runner_);
420 stuck_tasks_[i]->set_timeout_seconds(i + 2);
421 stuck_tasks_[i]->SignalTimeoutId.connect(this,
422 &TimeoutChangeTest::OnTimeoutId);
425 for (int i = task_count_ - 1; i >= 0; --i) {
426 stuck_tasks_[i]->Start();
428 task_runner_.clear_timeout_change();
430 // At this point, our timeouts are set as follows
431 // task[0] is 2 seconds, task[1] at 3 seconds, etc.
433 stuck_tasks_[0]->set_timeout_seconds(2);
434 // Now, task[0] is 2 seconds, task[1] at 3 seconds...
435 // so timeout change shouldn't be called.
436 EXPECT_FALSE(task_runner_.timeout_change());
437 task_runner_.clear_timeout_change();
439 stuck_tasks_[0]->set_timeout_seconds(1);
440 // task[0] is 1 seconds, task[1] at 3 seconds...
441 // The smallest timeout got smaller so timeout change be called.
442 EXPECT_TRUE(task_runner_.timeout_change());
443 task_runner_.clear_timeout_change();
445 stuck_tasks_[1]->set_timeout_seconds(2);
446 // task[0] is 1 seconds, task[1] at 2 seconds...
447 // The smallest timeout is still 1 second so no timeout change.
448 EXPECT_FALSE(task_runner_.timeout_change());
449 task_runner_.clear_timeout_change();
451 while (task_count_ > 0) {
452 int previous_count = task_count_;
453 task_runner_.PollTasks();
454 if (previous_count != task_count_) {
455 // We only get here when a task times out. When that
456 // happens, the timeout change should get called because
457 // the smallest timeout is now in the past.
458 EXPECT_TRUE(task_runner_.timeout_change());
459 task_runner_.clear_timeout_change();
461 Thread::Current()->socketserver()->Wait(500, false);
466 void OnTimeoutId(const int id) {
467 for (int i = 0; i < ARRAY_SIZE(stuck_tasks_); ++i) {
468 if (stuck_tasks_[i] && stuck_tasks_[i]->unique_id() == id) {
470 stuck_tasks_[i] = NULL;
476 MyTaskRunner task_runner_;
477 StuckTask* (stuck_tasks_[3]);
479 DISALLOW_EVIL_CONSTRUCTORS(TimeoutChangeTest);
482 TEST(start_task_test, TimeoutChange) {
483 TimeoutChangeTest timeout_change_test;
484 timeout_change_test.Start();
487 class DeleteTestTaskRunner : public TaskRunner {
489 DeleteTestTaskRunner() {
491 virtual void WakeTasks() { }
492 virtual int64 CurrentTime() {
493 return GetCurrentTime();
496 DISALLOW_EVIL_CONSTRUCTORS(DeleteTestTaskRunner);
499 TEST(unstarted_task_test, DeleteTask) {
500 // This test ensures that we don't
501 // crash if a task is deleted without running it.
502 DeleteTestTaskRunner task_runner;
503 HappyTask* happy_task = new HappyTask(&task_runner);
506 // try deleting the task directly
507 HappyTask* child_happy_task = new HappyTask(happy_task);
508 delete child_happy_task;
510 // run the unblocked tasks
511 task_runner.RunTasks();
514 TEST(unstarted_task_test, DoNotDeleteTask1) {
515 // This test ensures that we don't
516 // crash if a task runner is deleted without
517 // running a certain task.
518 DeleteTestTaskRunner task_runner;
519 HappyTask* happy_task = new HappyTask(&task_runner);
522 HappyTask* child_happy_task = new HappyTask(happy_task);
523 child_happy_task->Start();
525 // Never run the tasks
528 TEST(unstarted_task_test, DoNotDeleteTask2) {
529 // This test ensures that we don't
530 // crash if a taskrunner is delete with a
531 // task that has never been started.
532 DeleteTestTaskRunner task_runner;
533 HappyTask* happy_task = new HappyTask(&task_runner);
536 // Do not start the task.
537 // Note: this leaks memory, so don't do this.
538 // Instead, always run your tasks or delete them.
539 new HappyTask(happy_task);
541 // run the unblocked tasks
542 task_runner.RunTasks();