add unit test coverage
Showing
8 changed files
with
131 additions
and
23 deletions
1 | 1 | ||
2 | # influxdb-winston | 2 | # influxdb-winston |
3 | 3 | ||
4 | +[https://img.shields.io/npm/v/influxdb-winston.svg] | ||
5 | +[![NPM Status](https://img.shields.io/npm/v/influxdb-winston.svg)](https://www.npmjs.com/package/influxdb-winston) | ||
6 | +[![Build Status](https://travis-ci.org/YOHO-LAB/influxdb-winston.svg?branch=master)](https://travis-ci.org/YOHO-LAB/influxdb-winston) | ||
7 | + | ||
4 | ## What is this? | 8 | ## What is this? |
5 | 9 | ||
6 | A InfluxDB transport for winston by TCP or UDP. | 10 | A InfluxDB transport for winston by TCP or UDP. |
@@ -24,7 +24,7 @@ gulp.task('test-cov', function() { | @@ -24,7 +24,7 @@ gulp.task('test-cov', function() { | ||
24 | gulp.task('doc', function() { | 24 | gulp.task('doc', function() { |
25 | gulp.src(['libs/**/*.js', 'README.md']) | 25 | gulp.src(['libs/**/*.js', 'README.md']) |
26 | .pipe(gulpDoxx({ | 26 | .pipe(gulpDoxx({ |
27 | - title: 'yo', | 27 | + title: 'influxdb-winston', |
28 | urlPrefix: path.join(__dirname, "docs") | 28 | urlPrefix: path.join(__dirname, "docs") |
29 | 29 | ||
30 | })) | 30 | })) |
@@ -28,7 +28,7 @@ class TcpTransport extends BaseTransport { | @@ -28,7 +28,7 @@ class TcpTransport extends BaseTransport { | ||
28 | super(options); | 28 | super(options); |
29 | this.name = "tcpInfuxdbLogger"; | 29 | this.name = "tcpInfuxdbLogger"; |
30 | let that = this; | 30 | let that = this; |
31 | - this.initDatabase(function () { | 31 | + this.initDatabase(function() { |
32 | that.isCreated = true; | 32 | that.isCreated = true; |
33 | that.options.line = false; | 33 | that.options.line = false; |
34 | that.addLine(); | 34 | that.addLine(); |
@@ -45,13 +45,13 @@ class TcpTransport extends BaseTransport { | @@ -45,13 +45,13 @@ class TcpTransport extends BaseTransport { | ||
45 | let opts = this.makeReq(path, 'GET'); | 45 | let opts = this.makeReq(path, 'GET'); |
46 | 46 | ||
47 | //send to influxdb create database | 47 | //send to influxdb create database |
48 | - let req = http.request(opts, function (res) { | 48 | + let req = http.request(opts, function(res) { |
49 | let chunks = []; | 49 | let chunks = []; |
50 | - res.on("data", function (chunk) { | 50 | + res.on("data", function(chunk) { |
51 | chunks.push(chunk); | 51 | chunks.push(chunk); |
52 | }); | 52 | }); |
53 | 53 | ||
54 | - res.on("end", function () { | 54 | + res.on("end", function() { |
55 | let body = Buffer.concat(chunks); | 55 | let body = Buffer.concat(chunks); |
56 | callback(res, body.toString()); | 56 | callback(res, body.toString()); |
57 | }); | 57 | }); |
@@ -85,7 +85,6 @@ class TcpTransport extends BaseTransport { | @@ -85,7 +85,6 @@ class TcpTransport extends BaseTransport { | ||
85 | options = this.makeReq(path, 'POST'), | 85 | options = this.makeReq(path, 'POST'), |
86 | line = logger.options.line, | 86 | line = logger.options.line, |
87 | logs = logger.logs; | 87 | logs = logger.logs; |
88 | - | ||
89 | //if influxdb database uninit,add line to a array; | 88 | //if influxdb database uninit,add line to a array; |
90 | if (!logger.isCreated) { | 89 | if (!logger.isCreated) { |
91 | logger.logs.push({ | 90 | logger.logs.push({ |
@@ -95,14 +94,14 @@ class TcpTransport extends BaseTransport { | @@ -95,14 +94,14 @@ class TcpTransport extends BaseTransport { | ||
95 | } else { | 94 | } else { |
96 | 95 | ||
97 | //send to influxdb | 96 | //send to influxdb |
98 | - this.sendLine(options,line); | 97 | + this.sendLine(options, line); |
99 | if (logs.length < 1) { | 98 | if (logs.length < 1) { |
100 | return; | 99 | return; |
101 | } | 100 | } |
102 | 101 | ||
103 | //send logs's line to influxdb | 102 | //send logs's line to influxdb |
104 | for (var i = 0; i < logs.length; i++) { | 103 | for (var i = 0; i < logs.length; i++) { |
105 | - this.sendLine(logs[i].opts,logs[i].line); | 104 | + this.sendLine(logs[i].opts, logs[i].line); |
106 | } | 105 | } |
107 | 106 | ||
108 | // empty logs array. | 107 | // empty logs array. |
@@ -116,7 +115,7 @@ class TcpTransport extends BaseTransport { | @@ -116,7 +115,7 @@ class TcpTransport extends BaseTransport { | ||
116 | * @param {Object} options request | 115 | * @param {Object} options request |
117 | * @param {String} line influxdb line | 116 | * @param {String} line influxdb line |
118 | */ | 117 | */ |
119 | - sendLine(options,line) { | 118 | + sendLine(options, line) { |
120 | if (!line) { | 119 | if (!line) { |
121 | return; | 120 | return; |
122 | } | 121 | } |
1 | +"use strict"; | ||
2 | + | ||
3 | +let expect = require('expect.js'), | ||
4 | + rewire = require('rewire'), | ||
5 | + http = require('../mock/http'), | ||
6 | + os = require('os'); | ||
7 | + | ||
8 | +describe('/libs/TcpTransport', function() { | ||
9 | + let TcpTransport = rewire("../../libs/TcpTransport"); | ||
10 | + TcpTransport.__set__('http', http); | ||
11 | + describe('constructor', function() { | ||
12 | + let trans = new TcpTransport({ | ||
13 | + level: 'error', | ||
14 | + host: 'xxx', | ||
15 | + port: '1234', | ||
16 | + measurement: 'xxx' | ||
17 | + }); | ||
18 | + | ||
19 | + it('expect trans to be tcpInfuxdbLogger', function() { | ||
20 | + expect(trans.name).to.be('tcpInfuxdbLogger'); | ||
21 | + }); | ||
22 | + | ||
23 | + it('expect addline send buffer right value', function() { | ||
24 | + trans.log('info', 'xxx', {}, function() {}); | ||
25 | + let line = 'xxx,host=' + os.hostname() + ',pid=' + process.pid + ',level=info message="xxx",meta="{}"'; | ||
26 | + expect(http.writeRet).to.be(line); | ||
27 | + }); | ||
28 | + | ||
29 | + it('init database async,log add first,when database done,log add batch!', function(done) { | ||
30 | + http.async = true; | ||
31 | + let atrans = new TcpTransport({ | ||
32 | + level: 'error', | ||
33 | + host: 'xxx', | ||
34 | + measurement: 'xxx' | ||
35 | + }); | ||
36 | + let i = 0; | ||
37 | + atrans.log('info', 'xxx','123',function(){ | ||
38 | + i++; | ||
39 | + }); | ||
40 | + atrans.log('info', 'xxx','123',function(){ | ||
41 | + i++; | ||
42 | + }); | ||
43 | + if(i>1) { | ||
44 | + expect(i).to.be(2); | ||
45 | + } | ||
46 | + | ||
47 | + setTimeout(done,50); | ||
48 | + }); | ||
49 | + }); | ||
50 | + | ||
51 | +}); |
1 | +"use strict"; | ||
2 | + | ||
3 | +let expect = require('expect.js'), | ||
4 | +rewire = require('rewire'), | ||
5 | +dgram = require('../mock/dgram'), | ||
6 | +os = require('os'); | ||
7 | + | ||
8 | +describe('/libs/UdpTransport',function(){ | ||
9 | + let UdpTransport = rewire("../../libs/UdpTransport"); | ||
10 | + UdpTransport.__set__('dgram',dgram); | ||
11 | + describe('constructor',function(){ | ||
12 | + let trans = new UdpTransport({ | ||
13 | + level:'error', | ||
14 | + host:'xxx', | ||
15 | + port:'1234', | ||
16 | + measurement:'xxx' | ||
17 | + }); | ||
18 | + | ||
19 | + it('expect trans to be udpInfuxdbLogger',function(){ | ||
20 | + expect(trans.name).to.be('udpInfuxdbLogger'); | ||
21 | + }); | ||
22 | + | ||
23 | + it('expect addline send buffer right value',function(){ | ||
24 | + trans.log('info','xxx',{},function(){}); | ||
25 | + let line = 'xxx,host='+os.hostname()+',pid='+process.pid+',level=info message="xxx",meta="{}"' | ||
26 | + let buf = new Buffer(line); | ||
27 | + expect(dgram.ret.buf).to.eql(buf); | ||
28 | + expect(dgram.isClose).to.be(true); | ||
29 | + expect(dgram.ret.end).to.be(buf.length); | ||
30 | + expect(dgram.ret.port).to.be('1234'); | ||
31 | + expect(dgram.ret.host).to.be('xxx'); | ||
32 | + }); | ||
33 | + }); | ||
34 | + | ||
35 | +}); |
@@ -7,14 +7,25 @@ | @@ -7,14 +7,25 @@ | ||
7 | let dgram = { | 7 | let dgram = { |
8 | createSocket:function(name) { | 8 | createSocket:function(name) { |
9 | return { | 9 | return { |
10 | - send:function(p1,p2,p3,p4,p5,function() { | 10 | + send:function(p1,p2,p3,p4,p5,cb) { |
11 | 11 | ||
12 | - }), | ||
13 | - close:function() { | 12 | + dgram.ret = { |
13 | + buf:p1, | ||
14 | + start:p2, | ||
15 | + end:p3, | ||
16 | + port:p4, | ||
17 | + host:p5 | ||
18 | + }; | ||
14 | 19 | ||
20 | + cb(); | ||
21 | + }, | ||
22 | + close:function() { | ||
23 | + dgram.isClose = true; | ||
15 | } | 24 | } |
16 | } | 25 | } |
17 | - } | 26 | + }, |
27 | + ret:false, | ||
28 | + isClose:false | ||
18 | } | 29 | } |
19 | 30 | ||
20 | module.exports = dgram; | 31 | module.exports = dgram; |
@@ -5,28 +5,36 @@ | @@ -5,28 +5,36 @@ | ||
5 | * @type {Object} | 5 | * @type {Object} |
6 | */ | 6 | */ |
7 | let http = { | 7 | let http = { |
8 | - request:function(options,cb) { | 8 | + request: function(options, cb) { |
9 | + if (typeof cb === 'function') { | ||
9 | cb({ | 10 | cb({ |
10 | - on:function(event,cb) { | ||
11 | - if(event == 'data') { | ||
12 | - cd(http.buff); | 11 | + on: function(event, cb) { |
12 | + if (event == 'data') { | ||
13 | + cb(new Buffer('done')); | ||
13 | } else { | 14 | } else { |
15 | + if (http.async) { | ||
16 | + setTimeout(function(){ | ||
14 | cb(); | 17 | cb(); |
18 | + }, 10); | ||
19 | + } else { | ||
20 | + cb(); | ||
21 | + } | ||
22 | + | ||
23 | + } | ||
15 | } | 24 | } |
25 | + }); | ||
16 | } | 26 | } |
17 | - }) | ||
18 | 27 | ||
19 | return { | 28 | return { |
20 | - write:function(line) { | 29 | + write: function(line) { |
21 | http.writeRet = line; | 30 | http.writeRet = line; |
22 | }, | 31 | }, |
23 | - end:function() { | 32 | + end: function() { |
24 | 33 | ||
25 | } | 34 | } |
26 | - } | 35 | + }; |
27 | }, | 36 | }, |
28 | - buff: null | ||
29 | writeRet: null | 37 | writeRet: null |
30 | }; | 38 | }; |
31 | 39 | ||
32 | -module.exports = http; | ||
40 | +module.exports = http; |
-
Please register or login to post a comment