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