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.

test.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 'use strict';
  2. var expect = require('expect.js');
  3. var promiseRetry = require('../');
  4. var promiseDelay = require('sleep-promise');
  5. describe('promise-retry', function () {
  6. it('should call fn again if retry was called', function () {
  7. var count = 0;
  8. return promiseRetry(function (retry) {
  9. count += 1;
  10. return promiseDelay(10)
  11. .then(function () {
  12. if (count <= 2) {
  13. retry(new Error('foo'));
  14. }
  15. return 'final';
  16. });
  17. }, { factor: 1 })
  18. .then(function (value) {
  19. expect(value).to.be('final');
  20. expect(count).to.be(3);
  21. }, function () {
  22. throw new Error('should not fail');
  23. });
  24. });
  25. it('should call fn with the attempt number', function () {
  26. var count = 0;
  27. return promiseRetry(function (retry, number) {
  28. count += 1;
  29. expect(count).to.equal(number);
  30. return promiseDelay(10)
  31. .then(function () {
  32. if (count <= 2) {
  33. retry(new Error('foo'));
  34. }
  35. return 'final';
  36. });
  37. }, { factor: 1 })
  38. .then(function (value) {
  39. expect(value).to.be('final');
  40. expect(count).to.be(3);
  41. }, function () {
  42. throw new Error('should not fail');
  43. });
  44. });
  45. it('should not retry on fulfillment if retry was not called', function () {
  46. var count = 0;
  47. return promiseRetry(function () {
  48. count += 1;
  49. return promiseDelay(10)
  50. .then(function () {
  51. return 'final';
  52. });
  53. })
  54. .then(function (value) {
  55. expect(value).to.be('final');
  56. expect(count).to.be(1);
  57. }, function () {
  58. throw new Error('should not fail');
  59. });
  60. });
  61. it('should not retry on rejection if retry was not called', function () {
  62. var count = 0;
  63. return promiseRetry(function () {
  64. count += 1;
  65. return promiseDelay(10)
  66. .then(function () {
  67. throw new Error('foo');
  68. });
  69. })
  70. .then(function () {
  71. throw new Error('should not succeed');
  72. }, function (err) {
  73. expect(err.message).to.be('foo');
  74. expect(count).to.be(1);
  75. });
  76. });
  77. it('should not retry on rejection if nr of retries is 0', function () {
  78. var count = 0;
  79. return promiseRetry(function (retry) {
  80. count += 1;
  81. return promiseDelay(10)
  82. .then(function () {
  83. throw new Error('foo');
  84. })
  85. .catch(retry);
  86. }, { retries : 0 })
  87. .then(function () {
  88. throw new Error('should not succeed');
  89. }, function (err) {
  90. expect(err.message).to.be('foo');
  91. expect(count).to.be(1);
  92. });
  93. });
  94. it('should reject the promise if the retries were exceeded', function () {
  95. var count = 0;
  96. return promiseRetry(function (retry) {
  97. count += 1;
  98. return promiseDelay(10)
  99. .then(function () {
  100. throw new Error('foo');
  101. })
  102. .catch(retry);
  103. }, { retries: 2, factor: 1 })
  104. .then(function () {
  105. throw new Error('should not succeed');
  106. }, function (err) {
  107. expect(err.message).to.be('foo');
  108. expect(count).to.be(3);
  109. });
  110. });
  111. it('should pass options to the underlying retry module', function () {
  112. var count = 0;
  113. return promiseRetry(function (retry) {
  114. return promiseDelay(10)
  115. .then(function () {
  116. if (count < 2) {
  117. count += 1;
  118. retry(new Error('foo'));
  119. }
  120. return 'final';
  121. });
  122. }, { retries: 1, factor: 1 })
  123. .then(function () {
  124. throw new Error('should not succeed');
  125. }, function (err) {
  126. expect(err.message).to.be('foo');
  127. });
  128. });
  129. it('should convert direct fulfillments into promises', function () {
  130. return promiseRetry(function () {
  131. return 'final';
  132. }, { factor: 1 })
  133. .then(function (value) {
  134. expect(value).to.be('final');
  135. }, function () {
  136. throw new Error('should not fail');
  137. });
  138. });
  139. it('should convert direct rejections into promises', function () {
  140. promiseRetry(function () {
  141. throw new Error('foo');
  142. }, { retries: 1, factor: 1 })
  143. .then(function () {
  144. throw new Error('should not succeed');
  145. }, function (err) {
  146. expect(err.message).to.be('foo');
  147. });
  148. });
  149. it('should not crash on undefined rejections', function () {
  150. return promiseRetry(function () {
  151. throw undefined;
  152. }, { retries: 1, factor: 1 })
  153. .then(function () {
  154. throw new Error('should not succeed');
  155. }, function (err) {
  156. expect(err).to.be(undefined);
  157. })
  158. .then(function () {
  159. return promiseRetry(function (retry) {
  160. retry();
  161. }, { retries: 1, factor: 1 });
  162. })
  163. .then(function () {
  164. throw new Error('should not succeed');
  165. }, function (err) {
  166. expect(err).to.be(undefined);
  167. });
  168. });
  169. it('should retry if retry() was called with undefined', function () {
  170. var count = 0;
  171. return promiseRetry(function (retry) {
  172. count += 1;
  173. return promiseDelay(10)
  174. .then(function () {
  175. if (count <= 2) {
  176. retry();
  177. }
  178. return 'final';
  179. });
  180. }, { factor: 1 })
  181. .then(function (value) {
  182. expect(value).to.be('final');
  183. expect(count).to.be(3);
  184. }, function () {
  185. throw new Error('should not fail');
  186. });
  187. });
  188. it('should work with several retries in the same chain', function () {
  189. var count = 0;
  190. return promiseRetry(function (retry) {
  191. count += 1;
  192. return promiseDelay(10)
  193. .then(function () {
  194. retry(new Error('foo'));
  195. })
  196. .catch(function (err) {
  197. retry(err);
  198. });
  199. }, { retries: 1, factor: 1 })
  200. .then(function () {
  201. throw new Error('should not succeed');
  202. }, function (err) {
  203. expect(err.message).to.be('foo');
  204. expect(count).to.be(2);
  205. });
  206. });
  207. it('should allow options to be passed first', function () {
  208. var count = 0;
  209. return promiseRetry({ factor: 1 }, function (retry) {
  210. count += 1;
  211. return promiseDelay(10)
  212. .then(function () {
  213. if (count <= 2) {
  214. retry(new Error('foo'));
  215. }
  216. return 'final';
  217. });
  218. }).then(function (value) {
  219. expect(value).to.be('final');
  220. expect(count).to.be(3);
  221. }, function () {
  222. throw new Error('should not fail');
  223. });
  224. });
  225. });