This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.js
154 lines (122 loc) · 3.15 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
var Module = require("module");
var globalRequire = Module.prototype.require,
comb = require("comb");
function _require(name) {
if (name === "amqp") {
return AmqpStub();
} else {
return globalRequire.apply(this, arguments);
}
}
function Stub(proto) {
var stats = {};
function addCall(name, cb) {
return function () {
if (name in stats) {
stats[name].push(comb(arguments).toArray());
} else {
stats[name] = [comb(arguments).toArray()];
}
return cb.apply(this, arguments);
}
}
for (var key in proto) {
proto[key] = addCall(key, proto[key]);
}
return comb.merge({
reset: function reset() {
stats = {};
},
calledWith: function (name, args) {
return stats[name].some(function (called) {
return JSON.stringify(called) === JSON.stringify(args);
});
},
getCallCount: function (name) {
return (stats[name] || []).length;
}
}, proto);
}
var queueStub;
var QueueStub = function (name) {
if (!queueStub) {
queueStub = Stub({
bind: function () {
return 1;
},
subscribe: function () {
},
shift: function () {
}
});
}
return queueStub;
};
var exchangeStub;
var ExchangeStub = function (name) {
if (!exchangeStub) {
exchangeStub = Stub({
publish: function () {
},
destroy: function () {
}
})
}
return exchangeStub;
};
var connectionStub;
var ConnectionStub = function () {
if (!connectionStub) {
connectionStub = Stub({
on: function (event, cb) {
if (event === "ready") {
cb();
}
},
once: function (event, cb) {
if (event === "ready") {
cb();
}
},
removeListener: function (event, cb) {
},
publish: function () {
},
queue: function queue(name, opts, cb) {
if ("function" === typeof opts) {
cb = opts;
}
return cb(QueueStub(name));
},
exchange: function exchange(name, opts, cb) {
if ("function" === typeof opts) {
cb = opts;
}
return cb(ExchangeStub(name));
}
});
}
return connectionStub;
};
var amqpStub;
var AmqpStub = function () {
if (!amqpStub) {
amqpStub = Stub({
createConnection: function () {
return ConnectionStub();
}
});
}
return amqpStub;
};
exports.amqp = AmqpStub();
exports.connection = ConnectionStub();
exports.queue = QueueStub();
exports.exchange = ExchangeStub();
exports.reset = function () {
AmqpStub().reset();
ConnectionStub().reset();
QueueStub().reset();
ExchangeStub().reset();
}
Module.prototype.require = _require;