Permalink
Browse files

[test] tests.

  • Loading branch information...
1 parent 6747499 commit a203d22f0d0d09925c4f1615e765a2cfb5c25836 @chjj chjj committed May 31, 2012
Showing with 189 additions and 0 deletions.
  1. +117 −0 test/helpers.js
  2. +36 −0 test/winstond-http-test.js
  3. +36 −0 test/winstond-nssocket-test.js
View
@@ -0,0 +1,117 @@
+/**
+ * Helpers
+ */
+
+var fs = require('fs')
+ , assert = require('assert')
+ , vows = require('vows')
+ , winstond = require('winstond')
+ , winston = require('winston');
+
+exports.run = function(name, server, logger) {
+ var logFile = __dirname + '/test.log';
+
+ /**
+ * Server
+ */
+
+ server.listen();
+
+ server.add(winstond.transports.File, {
+ filename: logFile
+ });
+
+ try {
+ server.remove(winstond.transports.Console);
+ } catch (e) {
+ ;
+ }
+
+ /**
+ * Logging
+ */
+
+ return vows.describe('winstond/' + name).addBatch({
+ 'An instance of Server': {
+ 'topic': logger,
+ 'should allow logging': {
+ 'topic': function() {
+ var next = this.callback;
+ logger.log('info', 'Logging test.', {}, next);
+ },
+ 'when using the .log() method': function(logged) {
+ assert.isNotNull(logged);
+ }
+ }
+ }
+ }).addBatch({
+ 'An instance of Server': {
+ 'topic': logger,
+ 'should allow querying': {
+ 'topic': function() {
+ var next = this.callback;
+ logger.log('info', 'Logging test.', {}, function() {
+ setTimeout(function() {
+ logger.query(next);
+ }, 1000);
+ });
+ },
+ 'when using the .query() method': function(err, logs) {
+ assert.equal(logs.http
+ ? logs.http.file[0].message
+ : logs.nssocket.file[0].message, 'Logging test.');
+ }
+ }
+ }
+ }).addBatch({
+ 'An instance of Server': {
+ 'topic': logger,
+ 'should allow streaming': {
+ 'topic': function() {
+ var next = this.callback
+ , pending = 3
+ , logs = [];
+
+ try {
+ logger.remove(winston.transports.Console);
+ } catch (e) {
+ ;
+ }
+
+ logger.stream().on('log', function(log) {
+ logs.push(log);
+ if (!--pending) {
+ this.destroy();
+ next(null, logs);
+ }
+ });
+
+ setTimeout(function() {
+ var i = 3;
+ while (i--) {
+ logger.log('info', 'hello world ' + i, {});
+ }
+ logger.add(winston.transports.Console);
+ }, 1000);
+ },
+ 'when using the .stream() method': function(err, logs) {
+ assert.isNull(err);
+ logs.forEach(function(log) {
+ assert.equal(log.message.replace(/ \d+/g, ''), 'hello world');
+ });
+ }
+ }
+ }
+ }).addBatch({
+ 'An instance of Server': {
+ 'topic': logger,
+ 'should cleanup when done': function() {
+ try {
+ fs.unlinkSync(logFile);
+ } catch (e) {
+ ;
+ }
+ }
+ }
+ });
+};
View
@@ -0,0 +1,36 @@
+/**
+ * winstond http test
+ */
+
+var fs = require('fs')
+ , assert = require('assert')
+ , vows = require('vows')
+ , winstond = require('winstond')
+ , winston = require('winston')
+ , helpers = require('./helpers');
+
+/**
+ * Server
+ */
+
+var server = winstond.http.createServer({
+ services: ['collect', 'query', 'stream'],
+ port: 9003
+});
+
+/**
+ * Logger
+ */
+
+var logger = new winston.Logger;
+
+logger.add(winston.transports.Http, {
+ host: '127.0.0.1',
+ port: 9003
+});
+
+/**
+ * Tests
+ */
+
+helpers.run('http-server', server, logger).export(module);
@@ -0,0 +1,36 @@
+/**
+ * winstond nssocket test
+ */
+
+var fs = require('fs')
+ , assert = require('assert')
+ , vows = require('vows')
+ , winstond = require('winstond')
+ , winston = require('winston')
+ , helpers = require('./helpers');
+
+/**
+ * Server
+ */
+
+var server = winstond.nssocket.createServer({
+ services: ['collect', 'query', 'stream'],
+ port: 9003
+});
+
+/**
+ * Logger
+ */
+
+var logger = new winston.Logger;
+
+logger.add(require('winston-nssocket').Nssocket, {
+ host: '127.0.0.1',
+ port: 9003
+});
+
+/**
+ * Tests
+ */
+
+helpers.run('nssocket-server', server, logger).export(module);

0 comments on commit a203d22

Please sign in to comment.