amqplib 0.4.2 → 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +88 -0
- package/Makefile +2 -1
- package/README.md +4 -4
- package/callback_api.js +1 -0
- package/channel_api.js +7 -6
- package/examples/headers.js +2 -2
- package/examples/receive_generator.js +39 -0
- package/examples/send_generators.js +42 -0
- package/examples/ssl.js +1 -1
- package/examples/tutorials/README.md +1 -1
- package/examples/tutorials/callback_api/emit_log.js +1 -1
- package/examples/tutorials/callback_api/emit_log_direct.js +1 -1
- package/examples/tutorials/callback_api/emit_log_topic.js +1 -1
- package/examples/tutorials/callback_api/new_task.js +1 -1
- package/examples/tutorials/callback_api/rpc_client.js +1 -1
- package/examples/tutorials/callback_api/rpc_server.js +1 -1
- package/examples/tutorials/callback_api/send.js +1 -1
- package/examples/tutorials/emit_log.js +4 -5
- package/examples/tutorials/emit_log_direct.js +4 -5
- package/examples/tutorials/emit_log_topic.js +4 -5
- package/examples/tutorials/new_task.js +5 -6
- package/examples/tutorials/package.json +2 -3
- package/examples/tutorials/receive.js +4 -4
- package/examples/tutorials/receive_logs.js +1 -1
- package/examples/tutorials/receive_logs_direct.js +2 -2
- package/examples/tutorials/receive_logs_topic.js +6 -6
- package/examples/tutorials/rpc_client.js +25 -26
- package/examples/tutorials/rpc_server.js +2 -2
- package/examples/tutorials/send.js +4 -5
- package/examples/tutorials/worker.js +1 -1
- package/examples/waitForConfirms.js +2 -2
- package/lib/api_args.js +2 -0
- package/lib/callback_model.js +11 -10
- package/lib/channel.js +24 -12
- package/lib/channel_model.js +57 -65
- package/lib/codec.js +64 -38
- package/lib/connect.js +26 -15
- package/lib/connection.js +6 -4
- package/lib/credentials.js +20 -3
- package/lib/defs.js +203 -75
- package/lib/frame.js +2 -1
- package/lib/heartbeat.js +1 -1
- package/package.json +10 -8
- package/test/callback_api.js +6 -5
- package/test/channel.js +125 -107
- package/test/channel_api.js +125 -136
- package/test/codec.js +48 -9
- package/test/connect.js +88 -3
- package/test/connection.js +57 -56
- package/test/data.js +32 -9
- package/test/frame.js +4 -4
- package/test/mux.js +10 -6
- package/test/util.js +35 -21
- package/.npmignore +0 -9
package/test/connect.js
CHANGED
|
@@ -1,21 +1,67 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var connect = require('../lib/connect').connect;
|
|
4
|
+
var Buffer = require('safe-buffer').Buffer
|
|
5
|
+
var credentialsFromUrl = require('../lib/connect').credentialsFromUrl;
|
|
4
6
|
var assert = require('assert');
|
|
5
7
|
var util = require('./util');
|
|
6
8
|
var net = require('net');
|
|
7
9
|
var fail = util.fail, succeed = util.succeed,
|
|
8
|
-
kCallback = util.kCallback
|
|
10
|
+
kCallback = util.kCallback,
|
|
11
|
+
succeedIfAttributeEquals = util.succeedIfAttributeEquals;
|
|
12
|
+
var format = require('util').format;
|
|
9
13
|
|
|
10
14
|
var URL = process.env.URL || 'amqp://localhost';
|
|
11
15
|
|
|
16
|
+
var urlparse = require('url-parse');
|
|
17
|
+
|
|
18
|
+
suite("Credentials", function() {
|
|
19
|
+
|
|
20
|
+
function checkCreds(creds, user, pass, done) {
|
|
21
|
+
if (creds.mechanism != 'PLAIN') {
|
|
22
|
+
return done('expected mechanism PLAIN');
|
|
23
|
+
}
|
|
24
|
+
if (creds.username != user || creds.password != pass) {
|
|
25
|
+
return done(format("expected '%s', '%s'; got '%s', '%s'",
|
|
26
|
+
user, pass, creds.username, creds.password));
|
|
27
|
+
}
|
|
28
|
+
done();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
test("no creds", function(done) {
|
|
32
|
+
var parts = urlparse('amqp://localhost');
|
|
33
|
+
var creds = credentialsFromUrl(parts);
|
|
34
|
+
checkCreds(creds, 'guest', 'guest', done);
|
|
35
|
+
});
|
|
36
|
+
test("usual user:pass", function(done) {
|
|
37
|
+
var parts = urlparse('amqp://user:pass@localhost')
|
|
38
|
+
var creds = credentialsFromUrl(parts);
|
|
39
|
+
checkCreds(creds, 'user', 'pass', done);
|
|
40
|
+
});
|
|
41
|
+
test("missing user", function(done) {
|
|
42
|
+
var parts = urlparse('amqps://:password@localhost');
|
|
43
|
+
var creds = credentialsFromUrl(parts);
|
|
44
|
+
checkCreds(creds, '', 'password', done);
|
|
45
|
+
});
|
|
46
|
+
test("missing password", function(done) {
|
|
47
|
+
var parts = urlparse('amqps://username:@localhost');
|
|
48
|
+
var creds = credentialsFromUrl(parts);
|
|
49
|
+
checkCreds(creds, 'username', '', done);
|
|
50
|
+
});
|
|
51
|
+
test("escaped colons", function(done) {
|
|
52
|
+
var parts = urlparse('amqp://user%3Aname:pass%3Aword@localhost')
|
|
53
|
+
var creds = credentialsFromUrl(parts);
|
|
54
|
+
checkCreds(creds, 'user:name', 'pass:word', done);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
12
58
|
suite("Connect API", function() {
|
|
13
59
|
|
|
14
60
|
test("Connection refused", function(done) {
|
|
15
61
|
connect('amqp://localhost:23450', {},
|
|
16
62
|
kCallback(fail(done), succeed(done)));
|
|
17
63
|
});
|
|
18
|
-
|
|
64
|
+
|
|
19
65
|
// %% this ought to fail the promise, rather than throwing an error
|
|
20
66
|
test("bad URL", function() {
|
|
21
67
|
assert.throws(function() {
|
|
@@ -33,6 +79,33 @@ suite("Connect API", function() {
|
|
|
33
79
|
connect(u, {}, kCallback(fail(done), succeed(done)));
|
|
34
80
|
});
|
|
35
81
|
|
|
82
|
+
test("serverProperties", function(done) {
|
|
83
|
+
var url = require('url');
|
|
84
|
+
var parts = url.parse(URL, true);
|
|
85
|
+
var config = parts.query || {};
|
|
86
|
+
connect(config, {}, function(err, connection) {
|
|
87
|
+
if (err) { return done(err); }
|
|
88
|
+
assert.equal(connection.serverProperties.product, 'RabbitMQ');
|
|
89
|
+
done();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test("using custom heartbeat option", function(done) {
|
|
94
|
+
var url = require('url');
|
|
95
|
+
var parts = url.parse(URL, true);
|
|
96
|
+
var config = parts.query || {};
|
|
97
|
+
config.heartbeat = 20;
|
|
98
|
+
connect(config, {}, kCallback(succeedIfAttributeEquals('heartbeat', 20, done), fail(done)));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("wrongly typed heartbeat option", function(done) {
|
|
102
|
+
var url = require('url');
|
|
103
|
+
var parts = url.parse(URL, true);
|
|
104
|
+
var config = parts.query || {};
|
|
105
|
+
config.heartbeat = 'NOT A NUMBER';
|
|
106
|
+
connect(config, {}, kCallback(fail(done), succeed(done)));
|
|
107
|
+
});
|
|
108
|
+
|
|
36
109
|
test("using plain credentials", function(done) {
|
|
37
110
|
var url = require('url');
|
|
38
111
|
var parts = url.parse(URL, true);
|
|
@@ -45,10 +118,22 @@ suite("Connect API", function() {
|
|
|
45
118
|
kCallback(succeed(done), fail(done)));
|
|
46
119
|
});
|
|
47
120
|
|
|
121
|
+
test("using amqplain credentials", function(done) {
|
|
122
|
+
var url = require('url');
|
|
123
|
+
var parts = url.parse(URL, true);
|
|
124
|
+
var u = 'guest', p = 'guest';
|
|
125
|
+
if (parts.auth) {
|
|
126
|
+
var auth = parts.auth.split(":");
|
|
127
|
+
u = auth[0], p = auth[1];
|
|
128
|
+
}
|
|
129
|
+
connect(URL, {credentials: require('../lib/credentials').amqplain(u, p)},
|
|
130
|
+
kCallback(succeed(done), fail(done)));
|
|
131
|
+
});
|
|
132
|
+
|
|
48
133
|
test("using unsupported mechanism", function(done) {
|
|
49
134
|
var creds = {
|
|
50
135
|
mechanism: 'UNSUPPORTED',
|
|
51
|
-
response: function() { return
|
|
136
|
+
response: function() { return Buffer.from(''); }
|
|
52
137
|
};
|
|
53
138
|
connect(URL, {credentials: creds},
|
|
54
139
|
kCallback(fail(done), succeed(done)));
|
package/test/connection.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var assert = require('assert');
|
|
4
4
|
var defs = require('../lib/defs');
|
|
5
|
+
var Buffer = require('safe-buffer').Buffer;
|
|
5
6
|
var Connection = require('../lib/connection').Connection;
|
|
6
7
|
var HEARTBEAT = require('../lib/frame').HEARTBEAT;
|
|
7
8
|
var HB_BUF = require('../lib/frame').HEARTBEAT_BUF;
|
|
@@ -16,14 +17,14 @@ var OPEN_OPTS = {
|
|
|
16
17
|
// start-ok
|
|
17
18
|
'clientProperties': {},
|
|
18
19
|
'mechanism': 'PLAIN',
|
|
19
|
-
'response':
|
|
20
|
+
'response': Buffer.from(['', 'guest', 'guest'].join(String.fromCharCode(0))),
|
|
20
21
|
'locale': 'en_US',
|
|
21
|
-
|
|
22
|
+
|
|
22
23
|
// tune-ok
|
|
23
24
|
'channelMax': 0,
|
|
24
25
|
'frameMax': 0,
|
|
25
26
|
'heartbeat': 0,
|
|
26
|
-
|
|
27
|
+
|
|
27
28
|
// open
|
|
28
29
|
'virtualHost': '/',
|
|
29
30
|
'capabilities': '',
|
|
@@ -31,23 +32,23 @@ var OPEN_OPTS = {
|
|
|
31
32
|
};
|
|
32
33
|
module.exports.OPEN_OPTS = OPEN_OPTS;
|
|
33
34
|
|
|
34
|
-
function happy_open(send,
|
|
35
|
+
function happy_open(send, wait) {
|
|
35
36
|
// kick it off
|
|
36
37
|
send(defs.ConnectionStart,
|
|
37
38
|
{versionMajor: 0,
|
|
38
39
|
versionMinor: 9,
|
|
39
40
|
serverProperties: {},
|
|
40
|
-
mechanisms:
|
|
41
|
-
locales:
|
|
42
|
-
return
|
|
41
|
+
mechanisms: Buffer.from('PLAIN'),
|
|
42
|
+
locales: Buffer.from('en_US')});
|
|
43
|
+
return wait(defs.ConnectionStartOk)()
|
|
43
44
|
.then(function(f) {
|
|
44
45
|
send(defs.ConnectionTune,
|
|
45
46
|
{channelMax: 0,
|
|
46
47
|
heartbeat: 0,
|
|
47
48
|
frameMax: 0});
|
|
48
49
|
})
|
|
49
|
-
.then(
|
|
50
|
-
.then(
|
|
50
|
+
.then(wait(defs.ConnectionTuneOk))
|
|
51
|
+
.then(wait(defs.ConnectionOpen))
|
|
51
52
|
.then(function(f) {
|
|
52
53
|
send(defs.ConnectionOpenOk,
|
|
53
54
|
{knownHosts: ''});
|
|
@@ -65,11 +66,11 @@ function connectionTest(client, server) {
|
|
|
65
66
|
|
|
66
67
|
// NB only not a race here because the writes are synchronous
|
|
67
68
|
var protocolHeader = pair.server.read(8);
|
|
68
|
-
assert.deepEqual(
|
|
69
|
+
assert.deepEqual(Buffer.from("AMQP" + String.fromCharCode(0,0,9,1)),
|
|
69
70
|
protocolHeader);
|
|
70
71
|
|
|
71
|
-
var s = util.runServer(pair.server, function(send,
|
|
72
|
-
server(send,
|
|
72
|
+
var s = util.runServer(pair.server, function(send, wait) {
|
|
73
|
+
server(send, wait, bothDone, pair.server);
|
|
73
74
|
});
|
|
74
75
|
};
|
|
75
76
|
}
|
|
@@ -92,7 +93,7 @@ suite("Connection errors", function() {
|
|
|
92
93
|
var ss = util.socketPair();
|
|
93
94
|
var conn = new (require('../lib/connection').Connection)(ss.client);
|
|
94
95
|
ss.server.on('readable', function() {
|
|
95
|
-
ss.server.write(
|
|
96
|
+
ss.server.write(Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
|
|
96
97
|
});
|
|
97
98
|
conn.open({}, kCallback(fail(done), succeed(done)));
|
|
98
99
|
});
|
|
@@ -105,15 +106,15 @@ test("happy", connectionTest(
|
|
|
105
106
|
function(c, done) {
|
|
106
107
|
c.open(OPEN_OPTS, kCallback(succeed(done), fail(done)));
|
|
107
108
|
},
|
|
108
|
-
function(send,
|
|
109
|
-
happy_open(send,
|
|
109
|
+
function(send, wait, done) {
|
|
110
|
+
happy_open(send, wait).then(succeed(done), fail(done));
|
|
110
111
|
}));
|
|
111
112
|
|
|
112
113
|
test("wrong first frame", connectionTest(
|
|
113
114
|
function(c, done) {
|
|
114
115
|
c.open(OPEN_OPTS, kCallback(fail(done), succeed(done)));
|
|
115
116
|
},
|
|
116
|
-
function(send,
|
|
117
|
+
function(send, wait, done) {
|
|
117
118
|
// bad server! bad! whatever were you thinking?
|
|
118
119
|
completes(function() {
|
|
119
120
|
send(defs.ConnectionTune,
|
|
@@ -127,14 +128,14 @@ test("unexpected socket close", connectionTest(
|
|
|
127
128
|
function(c, done) {
|
|
128
129
|
c.open(OPEN_OPTS, kCallback(fail(done), succeed(done)));
|
|
129
130
|
},
|
|
130
|
-
function(send,
|
|
131
|
+
function(send, wait, done, socket) {
|
|
131
132
|
send(defs.ConnectionStart,
|
|
132
133
|
{versionMajor: 0,
|
|
133
134
|
versionMinor: 9,
|
|
134
135
|
serverProperties: {},
|
|
135
|
-
mechanisms:
|
|
136
|
-
locales:
|
|
137
|
-
return
|
|
136
|
+
mechanisms: Buffer.from('PLAIN'),
|
|
137
|
+
locales: Buffer.from('en_US')});
|
|
138
|
+
return wait(defs.ConnectionStartOk)()
|
|
138
139
|
.then(function() {
|
|
139
140
|
socket.end();
|
|
140
141
|
})
|
|
@@ -150,15 +151,15 @@ test("wrong frame on channel 0", connectionTest(
|
|
|
150
151
|
c.on('error', succeed(done));
|
|
151
152
|
c.open(OPEN_OPTS);
|
|
152
153
|
},
|
|
153
|
-
function(send,
|
|
154
|
-
happy_open(send,
|
|
154
|
+
function(send, wait, done) {
|
|
155
|
+
happy_open(send, wait)
|
|
155
156
|
.then(function() {
|
|
156
157
|
// there's actually nothing that would plausibly be sent to a
|
|
157
158
|
// just opened connection, so this is violating more than one
|
|
158
159
|
// rule. Nonetheless.
|
|
159
|
-
send(defs.ChannelOpenOk, {channelId:
|
|
160
|
+
send(defs.ChannelOpenOk, {channelId: Buffer.from('')}, 0);
|
|
160
161
|
})
|
|
161
|
-
.then(
|
|
162
|
+
.then(wait(defs.ConnectionClose))
|
|
162
163
|
.then(function(close) {
|
|
163
164
|
send(defs.ConnectionCloseOk, {}, 0);
|
|
164
165
|
}).then(succeed(done), fail(done));
|
|
@@ -169,15 +170,15 @@ test("unopened channel", connectionTest(
|
|
|
169
170
|
c.on('error', succeed(done));
|
|
170
171
|
c.open(OPEN_OPTS);
|
|
171
172
|
},
|
|
172
|
-
function(send,
|
|
173
|
-
happy_open(send,
|
|
173
|
+
function(send, wait, done) {
|
|
174
|
+
happy_open(send, wait)
|
|
174
175
|
.then(function() {
|
|
175
176
|
// there's actually nothing that would plausibly be sent to a
|
|
176
177
|
// just opened connection, so this is violating more than one
|
|
177
178
|
// rule. Nonetheless.
|
|
178
|
-
send(defs.ChannelOpenOk, {channelId:
|
|
179
|
+
send(defs.ChannelOpenOk, {channelId: Buffer.from('')}, 3);
|
|
179
180
|
})
|
|
180
|
-
.then(
|
|
181
|
+
.then(wait(defs.ConnectionClose))
|
|
181
182
|
.then(function(close) {
|
|
182
183
|
send(defs.ConnectionCloseOk, {}, 0);
|
|
183
184
|
}).then(succeed(done), fail(done));
|
|
@@ -192,9 +193,9 @@ test("unexpected socket close", connectionTest(
|
|
|
192
193
|
c.sendHeartbeat();
|
|
193
194
|
}, fail(errorAndClosed)));
|
|
194
195
|
},
|
|
195
|
-
function(send,
|
|
196
|
-
happy_open(send,
|
|
197
|
-
.then(
|
|
196
|
+
function(send, wait, done, socket) {
|
|
197
|
+
happy_open(send, wait)
|
|
198
|
+
.then(wait())
|
|
198
199
|
.then(function() {
|
|
199
200
|
socket.end();
|
|
200
201
|
}).then(succeed(done));
|
|
@@ -205,8 +206,8 @@ test("connection.blocked", connectionTest(
|
|
|
205
206
|
c.on('blocked', succeed(done));
|
|
206
207
|
c.open(OPEN_OPTS);
|
|
207
208
|
},
|
|
208
|
-
function(send,
|
|
209
|
-
happy_open(send,
|
|
209
|
+
function(send, wait, done, socket) {
|
|
210
|
+
happy_open(send, wait)
|
|
210
211
|
.then(function() {
|
|
211
212
|
send(defs.ConnectionBlocked, {reason: 'felt like it'}, 0);
|
|
212
213
|
})
|
|
@@ -218,8 +219,8 @@ test("connection.unblocked", connectionTest(
|
|
|
218
219
|
c.on('unblocked', succeed(done));
|
|
219
220
|
c.open(OPEN_OPTS);
|
|
220
221
|
},
|
|
221
|
-
function(send,
|
|
222
|
-
happy_open(send,
|
|
222
|
+
function(send, wait, done, socket) {
|
|
223
|
+
happy_open(send, wait)
|
|
223
224
|
.then(function() {
|
|
224
225
|
send(defs.ConnectionUnblocked, {}, 0);
|
|
225
226
|
})
|
|
@@ -239,9 +240,9 @@ test("happy", connectionTest(
|
|
|
239
240
|
c.close(kCallback(succeed(done), fail(done)));
|
|
240
241
|
}, function() {}));
|
|
241
242
|
},
|
|
242
|
-
function(send,
|
|
243
|
-
happy_open(send,
|
|
244
|
-
.then(
|
|
243
|
+
function(send, wait, done) {
|
|
244
|
+
happy_open(send, wait)
|
|
245
|
+
.then(wait(defs.ConnectionClose))
|
|
245
246
|
.then(function(close) {
|
|
246
247
|
send(defs.ConnectionCloseOk, {});
|
|
247
248
|
})
|
|
@@ -256,9 +257,9 @@ test("interleaved close frames", connectionTest(
|
|
|
256
257
|
c.close(kCallback(succeed(done), fail(done)));
|
|
257
258
|
}, done));
|
|
258
259
|
},
|
|
259
|
-
function(send,
|
|
260
|
-
happy_open(send,
|
|
261
|
-
.then(
|
|
260
|
+
function(send, wait, done) {
|
|
261
|
+
happy_open(send, wait)
|
|
262
|
+
.then(wait(defs.ConnectionClose))
|
|
262
263
|
.then(function(f) {
|
|
263
264
|
send(defs.ConnectionClose, {
|
|
264
265
|
replyText: "Ha!",
|
|
@@ -266,7 +267,7 @@ test("interleaved close frames", connectionTest(
|
|
|
266
267
|
methodId: 0, classId: 0
|
|
267
268
|
});
|
|
268
269
|
})
|
|
269
|
-
.then(
|
|
270
|
+
.then(wait(defs.ConnectionCloseOk))
|
|
270
271
|
.then(function(f) {
|
|
271
272
|
send(defs.ConnectionCloseOk, {});
|
|
272
273
|
})
|
|
@@ -280,8 +281,8 @@ test("server error close", connectionTest(
|
|
|
280
281
|
c.on('error', succeed(done));
|
|
281
282
|
c.open(OPEN_OPTS);
|
|
282
283
|
},
|
|
283
|
-
function(send,
|
|
284
|
-
happy_open(send,
|
|
284
|
+
function(send, wait, done) {
|
|
285
|
+
happy_open(send, wait)
|
|
285
286
|
.then(function(f) {
|
|
286
287
|
send(defs.ConnectionClose, {
|
|
287
288
|
replyText: "Begone",
|
|
@@ -289,7 +290,7 @@ test("server error close", connectionTest(
|
|
|
289
290
|
methodId: 0, classId: 0
|
|
290
291
|
});
|
|
291
292
|
})
|
|
292
|
-
.then(
|
|
293
|
+
.then(wait(defs.ConnectionCloseOk))
|
|
293
294
|
.then(succeed(done), fail(done));
|
|
294
295
|
}));
|
|
295
296
|
|
|
@@ -299,8 +300,8 @@ test("operator-intiated close", connectionTest(
|
|
|
299
300
|
c.on('error', fail(done));
|
|
300
301
|
c.open(OPEN_OPTS);
|
|
301
302
|
},
|
|
302
|
-
function(send,
|
|
303
|
-
happy_open(send,
|
|
303
|
+
function(send, wait, done) {
|
|
304
|
+
happy_open(send, wait)
|
|
304
305
|
.then(function(f) {
|
|
305
306
|
send(defs.ConnectionClose, {
|
|
306
307
|
replyText: "Begone",
|
|
@@ -308,7 +309,7 @@ test("operator-intiated close", connectionTest(
|
|
|
308
309
|
methodId: 0, classId: 0
|
|
309
310
|
});
|
|
310
311
|
})
|
|
311
|
-
.then(
|
|
312
|
+
.then(wait(defs.ConnectionCloseOk))
|
|
312
313
|
.then(succeed(done), fail(done));
|
|
313
314
|
}));
|
|
314
315
|
|
|
@@ -324,9 +325,9 @@ test("double close", connectionTest(
|
|
|
324
325
|
done();
|
|
325
326
|
}, done));
|
|
326
327
|
},
|
|
327
|
-
function(send,
|
|
328
|
-
happy_open(send,
|
|
329
|
-
.then(
|
|
328
|
+
function(send, wait, done) {
|
|
329
|
+
happy_open(send, wait)
|
|
330
|
+
.then(wait(defs.ConnectionClose))
|
|
330
331
|
.then(function() {
|
|
331
332
|
send(defs.ConnectionCloseOk, {});
|
|
332
333
|
})
|
|
@@ -358,15 +359,15 @@ test("send heartbeat after open", connectionTest(
|
|
|
358
359
|
c.open(opts);
|
|
359
360
|
}, done);
|
|
360
361
|
},
|
|
361
|
-
function(send,
|
|
362
|
+
function(send, wait, done, socket) {
|
|
362
363
|
var timer;
|
|
363
|
-
happy_open(send,
|
|
364
|
+
happy_open(send, wait)
|
|
364
365
|
.then(function() {
|
|
365
366
|
timer = setInterval(function() {
|
|
366
367
|
socket.write(HB_BUF);
|
|
367
368
|
}, heartbeat.UNITS_TO_MS);
|
|
368
369
|
})
|
|
369
|
-
.then(
|
|
370
|
+
.then(wait())
|
|
370
371
|
.then(function(hb) {
|
|
371
372
|
if (hb === HEARTBEAT) done();
|
|
372
373
|
else done("Next frame after silence not a heartbeat");
|
|
@@ -381,8 +382,8 @@ test("detect lack of heartbeats", connectionTest(
|
|
|
381
382
|
c.on('error', succeed(done));
|
|
382
383
|
c.open(opts);
|
|
383
384
|
},
|
|
384
|
-
function(send,
|
|
385
|
-
happy_open(send,
|
|
385
|
+
function(send, wait, done, socket) {
|
|
386
|
+
happy_open(send, wait)
|
|
386
387
|
.then(succeed(done), fail(done));
|
|
387
388
|
// conspicuously not sending anything ...
|
|
388
389
|
}));
|
package/test/data.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
'use strict';
|
|
4
4
|
|
|
5
5
|
var C = require('claire');
|
|
6
|
-
|
|
6
|
+
var Buffer = require('safe-buffer').Buffer;
|
|
7
7
|
var forAll = C.forAll;
|
|
8
8
|
var arb = C.data;
|
|
9
9
|
var transform = C.transform;
|
|
@@ -18,7 +18,7 @@ var Undefined = C.Undefined;
|
|
|
18
18
|
|
|
19
19
|
// Stub these out so we can use outside tests
|
|
20
20
|
// if (!suite) var suite = function() {}
|
|
21
|
-
// if (!test) var test = function() {}
|
|
21
|
+
// if (!test) var test = function() {}
|
|
22
22
|
|
|
23
23
|
// These aren't exported in claire/index. so I could have to reproduce
|
|
24
24
|
// them I guess.
|
|
@@ -36,7 +36,7 @@ function rangeInt(name, a, b) {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
function toFloat32(i) {
|
|
39
|
-
var b =
|
|
39
|
+
var b = Buffer.alloc(4);
|
|
40
40
|
b.writeFloatBE(i, 0);
|
|
41
41
|
return b.readFloatBE(0);
|
|
42
42
|
}
|
|
@@ -53,6 +53,12 @@ function floatChooser(maxExp) {
|
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
function explicitType(t, underlying) {
|
|
57
|
+
return label(t, transform(function(n) {
|
|
58
|
+
return {'!': t, value: n};
|
|
59
|
+
}, underlying));
|
|
60
|
+
}
|
|
61
|
+
|
|
56
62
|
// FIXME null, byte array, others?
|
|
57
63
|
|
|
58
64
|
var Octet = rangeInt('octet', 0, 255);
|
|
@@ -63,7 +69,7 @@ var ShortStr = label('shortstr',
|
|
|
63
69
|
|
|
64
70
|
var LongStr = label('longstr',
|
|
65
71
|
transform(
|
|
66
|
-
function(bytes) { return
|
|
72
|
+
function(bytes) { return Buffer.from(bytes); },
|
|
67
73
|
repeat(Octet)));
|
|
68
74
|
|
|
69
75
|
var UShort = rangeInt('short-uint', 0, 0xffff);
|
|
@@ -85,12 +91,27 @@ var Decimal = label('decimal', transform(
|
|
|
85
91
|
return {'!': 'decimal', value: {places: args[1], digits: args[0]}};
|
|
86
92
|
}, sequence(arb.UInt, Octet)));
|
|
87
93
|
|
|
94
|
+
// Signed 8 bit int
|
|
95
|
+
var Byte = rangeInt('byte', -128, 127);
|
|
96
|
+
|
|
97
|
+
// Explicitly typed values
|
|
98
|
+
var ExByte = explicitType('byte', Byte);
|
|
99
|
+
var ExInt8 = explicitType('int8', Byte);
|
|
100
|
+
var ExShort = explicitType('short', Short);
|
|
101
|
+
var ExInt16 = explicitType('int16', Short);
|
|
102
|
+
var ExInt = explicitType('int', Long);
|
|
103
|
+
var ExInt32 = explicitType('int32', Long);
|
|
104
|
+
var ExLong = explicitType('long', LongLong);
|
|
105
|
+
var ExInt64 = explicitType('int64', LongLong);
|
|
106
|
+
|
|
88
107
|
var FieldArray = label('field-array', recursive(function() {
|
|
89
108
|
return arb.Array(
|
|
90
109
|
arb.Null,
|
|
91
|
-
LongStr, ShortStr,
|
|
92
|
-
UShort, ULong, ULongLong,
|
|
93
|
-
Short, Long, LongLong,
|
|
110
|
+
LongStr, ShortStr,
|
|
111
|
+
Octet, UShort, ULong, ULongLong,
|
|
112
|
+
Byte, Short, Long, LongLong,
|
|
113
|
+
ExByte, ExInt8, ExShort, ExInt16,
|
|
114
|
+
ExInt, ExInt32, ExLong, ExInt64,
|
|
94
115
|
Bit, Float, Double, FieldTable, FieldArray)
|
|
95
116
|
}));
|
|
96
117
|
|
|
@@ -100,7 +121,9 @@ var FieldTable = label('table', recursive(function() {
|
|
|
100
121
|
arb.Null,
|
|
101
122
|
LongStr, ShortStr, Octet,
|
|
102
123
|
UShort, ULong, ULongLong,
|
|
103
|
-
Short, Long, LongLong,
|
|
124
|
+
Byte, Short, Long, LongLong,
|
|
125
|
+
ExByte, ExInt8, ExShort, ExInt16,
|
|
126
|
+
ExInt, ExInt32, ExLong, ExInt64,
|
|
104
127
|
Bit, Float, Double, FieldArray, FieldTable))
|
|
105
128
|
}));
|
|
106
129
|
|
|
@@ -184,7 +207,7 @@ function method(info) {
|
|
|
184
207
|
function properties(info) {
|
|
185
208
|
var types = info.args.map(argtype);
|
|
186
209
|
types.unshift(ULongLong); // size
|
|
187
|
-
var domain = sequence.apply(null, types);
|
|
210
|
+
var domain = sequence.apply(null, types);
|
|
188
211
|
var names = info.args.map(name);
|
|
189
212
|
return label(info.name, transform(function(fieldVals) {
|
|
190
213
|
return {id: info.id,
|
package/test/frame.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
var assert = require('assert');
|
|
4
4
|
var succeed = require('./util').succeed;
|
|
5
5
|
var fail = require('./util').fail;
|
|
6
|
-
|
|
6
|
+
var Buffer = require('safe-buffer').Buffer;
|
|
7
7
|
var connection = require('../lib/connection');
|
|
8
8
|
var Frames = connection.Connection;
|
|
9
9
|
var HEARTBEAT = require('../lib/frame').HEARTBEAT;
|
|
@@ -21,7 +21,7 @@ function inputs() {
|
|
|
21
21
|
return new PassThrough({objectMode: true});
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
var HB =
|
|
24
|
+
var HB = Buffer.from([defs.constants.FRAME_HEARTBEAT,
|
|
25
25
|
0, 0, // channel 0
|
|
26
26
|
0, 0, 0, 0, // zero size
|
|
27
27
|
defs.constants.FRAME_END]);
|
|
@@ -51,7 +51,7 @@ suite("Explicit parsing", function() {
|
|
|
51
51
|
var input = inputs();
|
|
52
52
|
var frames = new Frames(input);
|
|
53
53
|
frames.frameMax = 5; //for the max frame test
|
|
54
|
-
input.write(
|
|
54
|
+
input.write(Buffer.from(bytes));
|
|
55
55
|
frames.step(function(err, frame) {
|
|
56
56
|
if (err != null) done();
|
|
57
57
|
else done(new Error('Was a bogus frame!'));
|
|
@@ -166,7 +166,7 @@ var Content = transform(function(args) {
|
|
|
166
166
|
return {
|
|
167
167
|
method: args[0].fields,
|
|
168
168
|
header: args[1].fields,
|
|
169
|
-
body:
|
|
169
|
+
body: Buffer.from(args[2])
|
|
170
170
|
}
|
|
171
171
|
}, sequence(amqp.methods['BasicDeliver'],
|
|
172
172
|
amqp.properties['BasicProperties'], Body));
|
package/test/mux.js
CHANGED
|
@@ -37,7 +37,7 @@ test("single input", function(done) {
|
|
|
37
37
|
var mux = new Mux(output);
|
|
38
38
|
mux.pipeFrom(input);
|
|
39
39
|
|
|
40
|
-
var data = [1,2,3,4,5,6,7,8,9];
|
|
40
|
+
var data = [1,2,3,4,5,6,7,8,9];
|
|
41
41
|
// not 0, it's treated specially by PassThrough for some reason. By
|
|
42
42
|
// 'specially' I mean it breaks the stream. See e.g.,
|
|
43
43
|
// https://github.com/isaacs/readable-stream/pull/55
|
|
@@ -112,16 +112,20 @@ test("unpipe", function(done) {
|
|
|
112
112
|
|
|
113
113
|
schedule(function() {
|
|
114
114
|
pipedData.forEach(input.write.bind(input));
|
|
115
|
-
mux.unpipeFrom(input);
|
|
116
115
|
|
|
117
116
|
schedule(function() {
|
|
118
|
-
|
|
119
|
-
|
|
117
|
+
mux.unpipeFrom(input);
|
|
118
|
+
|
|
120
119
|
schedule(function() {
|
|
121
|
-
|
|
122
|
-
|
|
120
|
+
unpipedData.forEach(input.write.bind(input));
|
|
121
|
+
input.end();
|
|
122
|
+
schedule(function() {
|
|
123
|
+
// exhaust so that 'end' fires
|
|
124
|
+
var v; while (v = input.read());
|
|
125
|
+
});
|
|
123
126
|
});
|
|
124
127
|
});
|
|
128
|
+
|
|
125
129
|
});
|
|
126
130
|
|
|
127
131
|
input.on('end', function() {
|
package/test/util.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var Promise = require('bluebird');
|
|
3
4
|
var crypto = require('crypto');
|
|
4
5
|
var Connection = require('../lib/connection').Connection;
|
|
5
6
|
var PassThrough =
|
|
6
7
|
require('stream').PassThrough ||
|
|
7
8
|
require('readable-stream/passthrough');
|
|
8
|
-
var defer = require('when').defer;
|
|
9
9
|
var defs = require('../lib/defs');
|
|
10
10
|
var assert = require('assert');
|
|
11
11
|
|
|
@@ -76,29 +76,29 @@ function runServer(socket, run) {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
function
|
|
79
|
+
function wait(method) {
|
|
80
80
|
return function() {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
81
|
+
return new Promise(function(resolve, reject) {
|
|
82
|
+
if (method) {
|
|
83
|
+
frames.step(function(e, f) {
|
|
84
|
+
if (e !== null) return reject(e);
|
|
85
|
+
if (f.id === method)
|
|
86
|
+
resolve(f);
|
|
87
|
+
else
|
|
88
|
+
reject(new Error("Expected method: " + method +
|
|
89
|
+
", got " + f.id));
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
frames.step(function(e, f) {
|
|
94
|
+
if (e !== null) return reject(e);
|
|
95
|
+
else resolve(f);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
|
-
run(send,
|
|
101
|
+
run(send, wait);
|
|
102
102
|
return frames;
|
|
103
103
|
}
|
|
104
104
|
|
|
@@ -107,6 +107,19 @@ function succeed(done) {
|
|
|
107
107
|
return function() { done(); }
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
+
// Produce a callback that will complete the test successfully
|
|
111
|
+
// only if the value is an object, it has the specified
|
|
112
|
+
// attribute, and its value is equals to the expected value
|
|
113
|
+
function succeedIfAttributeEquals(attribute, value, done) {
|
|
114
|
+
return function(object) {
|
|
115
|
+
if (object && !(object instanceof Error) && value === object[attribute]) {
|
|
116
|
+
return done();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
done(new Error(attribute + " is not equal to " + value));
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
110
123
|
// Produce a callback that will fail the test, given either an error
|
|
111
124
|
// (to be used as a failure continuation) or any other value (to be
|
|
112
125
|
// used as a success continuation when failure is expected)
|
|
@@ -196,6 +209,7 @@ module.exports = {
|
|
|
196
209
|
socketPair: socketPair,
|
|
197
210
|
runServer: runServer,
|
|
198
211
|
succeed: succeed,
|
|
212
|
+
succeedIfAttributeEquals: succeedIfAttributeEquals,
|
|
199
213
|
fail: fail,
|
|
200
214
|
latch: latch,
|
|
201
215
|
completes: completes,
|