<commit_before>/*
 * client-reconnect-test.js: Basic tests for the reconnection of net client.
 *
 * (C) 2013, Nodejitsu Inc.
 *
 */

var assert = require('assert'),
    vows = require('vows'),
    async = require('utile').async,
    godot = require('../../lib/godot'),
    helpers = require('../helpers'),
    macros = require('../macros'),
    mocks = require('../mocks');

vows.describe('godot/net/client').addBatch({
  "Godot client": {
    "with no backoff and no server": {
      topic: function () {
        var callback = this.callback,
            port = helpers.nextPort;

        var client = godot.createClient({
          type: 'tcp',
          producers: [
            godot.producer(helpers.fixtures['producer-test'])
          ]
        });

        client.connect(port);
        client.on('error', function (err) {
          callback(null, err);
        });
      },
      "should emit an error": function (_, err) {
        assert(err);
        assert.instanceOf(err, Error);
      }
    },
    "with backoff and no server": {
      topic: function () {
        var callback = this.callback,
            port = helpers.nextPort,
            d = new Date();

        var client = godot.createClient({
          type: 'tcp',
          producers: [
            godot.producer(helpers.fixtures['producer-test'])
          ],
          reconnect: {
            type: 'exponential',
            maxTries: 2,
            initialDelay: 100,
            maxDelay: 300
          }
        });

        client.connect(port);
        client.on('error', function (err) {
          callback(null, err, (new Date() - d));
        });
      },
      "should emit an error": function (_, err) {
        assert(err);
        assert.instanceOf(err, Error);
      },
      "should take appropiate amount of time": function (_, err, t) {
        assert(t >= 300);
      }
    },
    "with backoff and server eventually coming up": {
      topic: function () {
        var callback = this.callback,
            port = helpers.nextPort,
            d = new Date();

        var client = godot.createClient({
          type: 'tcp',
          producers: [
            godot.producer(helpers.fixtures['producer-test'])
          ],
          reconnect: {
            type: 'exponential',
            maxTries: 2,
            initialDelay: 100,
            maxDelay: 300
          }
        });

        client.connect(port);
        client.on('error', function (err) {
          throw err;
        });

        setTimeout(function () {
          mocks.net.createServer({ type: 'tcp', port: port }, function (err, server) {
            if (err) {
              throw err;
            }

            server.once('data', function (data) {
              callback(null, data, (new Date()) - d);
            });
          });
        }, 100);
      },
      "should send data": function (err, data) {
        assert(!err);
        assert(data);
      },
      "should take appropiate amount of time": function (_, err, t) {
        assert(t >= 200);
      }
    }<commit_msg>change timeout to upper bound to avoid possible race condition<commit_after>/*
 * client-reconnect-test.js: Basic tests for the reconnection of net client.
 *
 * (C) 2013, Nodejitsu Inc.
 *
 */

var assert = require('assert'),
    vows = require('vows'),
    async = require('utile').async,
    godot = require('../../lib/godot'),
    helpers = require('../helpers'),
    macros = require('../macros'),
    mocks = require('../mocks');

vows.describe('godot/net/client').addBatch({
  "Godot client": {
    "with no backoff and no server": {
      topic: function () {
        var callback = this.callback,
            port = helpers.nextPort;

        var client = godot.createClient({
          type: 'tcp',
          producers: [
            godot.producer(helpers.fixtures['producer-test'])
          ]
        });

        client.connect(port);
        client.on('error', function (err) {
          callback(null, err);
        });
      },
      "should emit an error": function (_, err) {
        assert(err);
        assert.instanceOf(err, Error);
      }
    },
    "with backoff and no server": {
      topic: function () {
        var callback = this.callback,
            port = helpers.nextPort,
            d = new Date();

        var client = godot.createClient({
          type: 'tcp',
          producers: [
            godot.producer(helpers.fixtures['producer-test'])
          ],
          reconnect: {
            type: 'exponential',
            maxTries: 2,
            initialDelay: 100,
            maxDelay: 300
          }
        });

        client.connect(port);
        client.on('error', function (err) {
          callback(null, err, (new Date() - d));
        });
      },
      "should emit an error": function (_, err) {
        assert(err);
        assert.instanceOf(err, Error);
      },
      "should take appropiate amount of time": function (_, err, t) {
        assert(t >= 300);
      }
    },
    "with backoff and server eventually coming up": {
      topic: function () {
        var callback = this.callback,
            port = helpers.nextPort,
            d = new Date();

        var client = godot.createClient({
          type: 'tcp',
          producers: [
            godot.producer(helpers.fixtures['producer-test'])
          ],
          reconnect: {
            type: 'exponential',
            maxTries: 2,
            initialDelay: 100,
            maxDelay: 300
          }
        });

        client.connect(port);
        client.on('error', function (err) {
          throw err;
        });

        setTimeout(function () {
          mocks.net.createServer({ type: 'tcp', port: port }, function (err, server) {
            if (err) {
              throw err;
            }

            server.once('data', function (data) {
              callback(null, data, (new Date()) - d);
            });
          });
        }, 300);
      },
      "should send data": function (err, data) {
        assert(!err);
        assert(data);
      },
      "should take appropiate amount of time": function (_, err, t) {
        assert(t >= 200);
      }
    }
{'fraction_non_alphanumeric': 0.14195430238210988, 'fraction_numerical': 0.008750607681088965, 'mean_word_length': 1.5954583683767871, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '107326', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1738, 'n_words': 423}