From d1618d84639fb2cca6adb5b25e9c8fc365dc1d1b Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 3 Apr 2015 12:23:55 -0300 Subject: [PATCH] unit test for lock --- test/lock.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/lock.js diff --git a/test/lock.js b/test/lock.js new file mode 100644 index 0000000..cd7edeb --- /dev/null +++ b/test/lock.js @@ -0,0 +1,47 @@ +'use strict'; + +var _ = require('lodash'); +var chai = require('chai'); +var sinon = require('sinon'); +var should = chai.should(); +var Lock = require('../lib/lock'); + +describe('Lock', function() { + it('should lock tasks using the same token', function(done) { + var i = 0; + Lock.get('123', function(lock) { + i++; + setTimeout(function() { + lock.free(); + }, 2); + Lock.get('123', function(lock) { + i++; + lock.free(); + }); + }); + setTimeout(function() { + i.should.equal(1); + }, 1); + setTimeout(function() { + i.should.equal(2); + done(); + }, 3); + }); + it('should not lock tasks using different tokens', function(done) { + var i = 0; + Lock.get('123', function(lock) { + i++; + setTimeout(function() { + lock.free(); + }, 2); + Lock.get('456', function(lock) { + i++; + lock.free(); + }); + }); + setTimeout(function() { + i.should.equal(2); + done(); + }, 1); + }); +});