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.

101 lines
2.6 KiB

4 years ago
  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 getLib() {
  6. return {
  7. fn1: function() {},
  8. fn2: function() {},
  9. fn3: function() {}
  10. };
  11. }
  12. (function wrapAll() {
  13. var lib = getLib();
  14. retry.wrap(lib);
  15. assert.equal(lib.fn1.name, 'bound retryWrapper');
  16. assert.equal(lib.fn2.name, 'bound retryWrapper');
  17. assert.equal(lib.fn3.name, 'bound retryWrapper');
  18. }());
  19. (function wrapAllPassOptions() {
  20. var lib = getLib();
  21. retry.wrap(lib, {retries: 2});
  22. assert.equal(lib.fn1.name, 'bound retryWrapper');
  23. assert.equal(lib.fn2.name, 'bound retryWrapper');
  24. assert.equal(lib.fn3.name, 'bound retryWrapper');
  25. assert.equal(lib.fn1.options.retries, 2);
  26. assert.equal(lib.fn2.options.retries, 2);
  27. assert.equal(lib.fn3.options.retries, 2);
  28. }());
  29. (function wrapDefined() {
  30. var lib = getLib();
  31. retry.wrap(lib, ['fn2', 'fn3']);
  32. assert.notEqual(lib.fn1.name, 'bound retryWrapper');
  33. assert.equal(lib.fn2.name, 'bound retryWrapper');
  34. assert.equal(lib.fn3.name, 'bound retryWrapper');
  35. }());
  36. (function wrapDefinedAndPassOptions() {
  37. var lib = getLib();
  38. retry.wrap(lib, {retries: 2}, ['fn2', 'fn3']);
  39. assert.notEqual(lib.fn1.name, 'bound retryWrapper');
  40. assert.equal(lib.fn2.name, 'bound retryWrapper');
  41. assert.equal(lib.fn3.name, 'bound retryWrapper');
  42. assert.equal(lib.fn2.options.retries, 2);
  43. assert.equal(lib.fn3.options.retries, 2);
  44. }());
  45. (function runWrappedWithoutError() {
  46. var callbackCalled;
  47. var lib = {method: function(a, b, callback) {
  48. assert.equal(a, 1);
  49. assert.equal(b, 2);
  50. assert.equal(typeof callback, 'function');
  51. callback();
  52. }};
  53. retry.wrap(lib);
  54. lib.method(1, 2, function() {
  55. callbackCalled = true;
  56. });
  57. assert.ok(callbackCalled);
  58. }());
  59. (function runWrappedSeveralWithoutError() {
  60. var callbacksCalled = 0;
  61. var lib = {
  62. fn1: function (a, callback) {
  63. assert.equal(a, 1);
  64. assert.equal(typeof callback, 'function');
  65. callback();
  66. },
  67. fn2: function (a, callback) {
  68. assert.equal(a, 2);
  69. assert.equal(typeof callback, 'function');
  70. callback();
  71. }
  72. };
  73. retry.wrap(lib, {}, ['fn1', 'fn2']);
  74. lib.fn1(1, function() {
  75. callbacksCalled++;
  76. });
  77. lib.fn2(2, function() {
  78. callbacksCalled++;
  79. });
  80. assert.equal(callbacksCalled, 2);
  81. }());
  82. (function runWrappedWithError() {
  83. var callbackCalled;
  84. var lib = {method: function(callback) {
  85. callback(new Error('Some error'));
  86. }};
  87. retry.wrap(lib, {retries: 1});
  88. lib.method(function(err) {
  89. callbackCalled = true;
  90. assert.ok(err instanceof Error);
  91. });
  92. assert.ok(!callbackCalled);
  93. }());