Authored by ZhouQiqi

add unit test coverage

# influxdb-winston
[https://img.shields.io/npm/v/influxdb-winston.svg]
[![NPM Status](https://img.shields.io/npm/v/influxdb-winston.svg)](https://www.npmjs.com/package/influxdb-winston)
[![Build Status](https://travis-ci.org/YOHO-LAB/influxdb-winston.svg?branch=master)](https://travis-ci.org/YOHO-LAB/influxdb-winston)
## What is this?
A InfluxDB transport for winston by TCP or UDP.
... ...
... ... @@ -24,7 +24,7 @@ gulp.task('test-cov', function() {
gulp.task('doc', function() {
gulp.src(['libs/**/*.js', 'README.md'])
.pipe(gulpDoxx({
title: 'yo',
title: 'influxdb-winston',
urlPrefix: path.join(__dirname, "docs")
}))
... ...
... ... @@ -68,7 +68,7 @@ class BaseTranport extends Transport {
//make tags
let tags = [
'host=' + os.hostname(),
'host=' + os.hostname(),
'pid=' + process.pid,
'level=' + level,
... ...
... ... @@ -28,7 +28,7 @@ class TcpTransport extends BaseTransport {
super(options);
this.name = "tcpInfuxdbLogger";
let that = this;
this.initDatabase(function () {
this.initDatabase(function() {
that.isCreated = true;
that.options.line = false;
that.addLine();
... ... @@ -45,13 +45,13 @@ class TcpTransport extends BaseTransport {
let opts = this.makeReq(path, 'GET');
//send to influxdb create database
let req = http.request(opts, function (res) {
let req = http.request(opts, function(res) {
let chunks = [];
res.on("data", function (chunk) {
res.on("data", function(chunk) {
chunks.push(chunk);
});
res.on("end", function () {
res.on("end", function() {
let body = Buffer.concat(chunks);
callback(res, body.toString());
});
... ... @@ -85,7 +85,6 @@ class TcpTransport extends BaseTransport {
options = this.makeReq(path, 'POST'),
line = logger.options.line,
logs = logger.logs;
//if influxdb database uninit,add line to a array;
if (!logger.isCreated) {
logger.logs.push({
... ... @@ -95,14 +94,14 @@ class TcpTransport extends BaseTransport {
} else {
//send to influxdb
this.sendLine(options,line);
this.sendLine(options, line);
if (logs.length < 1) {
return;
}
//send logs's line to influxdb
for (var i = 0; i < logs.length; i++) {
this.sendLine(logs[i].opts,logs[i].line);
this.sendLine(logs[i].opts, logs[i].line);
}
// empty logs array.
... ... @@ -116,7 +115,7 @@ class TcpTransport extends BaseTransport {
* @param {Object} options request
* @param {String} line influxdb line
*/
sendLine(options,line) {
sendLine(options, line) {
if (!line) {
return;
}
... ...
"use strict";
let expect = require('expect.js'),
rewire = require('rewire'),
http = require('../mock/http'),
os = require('os');
describe('/libs/TcpTransport', function() {
let TcpTransport = rewire("../../libs/TcpTransport");
TcpTransport.__set__('http', http);
describe('constructor', function() {
let trans = new TcpTransport({
level: 'error',
host: 'xxx',
port: '1234',
measurement: 'xxx'
});
it('expect trans to be tcpInfuxdbLogger', function() {
expect(trans.name).to.be('tcpInfuxdbLogger');
});
it('expect addline send buffer right value', function() {
trans.log('info', 'xxx', {}, function() {});
let line = 'xxx,host=' + os.hostname() + ',pid=' + process.pid + ',level=info message="xxx",meta="{}"';
expect(http.writeRet).to.be(line);
});
it('init database async,log add first,when database done,log add batch!', function(done) {
http.async = true;
let atrans = new TcpTransport({
level: 'error',
host: 'xxx',
measurement: 'xxx'
});
let i = 0;
atrans.log('info', 'xxx','123',function(){
i++;
});
atrans.log('info', 'xxx','123',function(){
i++;
});
if(i>1) {
expect(i).to.be(2);
}
setTimeout(done,50);
});
});
});
\ No newline at end of file
... ...
"use strict";
let expect = require('expect.js'),
rewire = require('rewire'),
dgram = require('../mock/dgram'),
os = require('os');
describe('/libs/UdpTransport',function(){
let UdpTransport = rewire("../../libs/UdpTransport");
UdpTransport.__set__('dgram',dgram);
describe('constructor',function(){
let trans = new UdpTransport({
level:'error',
host:'xxx',
port:'1234',
measurement:'xxx'
});
it('expect trans to be udpInfuxdbLogger',function(){
expect(trans.name).to.be('udpInfuxdbLogger');
});
it('expect addline send buffer right value',function(){
trans.log('info','xxx',{},function(){});
let line = 'xxx,host='+os.hostname()+',pid='+process.pid+',level=info message="xxx",meta="{}"'
let buf = new Buffer(line);
expect(dgram.ret.buf).to.eql(buf);
expect(dgram.isClose).to.be(true);
expect(dgram.ret.end).to.be(buf.length);
expect(dgram.ret.port).to.be('1234');
expect(dgram.ret.host).to.be('xxx');
});
});
});
\ No newline at end of file
... ...
... ... @@ -7,14 +7,25 @@
let dgram = {
createSocket:function(name) {
return {
send:function(p1,p2,p3,p4,p5,function() {
send:function(p1,p2,p3,p4,p5,cb) {
}),
close:function() {
dgram.ret = {
buf:p1,
start:p2,
end:p3,
port:p4,
host:p5
};
cb();
},
close:function() {
dgram.isClose = true;
}
}
}
},
ret:false,
isClose:false
}
module.exports = dgram;
\ No newline at end of file
... ...
... ... @@ -5,28 +5,36 @@
* @type {Object}
*/
let http = {
request:function(options,cb) {
cb({
on:function(event,cb) {
if(event == 'data') {
cd(http.buff);
} else {
cb();
request: function(options, cb) {
if (typeof cb === 'function') {
cb({
on: function(event, cb) {
if (event == 'data') {
cb(new Buffer('done'));
} else {
if (http.async) {
setTimeout(function(){
cb();
}, 10);
} else {
cb();
}
}
}
}
})
});
}
return {
write:function(line) {
write: function(line) {
http.writeRet = line;
},
end:function() {
end: function() {
}
}
};
},
buff: null
writeRet: null
};
module.exports = http
\ No newline at end of file
module.exports = http;
\ No newline at end of file
... ...