test-retry-operation.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. var common = require('../common');
  2. var assert = common.assert;
  3. var fake = common.fake.create();
  4. var retry = require(common.dir.lib + '/retry');
  5. (function testErrors() {
  6. var operation = retry.operation();
  7. var error = new Error('some error');
  8. var error2 = new Error('some other error');
  9. operation._errors.push(error);
  10. operation._errors.push(error2);
  11. assert.deepEqual(operation.errors(), [error, error2]);
  12. })();
  13. (function testMainErrorReturnsMostFrequentError() {
  14. var operation = retry.operation();
  15. var error = new Error('some error');
  16. var error2 = new Error('some other error');
  17. operation._errors.push(error);
  18. operation._errors.push(error2);
  19. operation._errors.push(error);
  20. assert.strictEqual(operation.mainError(), error);
  21. })();
  22. (function testMainErrorReturnsLastErrorOnEqualCount() {
  23. var operation = retry.operation();
  24. var error = new Error('some error');
  25. var error2 = new Error('some other error');
  26. operation._errors.push(error);
  27. operation._errors.push(error2);
  28. assert.strictEqual(operation.mainError(), error2);
  29. })();
  30. (function testAttempt() {
  31. var operation = retry.operation();
  32. var fn = new Function();
  33. var timeoutOpts = {
  34. timeout: 1,
  35. cb: function() {}
  36. };
  37. operation.attempt(fn, timeoutOpts);
  38. assert.strictEqual(fn, operation._fn);
  39. assert.strictEqual(timeoutOpts.timeout, operation._operationTimeout);
  40. assert.strictEqual(timeoutOpts.cb, operation._operationTimeoutCb);
  41. })();
  42. (function testRetry() {
  43. var times = 3;
  44. var error = new Error('some error');
  45. var operation = retry.operation([1, 2, 3]);
  46. var attempts = 0;
  47. var finalCallback = fake.callback('finalCallback');
  48. fake.expectAnytime(finalCallback);
  49. var fn = function() {
  50. operation.attempt(function(currentAttempt) {
  51. attempts++;
  52. assert.equal(currentAttempt, attempts);
  53. if (operation.retry(error)) {
  54. return;
  55. }
  56. assert.strictEqual(attempts, 4);
  57. assert.strictEqual(operation.attempts(), attempts);
  58. assert.strictEqual(operation.mainError(), error);
  59. finalCallback();
  60. });
  61. };
  62. fn();
  63. })();
  64. (function testRetryForever() {
  65. var error = new Error('some error');
  66. var operation = retry.operation({ retries: 3, forever: true });
  67. var attempts = 0;
  68. var finalCallback = fake.callback('finalCallback');
  69. fake.expectAnytime(finalCallback);
  70. var fn = function() {
  71. operation.attempt(function(currentAttempt) {
  72. attempts++;
  73. assert.equal(currentAttempt, attempts);
  74. if (attempts !== 6 && operation.retry(error)) {
  75. return;
  76. }
  77. assert.strictEqual(attempts, 6);
  78. assert.strictEqual(operation.attempts(), attempts);
  79. assert.strictEqual(operation.mainError(), error);
  80. finalCallback();
  81. });
  82. };
  83. fn();
  84. })();
  85. (function testRetryForeverNoRetries() {
  86. var error = new Error('some error');
  87. var delay = 50
  88. var operation = retry.operation({
  89. retries: null,
  90. forever: true,
  91. minTimeout: delay,
  92. maxTimeout: delay
  93. });
  94. var attempts = 0;
  95. var startTime = new Date().getTime();
  96. var finalCallback = fake.callback('finalCallback');
  97. fake.expectAnytime(finalCallback);
  98. var fn = function() {
  99. operation.attempt(function(currentAttempt) {
  100. attempts++;
  101. assert.equal(currentAttempt, attempts);
  102. if (attempts !== 4 && operation.retry(error)) {
  103. return;
  104. }
  105. var endTime = new Date().getTime();
  106. var minTime = startTime + (delay * 3);
  107. var maxTime = minTime + 20 // add a little headroom for code execution time
  108. assert(endTime > minTime)
  109. assert(endTime < maxTime)
  110. assert.strictEqual(attempts, 4);
  111. assert.strictEqual(operation.attempts(), attempts);
  112. assert.strictEqual(operation.mainError(), error);
  113. finalCallback();
  114. });
  115. };
  116. fn();
  117. })();
  118. (function testStop() {
  119. var error = new Error('some error');
  120. var operation = retry.operation([1, 2, 3]);
  121. var attempts = 0;
  122. var finalCallback = fake.callback('finalCallback');
  123. fake.expectAnytime(finalCallback);
  124. var fn = function() {
  125. operation.attempt(function(currentAttempt) {
  126. attempts++;
  127. assert.equal(currentAttempt, attempts);
  128. if (attempts === 2) {
  129. operation.stop();
  130. assert.strictEqual(attempts, 2);
  131. assert.strictEqual(operation.attempts(), attempts);
  132. assert.strictEqual(operation.mainError(), error);
  133. finalCallback();
  134. }
  135. if (operation.retry(error)) {
  136. return;
  137. }
  138. });
  139. };
  140. fn();
  141. })();