<commit_before>var assert = require("assert"),
    should = require("should"),
    nsqStreams = require("../");

describe("Writer", function () {
    var writer,
        reader,
        payload = { test_data: 1234 };

    it("should emit an error when nsqd is not available", function (done) {
        writer = new nsqStreams.Writer("localhost", 10000);

        writer.on("error", function (error) {
            done();
        });
    });

    it("should emit ready when connected to nsqd", function (done) {
        writer = new nsqStreams.Writer("localhost", 4150);

        writer.on("ready", function () {
            done();
        });
    });

    describe("#write", function () {

        beforeEach(function (done) {
            writer.write("test_topic");
            writer.write(payload);

            reader = new nsqStreams.Reader("test_topic", "test_channel", { lookupdHTTPAddresses: "localhost:4161" });
            reader.on("nsqd_connected", function () {
                done();
            });

            reader.on("error", function (error) {
                done(error);
            });
        });

        it("should send a payload", function (done) {
            reader.on("readable", function () {
                var data = reader.read();

                should(data).not.be.null.and.be.a.string;

                data = JSON.parse(data);

                assert.deepEqual(data, payload);

                reader.close();

                done();
            });
        });
    });

    describe("#close", function () {

        it("should close the nsqd connection", function (done) {
            writer.close();
            writer.on("close", function () {
                done();
            });
        });
    });
});

describe("Reader", function () {
    var writer,
        reader,
        payload = { test_data: 1234 };

    describe("#read", function () {

        beforeEach(function (done) {
            writer = new nsqStreams.Writer("localhost", 4150);

            writer.on("ready", function () {
                writer.write("test_topic");
                writer.write(payload);

                reader = new nsqStreams.Reader("test_topic", "test_channel", { lookupdHTTPAddresses: "localhost:4161" });

                reader.on("nsqd_connected", function () {
                    done();
                });
            });
        });

        it("should receive the payload", function (done) {
            reader.on("readable", function () {
                var data = reader.read();

                should(data).not.be.null.and.be.a.string;

                data = JSON.parse(data);

                assert.deepEqual(data, payload);

                // Reader must be closed, otherwise done might be called more
                // than once which is an error.
                reader.close();

                done();
            });
        });

    });
});
<commit_msg>Allow to setting NSQ host and port parameters using env<commit_after>var assert = require("assert"),
    should = require("should"),
    nsqStreams = require("../");

var NSQD_HOST = process.env.NSQD_HOST || "localhost",
    NSQD_PORT = process.env.NSQD_PORT || 4150,
    NSQLOOKUPD_HOST = process.env.NSQLOOKUPD_HOST || "localhost",
    NSQLOOKUPD_PORT = process.env.NSQLOOKUPD_PORT || 4161;

describe("Writer", function () {
    var writer,
        reader,
        payload = { test_data: 1234 };

    it("should emit an error when nsqd is not available", function (done) {
        writer = new nsqStreams.Writer("localhost", 65535);

        writer.on("error", function (error) {
            done();
        });
    });

    it("should emit ready when connected to nsqd", function (done) {
        writer = new nsqStreams.Writer(NSQD_HOST, NSQD_PORT);

        writer.on("ready", function () {
            done();
        });
    });

    describe("#write", function () {
        writer = new nsqStreams.Writer(NSQD_HOST, NSQD_PORT);

        beforeEach(function (done) {
            writer.write("test_topic");
            writer.write(payload);

            reader = new nsqStreams.Reader("test_topic", "test_channel", { lookupdHTTPAddresses: NSQLOOKUPD_HOST + ":" + NSQLOOKUPD_PORT });
            reader.on("nsqd_connected", function () {
                console.log("nsqd_connected");
                done();
            });

            reader.on("error", function (error) {
                done(error);
            });
        });

        it("should send a payload", function (done) {
            reader.on("readable", function () {
                var data = reader.read();

                should(data).not.be.null.and.be.a.string;

                data = JSON.parse(data);

                assert.deepEqual(data, payload);

                reader.close();

                done();
            });
        });
    });

    describe("#close", function () {

        it("should close the nsqd connection", function (done) {
            writer.close();
            writer.on("close", function () {
                done();
            });
        });
    });
});

describe("Reader", function () {
    var writer,
        reader,
        payload = { test_data: 1234 };

    describe("#read", function () {

        beforeEach(function (done) {
            writer = new nsqStreams.Writer(NSQD_HOST, NSQD_PORT);

            writer.on("ready", function () {
                writer.write("test_topic");
                writer.write(payload);

                reader = new nsqStreams.Reader("test_topic", "test_channel", { lookupdHTTPAddresses: NSQLOOKUPD_HOST + ":" + NSQLOOKUPD_PORT });

                reader.on("nsqd_connected", function () {
                    done();
                });
            });
        });

        it("should receive the payload", function (done) {
            reader.on("readable", function () {
                var data = reader.read();

                should(data).not.be.null.and.be.a.string;

                data = JSON.parse(data);

                assert.deepEqual(data, payload);

                // Reader must be closed, otherwise done might be called more
                // than once which is an error.
                reader.close();

                done();
            });
        });

    });
});
{'fraction_non_alphanumeric': 0.14151394422310756, 'fraction_numerical': 0.00796812749003984, 'mean_word_length': 1.6559458315700382, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '1299880', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1639, 'n_words': 355}