-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathunix-connect-test.js
More file actions
132 lines (119 loc) · 3.38 KB
/
Copy pathunix-connect-test.js
File metadata and controls
132 lines (119 loc) · 3.38 KB
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
/* eslint no-sync: "off" */
const fs = require('fs');
const vows = require('vows');
const assert = require('assert');
const unix = require('unix-dgram');
const parser = require('@myndzi/glossy').Parse;
const Syslog = require('../lib/winston-syslog').Syslog;
const { MESSAGE, LEVEL } = require('triple-beam');
const SOCKNAME = '/tmp/unix_dgram.sock';
const transport = new Syslog({
protocol: 'unix-connect',
path: SOCKNAME
});
try {
fs.unlinkSync(SOCKNAME);
} catch (e) {
/* swallow */
}
let times = 0;
let server;
vows.describe('unix-connect').addBatch({
'Trying to log to a non-existant log server': {
'topic': function () {
const self = this;
transport.once('error', function (err) {
self.callback(null, err);
});
transport.log({ [LEVEL]: 'debug', [MESSAGE]: `data${++times}` }, function (err) {
assert.equal(null, err);
assert.equal(transport.queue.length, 1);
});
},
'should enqueue the log message': function (err) {
assert(err);
assert.equal(err.syscall, 'connect');
}
}
}).addBatch({
'Logging when log server is up': {
'topic': function () {
const self = this;
let n = 0;
server = unix.createSocket('unix_dgram', function (buf) {
parser.parse(buf, function (d) {
++n;
assert(n <= 2);
assert.equal(d.message, 'node[' + process.pid + ']: data' + n);
assert.equal(d.severity, 'debug');
if (n === 2) {
self.callback();
}
});
});
server.bind(SOCKNAME);
transport.log({ [LEVEL]: 'debug', [MESSAGE]: `data${++times}` }, function (err) {
assert.ifError(err);
});
},
'should print both the enqueed and the new msg': function (err) {
assert.ifError(err);
}
}
}).addBatch({
'Logging if server goes down again': {
'topic': function () {
const self = this;
transport.once('error', function (err) {
self.callback(null, err);
});
server.close();
transport.log({ [LEVEL]: 'debug', [MESSAGE]: `data${++times}` }, function (err) {
assert.ifError(err);
assert.equal(transport.queue.length, 1);
});
},
'should enqueue the log message': function (err) {
assert(err);
assert.equal(err.syscall, 'send');
transport.close();
}
}
}).addBatch({
'Logging works if server comes up again': {
'topic': function () {
const self = this;
transport.once('error', function (err) {
// Ignore error -- server hasn't come up yet, that's fine/expected
assert(err);
assert.equal(err.syscall, 'send');
});
let n = 2;
try {
fs.unlinkSync(SOCKNAME);
} catch (e) {
/* swallow */
}
server = unix.createSocket('unix_dgram', function (buf) {
parser.parse(buf, function (d) {
++n;
assert(n <= 4);
assert.equal(d.message, 'node[' + process.pid + ']: data' + n);
if (n === 4) {
self.callback();
}
});
});
server.bind(SOCKNAME);
transport.log({ [LEVEL]: 'debug', [MESSAGE]: `data${++times}` }, function (err) {
assert.ifError(err);
});
return null;
},
'should print both the enqueed and the new msg': function (err) {
assert.ifError(err);
server.close();
return null;
}
}
}).export(module);