1 | const sinon = require('sinon');
|
2 | const assert = require('assert');
|
3 | const logzioLogger = require('../lib/logzio-nodejs.js');
|
4 |
|
5 | const dummyHost = 'logz.io';
|
6 |
|
7 | const createLogger = function (options) {
|
8 | const myoptions = options;
|
9 | myoptions.token = 'acrSGIefherhsZYOpzxeGBpTyqgSzaMk';
|
10 | myoptions.type = 'testnode';
|
11 | myoptions.debug = true;
|
12 | myoptions.host = dummyHost;
|
13 | myoptions.sendIntervalMs = options.sendIntervalMs || 1000;
|
14 | return logzioLogger.createLogger(myoptions);
|
15 | };
|
16 |
|
17 | describe('sending udp', () => {
|
18 | it('sends single log', (done) => {
|
19 | const logger = createLogger({
|
20 | bufferSize: 1,
|
21 | protocol: 'udp',
|
22 | });
|
23 |
|
24 | let udpSentCounter = 0;
|
25 | sinon.stub(logger.udpClient, 'send').callsFake(() => { udpSentCounter += 1; });
|
26 |
|
27 | logger.log('hello from the other side');
|
28 | assert(udpSentCounter === 1);
|
29 |
|
30 | logger.close();
|
31 | done();
|
32 | });
|
33 |
|
34 | it('sends multiple logs', (done) => {
|
35 | const logger = createLogger({
|
36 | bufferSize: 2,
|
37 | protocol: 'udp',
|
38 | });
|
39 |
|
40 | let udpSentCounter = 0;
|
41 | sinon.stub(logger.udpClient, 'send').callsFake(() => { udpSentCounter += 1; });
|
42 |
|
43 | logger.log('hello from the other side');
|
44 | logger.log('hello from the other side');
|
45 | logger.log('hello from the other side');
|
46 | logger.log('hello from the other side');
|
47 | assert(udpSentCounter === 4);
|
48 |
|
49 | logger.close();
|
50 | done();
|
51 | });
|
52 |
|
53 | it('sends logs after close', (done) => {
|
54 | const logger = createLogger({
|
55 | bufferSize: 10,
|
56 | protocol: 'udp',
|
57 | });
|
58 |
|
59 | let udpSentCounter = 0;
|
60 | sinon.stub(logger.udpClient, 'send').callsFake(() => { udpSentCounter += 1; });
|
61 |
|
62 | logger.log('hello from the other side');
|
63 | logger.log('hello from the other side');
|
64 | logger.log('hello from the other side');
|
65 | logger.log('hello from the other side');
|
66 | assert(udpSentCounter === 0);
|
67 |
|
68 | logger.close();
|
69 | assert(udpSentCounter === 4);
|
70 |
|
71 | done();
|
72 | });
|
73 |
|
74 | it('call callback on udp error', (done) => {
|
75 | const udpError = 'udp error';
|
76 |
|
77 | const logger = createLogger({
|
78 | bufferSize: 1,
|
79 | protocol: 'udp',
|
80 | callback: function assertCalled(err) {
|
81 | assert(err.message.indexOf('Failed to send udp log message') >= 0);
|
82 | done();
|
83 | },
|
84 |
|
85 | });
|
86 |
|
87 | sinon.stub(logger.udpClient, 'send').callsFake((buffer, offset, length, port, address, callback) => {
|
88 | callback(udpError);
|
89 | });
|
90 |
|
91 | logger.log('hello from the other side');
|
92 | logger.sendAndClose();
|
93 | });
|
94 | });
|