<commit_before>var root = __dirname + "/../";
var path = require('path');
var net = require('net');
var util = require('util');
var fluent = require(path.join(root, 'lib/FluentLogger'));
var should = require('should');

var testPort = 24242;
var testHost = '127.0.0.1';

describe('FluentLogger', function() {
  var server;

  beforeEach(function(done) {
    server = new net.Server();
    server.listen(testPort, function() {
      done();
    });
  });

  afterEach(function(done) {
    try { server.close(); } catch(e) {}
    done();
  });

  describe('new instance', function() {
    it('should connect fluentd server', function(done) {
      server.on('connection', function(socket) {
        done();
      });
      new fluent.FluentLogger(testPort, testHost);
    });
  });

  describe('#post()', function() {
    var logger;

    it('should post log', function(done) {
      server.on('connection', function(socket) {
        socket.setEncoding('utf8');
        socket.on('data', function(data) {
          var json = JSON.parse(data);
          json[0].should.eql('test.fluent');
          json[1].should.be.a('number');
          json[2].should.eql({ foo: "bar" });
          done();
        });
      });

      logger = new fluent.FluentLogger(testPort, testHost);
      logger.post('test.fluent', { foo: "bar" });
    });
  });

  describe('retry feature', function() {
    beforeEach(function(done) {
      server.close();
      done();
    });

    it('should be retry if connection was broken', function(done) {
      this.timeout(10000);

      var logger = new fluent.FluentLogger(testPort, testHost);
      var count = 1;

      logger.on('retry', function() {
        util.print(count++);
      });

      logger.on('connect', function() {
        done();
      });

      setTimeout(function() {
        server.listen(testPort);
      }, 8000);
    });
  });

  describe('buffer feature', function() {
    beforeEach(function(done) {
      server.close();
      done();
    });

    it('should be push buffer if connection was broken', function(done) {
      var logger = new fluent.FluentLogger(testPort, testHost, { bufferSize: 100 });

      process.nextTick(function() {
        for (var i = 0; i < 10; i++) {
          logger.post('test.fluent', { foo: "bar" });
        }

        logger.head.should.eql(0);
        logger.tail.should.eql(10);
        logger.queues.length.should.eql(10);
        done();
      });
    });

    it('should be rotated buffer if number of post is greater than buffer size', function(done) {
      var logger = new fluent.FluentLogger(testPort, testHost, { bufferSize: 10 });

      process.nextTick(function() {
        for (var i = 0; i < 15; i++) {
          logger.post('test.fluent', { foo: "bar" });
        }
        logger.head.should.eql(6);
        logger.tail.should.eql(5);
        logger.queues.length.should.eql(10);
        done();
      });
    });
  });

  describe('flush buffer', function() {
    beforeEach(function(done) {
      server.close();
      done();
    });

    it('should be flushed buffer after reconnected', function(done) {
      this.timeout(10000);
      var logger = new fluent.FluentLogger(testPort, testHost);

      logger.on('connect', function() {
        logger.head.should.eql(0);
        logger.tail.should.eql(0);
        done();
      });

      process.nextTick(function() {
        for (var i = 0; i < 10; i++) {
          logger.post('test.fluent', { foo: "bar" });
        }

        server.listen(testPort, function () {});
      });
    });
  });
});<commit_msg>Refactor unit test<commit_after>var root = __dirname + "/../";
var path = require('path');
var net = require('net');
var util = require('util');
var fluent = require(path.join(root, 'lib/FluentLogger'));
var should = require('should');

var testPort = 24242;
var testHost = '127.0.0.1';

describe('FluentLogger', function() {
  var server;

  beforeEach(function(done) {
    server = new net.Server();
    server.listen(testPort, function() {
      done();
    });
  });

  afterEach(function(done) {
    try { server.close(); } catch(e) {}
    done();
  });

  describe('new instance', function() {
    it('should connect fluentd server', function(done) {
      server.on('connection', function(socket) {
        done();
      });
      new fluent.FluentLogger(testPort, testHost);
    });
  });

  describe('#post()', function() {
    var logger;

    it('should post log', function(done) {
      server.on('connection', function(socket) {
        socket.setEncoding('utf8');
        socket.on('data', function(data) {
          var json = JSON.parse(data);
          json[0].should.eql('test.fluent');
          json[1].should.be.a('number');
          json[2].should.eql({ foo: "bar" });
          done();
        });
      });

      logger = new fluent.FluentLogger(testPort, testHost);
      logger.post('test.fluent', { foo: "bar" });
    });
  });

  describe('retry feature', function() {
    beforeEach(function(done) {
      server.close();
      done();
    });

    it('should be retry if connection was broken', function(done) {
      var logger = new fluent.FluentLogger(testPort, testHost);
      var count = 1;

      logger.on('connect', function() {
        done();
      });

      server.listen(testPort);
    });
  });

  describe('buffer feature', function() {
    beforeEach(function(done) {
      server.close();
      process.nextTick(function() {
        done();
      });
    });

    it('should be push buffer if connection was broken', function(done) {
      var logger = new fluent.FluentLogger(testPort, testHost, { bufferSize: 100 });

      for (var i = 0; i < 10; i++) {
        logger.post('test.fluent', { foo: "bar" });
      }

      logger.head.should.eql(0);
      logger.tail.should.eql(10);
      logger.queues.length.should.eql(10);
      done();
    });

    it('should be rotated buffer if number of post is greater than buffer size', function(done) {
      var logger = new fluent.FluentLogger(testPort, testHost, { bufferSize: 10 });

      for (var i = 0; i < 15; i++) {
        logger.post('test.fluent', { foo: "bar" });
      }

      logger.head.should.eql(6);
      logger.tail.should.eql(5);
      logger.queues.length.should.eql(10);
      done();
    });
  });

  describe('flush buffer', function() {
    beforeEach(function(done) {
      server.close();
      process.nextTick(function() {
        done();
      });
    });

    it('should be flushed buffer after reconnected', function(done) {
      var logger = new fluent.FluentLogger(testPort, testHost);

      logger.on('connect', function() {
        logger.head.should.eql(0);
        logger.tail.should.eql(0);
        done();
      });

      for (var i = 0; i < 10; i++) {
        logger.post('test.fluent', { foo: "bar" });
      }

      server.listen(testPort, function () {});
    });
  });
});
{'fraction_non_alphanumeric': 0.1768974024089392, 'fraction_numerical': 0.013931214627775359, 'mean_word_length': 2.461577096936213, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7455402', 'n_tokens_mistral': 2233, 'n_tokens_neox': 2125, 'n_words': 446}