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_strategy.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2012 Mathieu Turcotte
  3. * Licensed under the MIT license.
  4. */
  5. var sinon = require('sinon');
  6. var util = require('util');
  7. var BackoffStrategy = require('../lib/strategy/strategy');
  8. function SampleBackoffStrategy(options) {
  9. BackoffStrategy.call(this, options);
  10. }
  11. util.inherits(SampleBackoffStrategy, BackoffStrategy);
  12. SampleBackoffStrategy.prototype.next_ = function() {
  13. return this.getInitialDelay();
  14. };
  15. SampleBackoffStrategy.prototype.reset_ = function() {};
  16. exports["BackoffStrategy"] = {
  17. setUp: function(callback) {
  18. this.random = sinon.stub(Math, 'random');
  19. callback();
  20. },
  21. tearDown: function(callback) {
  22. this.random.restore();
  23. callback();
  24. },
  25. "the randomisation factor should be between 0 and 1": function(test) {
  26. test.throws(function() {
  27. new BackoffStrategy({
  28. randomisationFactor: -0.1
  29. });
  30. });
  31. test.throws(function() {
  32. new BackoffStrategy({
  33. randomisationFactor: 1.1
  34. });
  35. });
  36. test.doesNotThrow(function() {
  37. new BackoffStrategy({
  38. randomisationFactor: 0.5
  39. });
  40. });
  41. test.done();
  42. },
  43. "the raw delay should be randomized based on the randomisation factor": function(test) {
  44. var strategy = new SampleBackoffStrategy({
  45. randomisationFactor: 0.5,
  46. initialDelay: 1000
  47. });
  48. this.random.returns(0.5);
  49. var backoffDelay = strategy.next();
  50. test.equals(backoffDelay, 1000 + (1000 * 0.5 * 0.5));
  51. test.done();
  52. },
  53. "the initial backoff delay should be greater than 0": function(test) {
  54. test.throws(function() {
  55. new BackoffStrategy({
  56. initialDelay: -1
  57. });
  58. });
  59. test.throws(function() {
  60. new BackoffStrategy({
  61. initialDelay: 0
  62. });
  63. });
  64. test.doesNotThrow(function() {
  65. new BackoffStrategy({
  66. initialDelay: 1
  67. });
  68. });
  69. test.done();
  70. },
  71. "the maximal backoff delay should be greater than 0": function(test) {
  72. test.throws(function() {
  73. new BackoffStrategy({
  74. maxDelay: -1
  75. });
  76. });
  77. test.throws(function() {
  78. new BackoffStrategy({
  79. maxDelay: 0
  80. });
  81. });
  82. test.done();
  83. },
  84. "the maximal backoff delay should be greater than the initial backoff delay": function(test) {
  85. test.throws(function() {
  86. new BackoffStrategy({
  87. initialDelay: 10,
  88. maxDelay: 10
  89. });
  90. });
  91. test.doesNotThrow(function() {
  92. new BackoffStrategy({
  93. initialDelay: 10,
  94. maxDelay: 11
  95. });
  96. });
  97. test.done();
  98. }
  99. };