-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
256 lines (221 loc) · 6.91 KB
/
test.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
'use strict';
var Syslog = require('./index'),
fs = require('fs'),
net = require('net'),
dgram = require('dgram');
var BIND_PORT = 1234,
SOCKET_FILE = '/tmp/syslog2-test';
require('should');
function unlink(done) {
fs.unlink(SOCKET_FILE, done.bind(null, null));
}
before(unlink);
after(unlink);
// these are more thoroughly tested in their respective modules; just making sure
// that the logic to hook them up works correctly
describe('Transports', function () {
describe('tcp', function () {
var server;
before(function (done) {
server = net.createServer();
server.listen(BIND_PORT, done);
});
after(function (done) {
server.close(done);
});
it('should connect and pass messages', function (done) {
var log = new Syslog({
connection: {
type: 'tcp',
host: '127.0.0.1',
port: BIND_PORT
}
});
server.once('connection', function (socket) {
socket.once('data', function (chunk) {
log.end(done);
});
});
log.connect(function () {
log.write({msg: 'hello'});
});
});
});
describe('unix socket', function () {
var server;
before(function (done) {
server = net.createServer();
server.listen(SOCKET_FILE, done);
});
after(function (done) {
server.close(done);
});
it('should connect and pass messages', function (done) {
var log = new Syslog({
connection: {
type: 'unix',
path: SOCKET_FILE
}
});
server.once('connection', function (socket) {
socket.once('data', function (chunk) {
log.end(done);
});
});
log.connect(function () {
log.write({msg: 'hello'});
});
});
});
describe('udp', function () {
var server;
before(function (done) {
server = dgram.createSocket('udp4');
server.bind(BIND_PORT, done);
});
after(function () {
server.close();
});
it('should pass messages', function (done) {
var log = new Syslog({
connection: {
type: 'udp',
host: '127.0.0.1',
port: BIND_PORT
}
});
server.once('message', function (chunk) {
log.end(done);
});
log.connect(function () {
log.write({msg: 'hello'});
});
});
});
});
describe('Syslog2', function () {
var server;
beforeEach(function (done) {
server = net.createServer();
server.listen(BIND_PORT, done);
});
afterEach(function (done) {
server.close(done);
});
it('should default to not reconnecting', function (done) {
var log = new Syslog({
connection: {
type: 'tcp',
port: BIND_PORT
}
});
server.once('connection', function (socket) {
socket.destroy();
});
log.connect(function () {
log.once('error', function (err) {
err.message.should.match(/Disconnected, reconnect disabled/);
log.end(done);
});
});
});
it('should emit a \'warn\' event for socket errors while reconnecting', function (done) {
var log = new Syslog({
connection: {
type: 'tcp',
port: BIND_PORT
}, reconnect: {
enabled: true,
maxTries: 1,
initalDelay: 0,
delayFactor: 0,
maxDelay: 0
}
});
server.once('connection', function (socket) {
log.stream.emit('error', 'foo');
socket.destroy();
});
log.connect(function () {
log.once('warn', function (err) {
err.should.equal('foo');
log.end(done);
});
});
});
it('should give up after the specified number of reconnection attempts', function (done) {
var log = new Syslog({
connection: {
type: 'tcp',
ip: '127.0.0.1',
port: 64993
}, reconnect: {
enabled: true,
maxTries: 1,
initalDelay: 0,
delayFactor: 0,
maxDelay: 0
}
});
server.once('connection', function (socket) {
socket.destroy();
});
log.connect(function () {
log.once('error', function (err) {
err.should.match(/Unable to reconnect, reach max retries/);
log.end(done);
});
});
});
it('should not try to reconnect multiple times', function (done) {
var log = new Syslog({
connection: {
type: 'tcp',
ip: '127.0.0.1',
port: 64993
}, reconnect: {
enabled: true,
maxTries: 100,
initalDelay: 0,
delayFactor: 0,
maxDelay: 0
}
});
server.once('connection', function (socket) {
socket.destroy();
});
log.connect(function () {
var warnings = 0;
log.on('warn', function () {
warnings++;
});
log.once('error', function (err) {
warnings.should.equal(100);
err.should.match(/Unable to reconnect, reached max retries/);
log.end(done);
});
});
});
it('should buffer messages for delivery when connected', function (done) {
var log = new Syslog({
connection: {
type: 'tcp',
ip: '127.0.0.1',
port: BIND_PORT
}
});
server.once('connection', function (socket) {
socket.once('data', function (chunk) {
log.end(done);
});
});
log.on('warn', function (err) {
console.log(err.stack);
});
log.write({ msg: 'hello' });
log.connect();
});
xit('should not lose messages on write errors', function (done) {
// implement the ring buffer thing and a protection layer rather than a direct pipe
});
});