You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
16 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifdef POSIX
  28. #include <sys/time.h>
  29. #endif // POSIX
  30. // TODO: Remove this once the cause of sporadic failures in these
  31. // tests is tracked down.
  32. #include <iostream>
  33. #ifdef WIN32
  34. #include "talk/base/win32.h"
  35. #endif // WIN32
  36. #include "talk/base/common.h"
  37. #include "talk/base/gunit.h"
  38. #include "talk/base/logging.h"
  39. #include "talk/base/task.h"
  40. #include "talk/base/taskrunner.h"
  41. #include "talk/base/thread.h"
  42. #include "talk/base/timeutils.h"
  43. namespace talk_base {
  44. static int64 GetCurrentTime() {
  45. return static_cast<int64>(Time()) * 10000;
  46. }
  47. // feel free to change these numbers. Note that '0' won't work, though
  48. #define STUCK_TASK_COUNT 5
  49. #define HAPPY_TASK_COUNT 20
  50. // this is a generic timeout task which, when it signals timeout, will
  51. // include the unique ID of the task in the signal (we don't use this
  52. // in production code because we haven't yet had occasion to generate
  53. // an array of the same types of task)
  54. class IdTimeoutTask : public Task, public sigslot::has_slots<> {
  55. public:
  56. explicit IdTimeoutTask(TaskParent *parent) : Task(parent) {
  57. SignalTimeout.connect(this, &IdTimeoutTask::OnLocalTimeout);
  58. }
  59. sigslot::signal1<const int> SignalTimeoutId;
  60. sigslot::signal1<const int> SignalDoneId;
  61. virtual int ProcessStart() {
  62. return STATE_RESPONSE;
  63. }
  64. void OnLocalTimeout() {
  65. SignalTimeoutId(unique_id());
  66. }
  67. protected:
  68. virtual void Stop() {
  69. SignalDoneId(unique_id());
  70. Task::Stop();
  71. }
  72. };
  73. class StuckTask : public IdTimeoutTask {
  74. public:
  75. explicit StuckTask(TaskParent *parent) : IdTimeoutTask(parent) {}
  76. virtual int ProcessStart() {
  77. return STATE_BLOCKED;
  78. }
  79. };
  80. class HappyTask : public IdTimeoutTask {
  81. public:
  82. explicit HappyTask(TaskParent *parent) : IdTimeoutTask(parent) {
  83. time_to_perform_ = rand() % (STUCK_TASK_COUNT / 2);
  84. }
  85. virtual int ProcessStart() {
  86. if (ElapsedTime() > (time_to_perform_ * 1000 * 10000))
  87. return STATE_RESPONSE;
  88. else
  89. return STATE_BLOCKED;
  90. }
  91. private:
  92. int time_to_perform_;
  93. };
  94. // simple implementation of a task runner which uses Windows'
  95. // GetSystemTimeAsFileTime() to get the current clock ticks
  96. class MyTaskRunner : public TaskRunner {
  97. public:
  98. virtual void WakeTasks() { RunTasks(); }
  99. virtual int64 CurrentTime() {
  100. return GetCurrentTime();
  101. }
  102. bool timeout_change() const {
  103. return timeout_change_;
  104. }
  105. void clear_timeout_change() {
  106. timeout_change_ = false;
  107. }
  108. protected:
  109. virtual void OnTimeoutChange() {
  110. timeout_change_ = true;
  111. }
  112. bool timeout_change_;
  113. };
  114. //
  115. // this unit test is primarily concerned (for now) with the timeout
  116. // functionality in tasks. It works as follows:
  117. //
  118. // * Create a bunch of tasks, some "stuck" (ie., guaranteed to timeout)
  119. // and some "happy" (will immediately finish).
  120. // * Set the timeout on the "stuck" tasks to some number of seconds between
  121. // 1 and the number of stuck tasks
  122. // * Start all the stuck & happy tasks in random order
  123. // * Wait "number of stuck tasks" seconds and make sure everything timed out
  124. class TaskTest : public sigslot::has_slots<> {
  125. public:
  126. TaskTest() {}
  127. // no need to delete any tasks; the task runner owns them
  128. ~TaskTest() {}
  129. void Start() {
  130. // create and configure tasks
  131. for (int i = 0; i < STUCK_TASK_COUNT; ++i) {
  132. stuck_[i].task_ = new StuckTask(&task_runner_);
  133. stuck_[i].task_->SignalTimeoutId.connect(this,
  134. &TaskTest::OnTimeoutStuck);
  135. stuck_[i].timed_out_ = false;
  136. stuck_[i].xlat_ = stuck_[i].task_->unique_id();
  137. stuck_[i].task_->set_timeout_seconds(i + 1);
  138. LOG(LS_INFO) << "Task " << stuck_[i].xlat_ << " created with timeout "
  139. << stuck_[i].task_->timeout_seconds();
  140. }
  141. for (int i = 0; i < HAPPY_TASK_COUNT; ++i) {
  142. happy_[i].task_ = new HappyTask(&task_runner_);
  143. happy_[i].task_->SignalTimeoutId.connect(this,
  144. &TaskTest::OnTimeoutHappy);
  145. happy_[i].task_->SignalDoneId.connect(this,
  146. &TaskTest::OnDoneHappy);
  147. happy_[i].timed_out_ = false;
  148. happy_[i].xlat_ = happy_[i].task_->unique_id();
  149. }
  150. // start all the tasks in random order
  151. int stuck_index = 0;
  152. int happy_index = 0;
  153. for (int i = 0; i < STUCK_TASK_COUNT + HAPPY_TASK_COUNT; ++i) {
  154. if ((stuck_index < STUCK_TASK_COUNT) &&
  155. (happy_index < HAPPY_TASK_COUNT)) {
  156. if (rand() % 2 == 1) {
  157. stuck_[stuck_index++].task_->Start();
  158. } else {
  159. happy_[happy_index++].task_->Start();
  160. }
  161. } else if (stuck_index < STUCK_TASK_COUNT) {
  162. stuck_[stuck_index++].task_->Start();
  163. } else {
  164. happy_[happy_index++].task_->Start();
  165. }
  166. }
  167. for (int i = 0; i < STUCK_TASK_COUNT; ++i) {
  168. std::cout << "Stuck task #" << i << " timeout is " <<
  169. stuck_[i].task_->timeout_seconds() << " at " <<
  170. stuck_[i].task_->timeout_time() << std::endl;
  171. }
  172. // just a little self-check to make sure we started all the tasks
  173. ASSERT_EQ(STUCK_TASK_COUNT, stuck_index);
  174. ASSERT_EQ(HAPPY_TASK_COUNT, happy_index);
  175. // run the unblocked tasks
  176. LOG(LS_INFO) << "Running tasks";
  177. task_runner_.RunTasks();
  178. std::cout << "Start time is " << GetCurrentTime() << std::endl;
  179. // give all the stuck tasks time to timeout
  180. for (int i = 0; !task_runner_.AllChildrenDone() && i < STUCK_TASK_COUNT;
  181. ++i) {
  182. Thread::Current()->ProcessMessages(1000);
  183. for (int j = 0; j < HAPPY_TASK_COUNT; ++j) {
  184. if (happy_[j].task_) {
  185. happy_[j].task_->Wake();
  186. }
  187. }
  188. LOG(LS_INFO) << "Polling tasks";
  189. task_runner_.PollTasks();
  190. }
  191. // We see occasional test failures here due to the stuck tasks not having
  192. // timed-out yet, which seems like it should be impossible. To help track
  193. // this down we have added logging of the timing information, which we send
  194. // directly to stdout so that we get it in opt builds too.
  195. std::cout << "End time is " << GetCurrentTime() << std::endl;
  196. }
  197. void OnTimeoutStuck(const int id) {
  198. LOG(LS_INFO) << "Timed out task " << id;
  199. int i;
  200. for (i = 0; i < STUCK_TASK_COUNT; ++i) {
  201. if (stuck_[i].xlat_ == id) {
  202. stuck_[i].timed_out_ = true;
  203. stuck_[i].task_ = NULL;
  204. break;
  205. }
  206. }
  207. // getting a bad ID here is a failure, but let's continue
  208. // running to see what else might go wrong
  209. EXPECT_LT(i, STUCK_TASK_COUNT);
  210. }
  211. void OnTimeoutHappy(const int id) {
  212. int i;
  213. for (i = 0; i < HAPPY_TASK_COUNT; ++i) {
  214. if (happy_[i].xlat_ == id) {
  215. happy_[i].timed_out_ = true;
  216. happy_[i].task_ = NULL;
  217. break;
  218. }
  219. }
  220. // getting a bad ID here is a failure, but let's continue
  221. // running to see what else might go wrong
  222. EXPECT_LT(i, HAPPY_TASK_COUNT);
  223. }
  224. void OnDoneHappy(const int id) {
  225. int i;
  226. for (i = 0; i < HAPPY_TASK_COUNT; ++i) {
  227. if (happy_[i].xlat_ == id) {
  228. happy_[i].task_ = NULL;
  229. break;
  230. }
  231. }
  232. // getting a bad ID here is a failure, but let's continue
  233. // running to see what else might go wrong
  234. EXPECT_LT(i, HAPPY_TASK_COUNT);
  235. }
  236. void check_passed() {
  237. EXPECT_TRUE(task_runner_.AllChildrenDone());
  238. // make sure none of our happy tasks timed out
  239. for (int i = 0; i < HAPPY_TASK_COUNT; ++i) {
  240. EXPECT_FALSE(happy_[i].timed_out_);
  241. }
  242. // make sure all of our stuck tasks timed out
  243. for (int i = 0; i < STUCK_TASK_COUNT; ++i) {
  244. EXPECT_TRUE(stuck_[i].timed_out_);
  245. if (!stuck_[i].timed_out_) {
  246. std::cout << "Stuck task #" << i << " timeout is at "
  247. << stuck_[i].task_->timeout_time() << std::endl;
  248. }
  249. }
  250. std::cout.flush();
  251. }
  252. private:
  253. struct TaskInfo {
  254. IdTimeoutTask *task_;
  255. bool timed_out_;
  256. int xlat_;
  257. };
  258. MyTaskRunner task_runner_;
  259. TaskInfo stuck_[STUCK_TASK_COUNT];
  260. TaskInfo happy_[HAPPY_TASK_COUNT];
  261. };
  262. TEST(start_task_test, Timeout) {
  263. TaskTest task_test;
  264. task_test.Start();
  265. task_test.check_passed();
  266. }
  267. // Test for aborting the task while it is running
  268. class AbortTask : public Task {
  269. public:
  270. explicit AbortTask(TaskParent *parent) : Task(parent) {
  271. set_timeout_seconds(1);
  272. }
  273. virtual int ProcessStart() {
  274. Abort();
  275. return STATE_NEXT;
  276. }
  277. private:
  278. DISALLOW_EVIL_CONSTRUCTORS(AbortTask);
  279. };
  280. class TaskAbortTest : public sigslot::has_slots<> {
  281. public:
  282. TaskAbortTest() {}
  283. // no need to delete any tasks; the task runner owns them
  284. ~TaskAbortTest() {}
  285. void Start() {
  286. Task *abort_task = new AbortTask(&task_runner_);
  287. abort_task->SignalTimeout.connect(this, &TaskAbortTest::OnTimeout);
  288. abort_task->Start();
  289. // run the task
  290. task_runner_.RunTasks();
  291. }
  292. private:
  293. void OnTimeout() {
  294. FAIL() << "Task timed out instead of aborting.";
  295. }
  296. MyTaskRunner task_runner_;
  297. DISALLOW_EVIL_CONSTRUCTORS(TaskAbortTest);
  298. };
  299. TEST(start_task_test, Abort) {
  300. TaskAbortTest abort_test;
  301. abort_test.Start();
  302. }
  303. // Test for aborting a task to verify that it does the Wake operation
  304. // which gets it deleted.
  305. class SetBoolOnDeleteTask : public Task {
  306. public:
  307. SetBoolOnDeleteTask(TaskParent *parent, bool *set_when_deleted)
  308. : Task(parent),
  309. set_when_deleted_(set_when_deleted) {
  310. EXPECT_TRUE(NULL != set_when_deleted);
  311. EXPECT_FALSE(*set_when_deleted);
  312. }
  313. virtual ~SetBoolOnDeleteTask() {
  314. *set_when_deleted_ = true;
  315. }
  316. virtual int ProcessStart() {
  317. return STATE_BLOCKED;
  318. }
  319. private:
  320. bool* set_when_deleted_;
  321. DISALLOW_EVIL_CONSTRUCTORS(SetBoolOnDeleteTask);
  322. };
  323. class AbortShouldWakeTest : public sigslot::has_slots<> {
  324. public:
  325. AbortShouldWakeTest() {}
  326. // no need to delete any tasks; the task runner owns them
  327. ~AbortShouldWakeTest() {}
  328. void Start() {
  329. bool task_deleted = false;
  330. Task *task_to_abort = new SetBoolOnDeleteTask(&task_runner_, &task_deleted);
  331. task_to_abort->Start();
  332. // Task::Abort() should call TaskRunner::WakeTasks(). WakeTasks calls
  333. // TaskRunner::RunTasks() immediately which should delete the task.
  334. task_to_abort->Abort();
  335. EXPECT_TRUE(task_deleted);
  336. if (!task_deleted) {
  337. // avoid a crash (due to referencing a local variable)
  338. // if the test fails.
  339. task_runner_.RunTasks();
  340. }
  341. }
  342. private:
  343. void OnTimeout() {
  344. FAIL() << "Task timed out instead of aborting.";
  345. }
  346. MyTaskRunner task_runner_;
  347. DISALLOW_EVIL_CONSTRUCTORS(AbortShouldWakeTest);
  348. };
  349. TEST(start_task_test, AbortShouldWake) {
  350. AbortShouldWakeTest abort_should_wake_test;
  351. abort_should_wake_test.Start();
  352. }
  353. // Validate that TaskRunner's OnTimeoutChange gets called appropriately
  354. // * When a task calls UpdateTaskTimeout
  355. // * When the next timeout task time, times out
  356. class TimeoutChangeTest : public sigslot::has_slots<> {
  357. public:
  358. TimeoutChangeTest()
  359. : task_count_(ARRAY_SIZE(stuck_tasks_)) {}
  360. // no need to delete any tasks; the task runner owns them
  361. ~TimeoutChangeTest() {}
  362. void Start() {
  363. for (int i = 0; i < task_count_; ++i) {
  364. stuck_tasks_[i] = new StuckTask(&task_runner_);
  365. stuck_tasks_[i]->set_timeout_seconds(i + 2);
  366. stuck_tasks_[i]->SignalTimeoutId.connect(this,
  367. &TimeoutChangeTest::OnTimeoutId);
  368. }
  369. for (int i = task_count_ - 1; i >= 0; --i) {
  370. stuck_tasks_[i]->Start();
  371. }
  372. task_runner_.clear_timeout_change();
  373. // At this point, our timeouts are set as follows
  374. // task[0] is 2 seconds, task[1] at 3 seconds, etc.
  375. stuck_tasks_[0]->set_timeout_seconds(2);
  376. // Now, task[0] is 2 seconds, task[1] at 3 seconds...
  377. // so timeout change shouldn't be called.
  378. EXPECT_FALSE(task_runner_.timeout_change());
  379. task_runner_.clear_timeout_change();
  380. stuck_tasks_[0]->set_timeout_seconds(1);
  381. // task[0] is 1 seconds, task[1] at 3 seconds...
  382. // The smallest timeout got smaller so timeout change be called.
  383. EXPECT_TRUE(task_runner_.timeout_change());
  384. task_runner_.clear_timeout_change();
  385. stuck_tasks_[1]->set_timeout_seconds(2);
  386. // task[0] is 1 seconds, task[1] at 2 seconds...
  387. // The smallest timeout is still 1 second so no timeout change.
  388. EXPECT_FALSE(task_runner_.timeout_change());
  389. task_runner_.clear_timeout_change();
  390. while (task_count_ > 0) {
  391. int previous_count = task_count_;
  392. task_runner_.PollTasks();
  393. if (previous_count != task_count_) {
  394. // We only get here when a task times out. When that
  395. // happens, the timeout change should get called because
  396. // the smallest timeout is now in the past.
  397. EXPECT_TRUE(task_runner_.timeout_change());
  398. task_runner_.clear_timeout_change();
  399. }
  400. Thread::Current()->socketserver()->Wait(500, false);
  401. }
  402. }
  403. private:
  404. void OnTimeoutId(const int id) {
  405. for (int i = 0; i < ARRAY_SIZE(stuck_tasks_); ++i) {
  406. if (stuck_tasks_[i] && stuck_tasks_[i]->unique_id() == id) {
  407. task_count_--;
  408. stuck_tasks_[i] = NULL;
  409. break;
  410. }
  411. }
  412. }
  413. MyTaskRunner task_runner_;
  414. StuckTask* (stuck_tasks_[3]);
  415. int task_count_;
  416. DISALLOW_EVIL_CONSTRUCTORS(TimeoutChangeTest);
  417. };
  418. TEST(start_task_test, TimeoutChange) {
  419. TimeoutChangeTest timeout_change_test;
  420. timeout_change_test.Start();
  421. }
  422. class DeleteTestTaskRunner : public TaskRunner {
  423. public:
  424. DeleteTestTaskRunner() {
  425. }
  426. virtual void WakeTasks() { }
  427. virtual int64 CurrentTime() {
  428. return GetCurrentTime();
  429. }
  430. private:
  431. DISALLOW_EVIL_CONSTRUCTORS(DeleteTestTaskRunner);
  432. };
  433. TEST(unstarted_task_test, DeleteTask) {
  434. // This test ensures that we don't
  435. // crash if a task is deleted without running it.
  436. DeleteTestTaskRunner task_runner;
  437. HappyTask* happy_task = new HappyTask(&task_runner);
  438. happy_task->Start();
  439. // try deleting the task directly
  440. HappyTask* child_happy_task = new HappyTask(happy_task);
  441. delete child_happy_task;
  442. // run the unblocked tasks
  443. task_runner.RunTasks();
  444. }
  445. TEST(unstarted_task_test, DoNotDeleteTask1) {
  446. // This test ensures that we don't
  447. // crash if a task runner is deleted without
  448. // running a certain task.
  449. DeleteTestTaskRunner task_runner;
  450. HappyTask* happy_task = new HappyTask(&task_runner);
  451. happy_task->Start();
  452. HappyTask* child_happy_task = new HappyTask(happy_task);
  453. child_happy_task->Start();
  454. // Never run the tasks
  455. }
  456. TEST(unstarted_task_test, DoNotDeleteTask2) {
  457. // This test ensures that we don't
  458. // crash if a taskrunner is delete with a
  459. // task that has never been started.
  460. DeleteTestTaskRunner task_runner;
  461. HappyTask* happy_task = new HappyTask(&task_runner);
  462. happy_task->Start();
  463. // Do not start the task.
  464. // Note: this leaks memory, so don't do this.
  465. // Instead, always run your tasks or delete them.
  466. new HappyTask(happy_task);
  467. // run the unblocked tasks
  468. task_runner.RunTasks();
  469. }
  470. } // namespace talk_base