Ohm-Management - Projektarbeit B-ME
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.

backoff.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2012 Mathieu Turcotte
  3. * Licensed under the MIT license.
  4. */
  5. var sinon = require('sinon');
  6. var Backoff = require('../lib/backoff');
  7. var BackoffStrategy = require('../lib/strategy/strategy');
  8. exports["Backoff"] = {
  9. setUp: function(callback) {
  10. this.backoffStrategy = sinon.stub(new BackoffStrategy());
  11. this.backoff = new Backoff(this.backoffStrategy);
  12. this.clock = sinon.useFakeTimers();
  13. this.spy = new sinon.spy();
  14. callback();
  15. },
  16. tearDown: function(callback) {
  17. this.clock.restore();
  18. callback();
  19. },
  20. "the backoff event should be emitted when backoff starts": function(test) {
  21. this.backoffStrategy.next.returns(10);
  22. this.backoff.on('backoff', this.spy);
  23. this.backoff.backoff();
  24. test.ok(this.spy.calledOnce,
  25. 'Backoff event should be emitted when backoff starts.');
  26. test.done();
  27. },
  28. "the ready event should be emitted on backoff completion": function(test) {
  29. this.backoffStrategy.next.returns(10);
  30. this.backoff.on('ready', this.spy);
  31. this.backoff.backoff();
  32. this.clock.tick(10);
  33. test.ok(this.spy.calledOnce,
  34. 'Ready event should be emitted when backoff ends.');
  35. test.done();
  36. },
  37. "the backoff event should be passed the backoff delay": function(test) {
  38. this.backoffStrategy.next.returns(989);
  39. this.backoff.on('backoff', this.spy);
  40. this.backoff.backoff();
  41. test.equal(this.spy.getCall(0).args[1], 989, 'Backoff event should ' +
  42. 'carry the backoff delay as its second argument.');
  43. test.done();
  44. },
  45. "the ready event should be passed the backoff delay": function(test) {
  46. this.backoffStrategy.next.returns(989);
  47. this.backoff.on('ready', this.spy);
  48. this.backoff.backoff();
  49. this.clock.tick(989);
  50. test.equal(this.spy.getCall(0).args[1], 989, 'Ready event should ' +
  51. 'carry the backoff delay as its second argument.');
  52. test.done();
  53. },
  54. "the fail event should be emitted when backoff limit is reached": function(test) {
  55. var err = new Error('Fail');
  56. this.backoffStrategy.next.returns(10);
  57. this.backoff.on('fail', this.spy);
  58. this.backoff.failAfter(2);
  59. // Consume first 2 backoffs.
  60. for (var i = 0; i < 2; i++) {
  61. this.backoff.backoff();
  62. this.clock.tick(10);
  63. }
  64. // Failure should occur on the third call, and not before.
  65. test.ok(!this.spy.calledOnce, 'Fail event shouldn\'t have been emitted.');
  66. this.backoff.backoff(err);
  67. test.ok(this.spy.calledOnce, 'Fail event should have been emitted.');
  68. test.equal(this.spy.getCall(0).args[0], err, 'Error should be passed');
  69. test.done();
  70. },
  71. "calling backoff while a backoff is in progress should throw an error": function(test) {
  72. this.backoffStrategy.next.returns(10);
  73. var backoff = this.backoff;
  74. backoff.backoff();
  75. test.throws(function() {
  76. backoff.backoff();
  77. }, /in progress/);
  78. test.done();
  79. },
  80. "backoff limit should be greater than 0": function(test) {
  81. var backoff = this.backoff;
  82. test.throws(function() {
  83. backoff.failAfter(0);
  84. }, /greater than 0 but got 0/);
  85. test.done();
  86. },
  87. "reset should cancel any backoff in progress": function(test) {
  88. this.backoffStrategy.next.returns(10);
  89. this.backoff.on('ready', this.spy);
  90. this.backoff.backoff();
  91. this.backoff.reset();
  92. this.clock.tick(100); // 'ready' should not be emitted.
  93. test.equals(this.spy.callCount, 0, 'Reset should have aborted the backoff.');
  94. test.done();
  95. },
  96. "reset should reset the backoff strategy": function(test) {
  97. this.backoff.reset();
  98. test.ok(this.backoffStrategy.reset.calledOnce,
  99. 'The backoff strategy should have been resetted.');
  100. test.done();
  101. },
  102. "backoff should be reset after fail": function(test) {
  103. this.backoffStrategy.next.returns(10);
  104. this.backoff.failAfter(1);
  105. this.backoff.backoff();
  106. this.clock.tick(10);
  107. this.backoff.backoff();
  108. test.ok(this.backoffStrategy.reset.calledOnce,
  109. 'Backoff should have been resetted after failure.');
  110. test.done();
  111. },
  112. "the backoff number should increase from 0 to N - 1": function(test) {
  113. this.backoffStrategy.next.returns(10);
  114. this.backoff.on('backoff', this.spy);
  115. var expectedNumbers = [0, 1, 2, 3, 4];
  116. var actualNumbers = [];
  117. for (var i = 0; i < expectedNumbers.length; i++) {
  118. this.backoff.backoff();
  119. this.clock.tick(10);
  120. actualNumbers.push(this.spy.getCall(i).args[0]);
  121. }
  122. test.deepEqual(expectedNumbers, actualNumbers,
  123. 'Backoff number should increase from 0 to N - 1.');
  124. test.done();
  125. }
  126. };