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.

fibonacci_backoff_strategy.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) 2012 Mathieu Turcotte
  3. * Licensed under the MIT license.
  4. */
  5. var sinon = require('sinon');
  6. var FibonacciBackoffStrategy = require('../lib/strategy/fibonacci');
  7. exports["FibonacciBackoffStrategy"] = {
  8. setUp: function(callback) {
  9. this.strategy = new FibonacciBackoffStrategy({
  10. initialDelay: 10,
  11. maxDelay: 1000
  12. });
  13. callback();
  14. },
  15. "backoff delays should follow a Fibonacci sequence": function(test) {
  16. // Fibonacci sequence: x[i] = x[i-1] + x[i-2].
  17. var expectedDelays = [10, 10, 20, 30, 50, 80, 130, 210, 340, 550, 890, 1000];
  18. var actualDelays = [];
  19. for (var i = 0; i < expectedDelays.length; i++) {
  20. actualDelays.push(this.strategy.next());
  21. }
  22. test.deepEqual(expectedDelays, actualDelays,
  23. 'Generated delays should follow a Fibonacci sequence.');
  24. test.done();
  25. },
  26. "backoff delays should restart from the initial delay after reset": function(test) {
  27. var strategy = new FibonacciBackoffStrategy({
  28. initialDelay: 10,
  29. maxDelay: 1000
  30. });
  31. strategy.next();
  32. strategy.reset();
  33. var backoffDelay = strategy.next();
  34. test.equals(backoffDelay, 10,
  35. 'Strategy should return the initial delay after reset.');
  36. test.done();
  37. }
  38. };