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.

exponential_backoff_strategy.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2012 Mathieu Turcotte
  3. * Licensed under the MIT license.
  4. */
  5. var sinon = require('sinon');
  6. var ExponentialBackoffStrategy = require('../lib/strategy/exponential');
  7. exports["ExponentialBackoffStrategy"] = {
  8. "backoff delays should follow an exponential sequence": function(test) {
  9. var strategy = new ExponentialBackoffStrategy({
  10. initialDelay: 10,
  11. maxDelay: 1000
  12. });
  13. // Exponential sequence: x[i] = x[i-1] * 2.
  14. var expectedDelays = [10, 20, 40, 80, 160, 320, 640, 1000, 1000];
  15. var actualDelays = expectedDelays.map(function () {
  16. return strategy.next();
  17. });
  18. test.deepEqual(expectedDelays, actualDelays,
  19. 'Generated delays should follow an exponential sequence.');
  20. test.done();
  21. },
  22. "backoff delay factor should be configurable": function (test) {
  23. var strategy = new ExponentialBackoffStrategy({
  24. initialDelay: 10,
  25. maxDelay: 270,
  26. factor: 3
  27. });
  28. // Exponential sequence: x[i] = x[i-1] * 3.
  29. var expectedDelays = [10, 30, 90, 270, 270];
  30. var actualDelays = expectedDelays.map(function () {
  31. return strategy.next();
  32. });
  33. test.deepEqual(expectedDelays, actualDelays,
  34. 'Generated delays should follow a configurable exponential sequence.');
  35. test.done();
  36. },
  37. "backoff delays should restart from the initial delay after reset": function(test) {
  38. var strategy = new ExponentialBackoffStrategy({
  39. initialDelay: 10,
  40. maxDelay: 1000
  41. });
  42. strategy.next();
  43. strategy.reset();
  44. var backoffDelay = strategy.next();
  45. test.equals(backoffDelay, 10,
  46. 'Strategy should return the initial delay after reset.');
  47. test.done();
  48. }
  49. };