<commit_before>'use strict';

var assert = require('chai').assert;
var client = require('redis').createClient();
var Redlock = require('./redlock');

var redlock = new Redlock({
	retryCount: 2,
	retryDelay: 150
}, client);

var resource = 'Redlock:test:resource';

describe('Redlock', function(){

	before(function(done){
		client.del(resource, done);
	});

	var one;
	it('should lock a resource', function(done){
		redlock.lock(resource, 200, function(err, lock){
			if(err) throw err;
			assert.isObject(lock);
			assert.isAbove(lock.expiration, Date.now()-1);
			one = lock;
			done();
		});
	});

	var two;
	var two_expiration;
	it('should wait until a lock expires before issuing another lock', function(done){
		assert(one, 'Could not run because a required previous test failed.');
		redlock.lock(resource, 800, function(err, lock){
			if(err) throw err;
			assert.isObject(lock);
			assert.isAbove(lock.expiration, Date.now()-1);
			assert.isAbove(Date.now()+1, one.expiration);
			two = lock;
			two_expiration = lock.expiration;
			done();
		});
	});

	it('should unlock a resource', function(done){
		assert(two, 'Could not run because a required previous test failed.');
		two.unlock(done);
	});

	var three;
	it('should issue another lock immediately after a resource is unlocked', function(done){
		assert(two_expiration, 'Could not run because a required previous test failed.');
		redlock.lock(resource, 800, function(err, lock){
			if(err) throw err;
			assert.isObject(lock);
			assert.isAbove(lock.expiration, Date.now()-1);
			assert.isBelow(Date.now()-1, two_expiration);
			three = lock;
			done();
		});
	});

	var four;
	it('should extend an unexpired lock', function(done){
		assert(three, 'Could not run because a required previous test failed.');
		three.extend(800, function(err, lock){
			if(err) throw err;
			assert.isObject(lock);
			assert.isAbove(lock.expiration, Date.now()-1);
			assert.isAbove(lock.expiration, three.expiration);
			four = lock;
			done();
		});
	});

	it('should fail after the maximum retry count is exceeded', function(done){
		assert(four, 'Could not run because a required previous test failed.');
		redlock.lock(resource, 200, function(err, lock){
			assert.isNotNull(err);
			assert.equal(err.name, 'LockError');
			done();
		});
	});

	it('should fail to extend an expired lock', function(done){
		assert(four, 'Could not run because a required previous test failed.');
		setTimeout(function(){<commit_msg>One more test case<commit_after>'use strict';

var assert = require('chai').assert;
var client = require('redis').createClient();
var Redlock = require('./redlock');

var redlock = new Redlock({
	retryCount: 2,
	retryDelay: 150
}, client);

var resource = 'Redlock:test:resource';

describe('Redlock', function(){

	before(function(done){
		client.del(resource, done);
	});

	var one;
	it('should lock a resource', function(done){
		redlock.lock(resource, 200, function(err, lock){
			if(err) throw err;
			assert.isObject(lock);
			assert.isAbove(lock.expiration, Date.now()-1);
			one = lock;
			done();
		});
	});

	var two;
	var two_expiration;
	it('should wait until a lock expires before issuing another lock', function(done){
		assert(one, 'Could not run because a required previous test failed.');
		redlock.lock(resource, 800, function(err, lock){
			if(err) throw err;
			assert.isObject(lock);
			assert.isAbove(lock.expiration, Date.now()-1);
			assert.isAbove(Date.now()+1, one.expiration);
			two = lock;
			two_expiration = lock.expiration;
			done();
		});
	});

	it('should unlock a resource', function(done){
		assert(two, 'Could not run because a required previous test failed.');
		two.unlock(done);
	});

	var three;
	it('should issue another lock immediately after a resource is unlocked', function(done){
		assert(two_expiration, 'Could not run because a required previous test failed.');
		redlock.lock(resource, 800, function(err, lock){
			if(err) throw err;
			assert.isObject(lock);
			assert.isAbove(lock.expiration, Date.now()-1);
			assert.isBelow(Date.now()-1, two_expiration);
			three = lock;
			done();
		});
	});

	var four;
	it('should extend an unexpired lock', function(done){
		assert(three, 'Could not run because a required previous test failed.');
		three.extend(800, function(err, lock){
			if(err) throw err;
			assert.isObject(lock);
			assert.isAbove(lock.expiration, Date.now()-1);
			assert.isAbove(lock.expiration, three.expiration-1);
			four = lock;
			done();
		});
	});

	it('should fail after the maximum retry count is exceeded', function(done){
		assert(four, 'Could not run because a required previous test failed.');
		redlock.lock(resource, 200, function(err, lock){
			assert.isNotNull(err);
			assert.equal(err.name, 'LockError');
			done();
		});
	});

	it('should fail to extend an expired lock', function(done){
		assert(four, 'Could not run because a required previous test failed.');
		setTimeout(function(){
{'fraction_non_alphanumeric': 0.15112279991907748, 'fraction_numerical': 0.010317620878009305, 'mean_word_length': 4.487236403995561, 'pattern_counts': {'":': 0, '<': 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': '1573890', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1569, 'n_words': 448}