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