runloop.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (function () {
  2. 'use strict';
  3. QUnit.module("RunLoop");
  4. var service = getMisagoService('runloop');
  5. QUnit.test("run", function(assert) {
  6. var container = {timesLeft: 5};
  7. var runloop = service.factory(container);
  8. var done = assert.async();
  9. runloop.run(function(_) {
  10. assert.ok(_.timesLeft, "#" + (6 - _.timesLeft) + ": runloop is called");
  11. _.timesLeft -= 1;
  12. if (_.timesLeft === 0) {
  13. assert.equal(runloop._intervals['test-loop'], null,
  14. "test loop was stopped");
  15. done();
  16. return false;
  17. } else if (_.timesLeft < 0) {
  18. assert.ok(false, 'runloop was not finished by false from function');
  19. }
  20. }, 'test-loop', 100);
  21. });
  22. QUnit.test("runOnce", function(assert) {
  23. var container = {};
  24. var runloop = service.factory(container);
  25. var ranOnce = assert.async();
  26. runloop.runOnce(function(_) {
  27. assert.equal(runloop._intervals['test-run-once'], null,
  28. "runloop item name was removed from intervals");
  29. assert.equal(container, _, "test task was ran with container");
  30. ranOnce();
  31. }, 'test-run-once', 80);
  32. assert.ok(runloop._intervals['test-run-once'],
  33. "runloop item name was added to intervals");
  34. var ranOnceOther = assert.async();
  35. runloop.runOnce(function(_) {
  36. assert.equal(runloop._intervals['test-run-once-other'], null,
  37. "runloop item name was removed from intervals");
  38. assert.equal(container, _, "other test task was ran with container");
  39. ranOnceOther();
  40. }, 'test-run-once-other', 100);
  41. assert.ok(runloop._intervals['test-run-once-other'],
  42. "runloop item name was added to intervals");
  43. });
  44. QUnit.test("stop", function(assert) {
  45. var container = {};
  46. var runloop = service.factory(container);
  47. runloop.run(function() {
  48. assert.ok(false, "runloop should be canceled before first run");
  49. return false;
  50. }, 'canceled-loop', 500);
  51. runloop.stop('canceled-loop');
  52. assert.equal(runloop._intervals['canceled-loop'], null,
  53. "stop canceled named loop");
  54. runloop.run(function() {
  55. assert.ok(false, "runloop should be canceled before first run");
  56. return false;
  57. }, 'other-canceled-loop', 500);
  58. runloop.stop();
  59. assert.equal(runloop._intervals['other-canceled-loop'], null,
  60. "stop canceled all loops");
  61. });
  62. }());