<commit_before>/*global describe:true, it:true, before:true, after:true */

var
	should    = require('chai').should(),
	fivebeans = require('../index'),
	fs        = require('fs'),
	semver    = require('semver')
	;

var host = '127.0.0.1';
var port = 11300;
var tube = 'testtube';

function readTestImage()
{
	return fs.readFileSync('./test/test.png');
}

describe('FiveBeansClient', function()
{
	var producer, consumer, testjobid;
	var version;

	before(function()
	{
		producer = new fivebeans.client(host);
		consumer = new fivebeans.client(host, port);
	});

	describe('#FiveBeansClient()', function()
	{
		it('creates a client with the passed-in options', function()
		{
			producer.host.should.equal(host);
			producer.port.should.equal(port);
		});
	});

	describe('#connect()', function()
	{
		it('creates and saves a connection', function(done)
		{
			producer.on('connect', function()
			{
				producer.stream.should.be.ok;
				done();

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

	describe('job producer:', function()
	{
		it('#use() connects to a specific tube', function(done)
		{
			producer.use(tube, function(err, response)
			{
				should.not.exist(err);
				response.should.equal(tube);
				done();
			});
		});

		it('#list_tube_used() returns the tube used by a producer', function(done)
		{
			producer.list_tube_used(function(err, response)
			{
				should.not.exist(err);
				response.should.equal(tube);
				done();
			});
		});

		it('#put() submits a job', function(done)
		{
			var data = { type: 'test', payload: 'the explosive energy of the warhead of a missile or of the bomb load  of an aircraft' };
			producer.put(0, 0, 60, JSON.stringify(data), function(err, jobid)
			{
				should.not.exist(err);
				jobid.should.exist;
				done();
			});
		});

		after(function(done)
		{
			producer.stats(function(err, response)
			{
				if (response.version)
					version = response.version + '.0';
				done();
			});

		});
	});

	describe('job consumer:', function()
	{
		it('#watch() watches a tube', function(done)
		{
			consumer.on('connect', function()
			{
				consumer.watch(tube, function(err, response)
				{
					should.not.exist(err);
					response.should.equal('2');
					done();
				});
			}).on('error', function(err)
			{
				throw(err);
			});
			consumer.connect();
		});

		it('#ignore() ignores a tube', function(done)
		{
			consumer.ignore('default', function(err, response)
			{
				should.not.exist(err);
				response.should.equal('1');
				done();
			});
		});

		it('#list_tubes_watched() returns the tubes the consumer watches', function(done)
		{
			consumer.list_tubes_watched(function(err, response)
			{
				should.not.exist(err);
				response.length.should.equal(1);
				response.indexOf(tube).should.equal(0);
				done();
			});
		});

		it('#peek_ready() peeks ahead at jobs', function(done)
		{
			this.timeout(4000);
			producer.peek_ready(function(err, jobid, payload)
			{
				should.not.exist(err);
				jobid.should.exist;
				testjobid = jobid;
				var parsed = JSON.parse(payload);
				parsed.should.have.property('type');
				parsed.type.should.equal('test');
				done();
			});
		});

		it('#stats_job() returns job stats', function(done)
		{
			consumer.stats_job(testjobid, function(err, response)
			{
				response.should.be.a('object');
				response.should.have.property('id');
				response.id.should.equal(parseInt(testjobid, 10));
				response.tube.should.equal(tube);
				done();
			});
		});

		it('#reserve() returns a job', function(done)
		{
			consumer.reserve(function(err, jobid, payload)
			{
				should.not.exist(err);
				jobid.should.equal(testjobid);
				var parsed = JSON.parse(payload);
				parsed.should.have.property('type');
				parsed.type.should.equal('test');
				done();
			});
		});

		it('#touch() informs the server the client is still working', function(done)
		{
			consumer.touch(testjobid, function(err)<commit_msg>Add a test to attempt to reproduce the behavior described in issue #24. This test passes, so I haven't figured out what the root cause of the problem is yet.<commit_after>/*global describe:true, it:true, before:true, after:true */

var
	should    = require('chai').should(),
	fivebeans = require('../index'),
	fs        = require('fs'),
	semver    = require('semver')
	;

var host = '127.0.0.1';
var port = 11300;
var tube = 'testtube';

function readTestImage()
{
	return fs.readFileSync('./test/test.png');
}

describe('FiveBeansClient', function()
{
	var producer, consumer, testjobid;
	var version;

	before(function()
	{
		producer = new fivebeans.client(host);
		consumer = new fivebeans.client(host, port);
	});

	describe('#FiveBeansClient()', function()
	{
		it('creates a client with the passed-in options', function()
		{
			producer.host.should.equal(host);
			producer.port.should.equal(port);
		});
	});

	describe('#connect()', function()
	{
		it('creates and saves a connection', function(done)
		{
			producer.on('connect', function()
			{
				producer.stream.should.be.ok;
				done();

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

	describe('job producer:', function()
	{
		it('#use() connects to a specific tube', function(done)
		{
			producer.use(tube, function(err, response)
			{
				should.not.exist(err);
				response.should.equal(tube);
				done();
			});
		});

		it('#list_tube_used() returns the tube used by a producer', function(done)
		{
			producer.list_tube_used(function(err, response)
			{
				should.not.exist(err);
				response.should.equal(tube);
				done();
			});
		});

		it('#put() submits a job', function(done)
		{
			var data = { type: 'test', payload: 'the explosive energy of the warhead of a missile or of the bomb load  of an aircraft' };
			producer.put(0, 0, 60, JSON.stringify(data), function(err, jobid)
			{
				should.not.exist(err);
				jobid.should.exist;
				done();
			});
		});

		after(function(done)
		{
			producer.stats(function(err, response)
			{
				if (response.version)
					version = response.version + '.0';
				done();
			});

		});
	});

	describe('job consumer:', function()
	{
		it('#watch() watches a tube', function(done)
		{
			consumer.on('connect', function()
			{
				consumer.watch(tube, function(err, response)
				{
					should.not.exist(err);
					response.should.equal('2');
					done();
				});
			}).on('error', function(err)
			{
				throw(err);
			});
			consumer.connect();
		});

		it('#ignore() ignores a tube', function(done)
		{
			consumer.ignore('default', function(err, response)
			{
				should.not.exist(err);
				response.should.equal('1');
				done();
			});
		});

		it('#list_tubes_watched() returns the tubes the consumer watches', function(done)
		{
			consumer.list_tubes_watched(function(err, response)
			{
				should.not.exist(err);
				response.length.should.equal(1);
				response.indexOf(tube).should.equal(0);
				done();
			});
		});

		it('#peek_ready() peeks ahead at jobs', function(done)
		{
			this.timeout(4000);
			producer.peek_ready(function(err, jobid, payload)
			{
				should.not.exist(err);
				jobid.should.exist;
				testjobid = jobid;
				var parsed = JSON.parse(payload);
				parsed.should.have.property('type');
				parsed.type.should.equal('test');
				done();
			});
		});

		it('#stats_job() returns job stats', function(done)
		{
			consumer.stats_job(testjobid, function(err, response)
			{
				response.should.be.a('object');
				response.should.have.property('id');
				response.id.should.equal(parseInt(testjobid, 10));
				response.tube.should.equal(tube);
				done();
			});
		});

		it('consumer can run stats_job() while a job is reserved', function(done)
		{
			consumer.reserve(function(err, jobid, payload)
			{
				should.not.exist(err);
				consumer.stats_job(jobid, function(err, res)
				{
					should.not.exist(err);
					res.should.be.an('object');
					res.should.have.property('id');
					res.id.should.equal(parseInt(jobid, 10));
					res.state.should.equal('reserved');
					consumer.release(jobid, 1, 1, function(err)
					{
						should.not.exist(err);
						done();
					});
				});
			});
		});

		it('#reserve() returns a job', function(done)
		{
			consumer.reserve(function(err, jobid, payload)
			{
				should.not.exist(err);
				jobid.should.equal(testjobid);
				var parsed = JSON.parse(payload);
				parsed.should.have.property('type');
				parsed.type.should.equal('test');
				done();
			});
		});

		it('#touch() informs the server the client is still working', function(done)
		{
			consumer.touch(testjobid, function(err)
{'fraction_non_alphanumeric': 0.1751596053395241, 'fraction_numerical': 0.006732443412652351, 'mean_word_length': 3.342741935483871, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1958974', 'n_tokens_mistral': 3629, 'n_tokens_neox': 2889, 'n_words': 563}