<commit_before>sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/LockManager.js"
SandboxedModule = require('sandboxed-module')

describe "LockManager", ->
	beforeEach ->
		@Settings = 		
			redis:
				web:{}
		@LockManager = SandboxedModule.require modulePath, requires:
			"redis-sharelatex":
				createClient: () => @rclient =
					auth: sinon.stub()
			"settings-sharelatex": @Settings

		@key = "lock-key"
		@callback = sinon.stub()

	describe "checkLock", ->
		describe "when the lock is taken", ->
			beforeEach ->
				@rclient.exists = sinon.stub().callsArgWith(1, null, "1")
				@LockManager.checkLock @key, @callback

			it "should check the lock in redis", ->
				@rclient.exists
					.calledWith(@key)
					.should.equal true

			it "should return the callback with false", ->
				@callback.calledWith(null, false).should.equal true

		describe "when the lock is free", ->
			beforeEach ->
				@rclient.exists = sinon.stub().callsArgWith(1, null, "0")
				@LockManager.checkLock @key, @callback

			it "should return the callback with true", ->
				@callback.calledWith(null, true).should.equal true


	describe "tryLock", ->
		describe "when the lock is taken", ->
			beforeEach ->
				@rclient.set = sinon.stub().callsArgWith(5, null, null)
				@LockManager.randomLock = sinon.stub().returns "locked-random-value"
				@LockManager.tryLock @key, @callback

			it "should check the lock in redis", ->
				@rclient.set
					.calledWith(@key, "locked-random-value", "EX", @LockManager.LOCK_TTL, "NX")
					.should.equal true

			it "should return the callback with false", ->
				@callback.calledWith(null, false).should.equal true

		describe "when the lock is free", ->
			beforeEach ->
				@rclient.set = sinon.stub().callsArgWith(5, null, "OK")
				@LockManager.tryLock @key, @callback

			it "should return the callback with true", ->
				@callback.calledWith(null, true).should.equal true

	describe "deleteLock", ->
		beforeEach -> 
			beforeEach ->
				@rclient.del = sinon.stub().callsArg(1)
				@LockManager.deleteLock @key, @callback

			it "should delete the lock in redis", ->
				@rclient.del
					.calledWith(key)
					.should.equal true

			it "should call the callback", ->
				@callback.called.should.equal true

	describe "getLock", ->
		describe "when the lock is not taken", ->
			beforeEach (done) ->
				@LockManager.tryLock = sinon.stub().callsArgWith(1, null, true)
				@LockManager.getLock @key, (args...) =>
					@callback(args...)
					done()

			it "should try to get the lock", ->
				@LockManager.tryLock
					.calledWith(@key)
					.should.equal true

			it "should only need to try once", ->
				@LockManager.tryLock.callCount.should.equal 1

			it "should return the callback", ->
				@callback.calledWith(null).should.equal true

		describe "when the lock is initially set", ->
			beforeEach (done) ->
				startTime = Date.now()
				@LockManager.LOCK_TEST_INTERVAL = 5
				@LockManager.tryLock = (doc_id, callback = (error, isFree) ->) ->
					if Date.now() - startTime < 20
						callback null, false
					else
						callback null, true
				sinon.spy @LockManager, "tryLock"

				@LockManager.getLock @key, (args...) =>
					@callback(args...)
					done()<commit_msg>increase timeout in lock manager test<commit_after>sinon = require('sinon')
chai = require('chai')
should = chai.should()
expect = chai.expect
modulePath = "../../../../app/js/LockManager.js"
SandboxedModule = require('sandboxed-module')

describe "LockManager", ->
	beforeEach ->
		@Settings = 		
			redis:
				web:{}
		@LockManager = SandboxedModule.require modulePath, requires:
			"redis-sharelatex":
				createClient: () => @rclient =
					auth: sinon.stub()
			"settings-sharelatex": @Settings

		@key = "lock-key"
		@callback = sinon.stub()

	describe "checkLock", ->
		describe "when the lock is taken", ->
			beforeEach ->
				@rclient.exists = sinon.stub().callsArgWith(1, null, "1")
				@LockManager.checkLock @key, @callback

			it "should check the lock in redis", ->
				@rclient.exists
					.calledWith(@key)
					.should.equal true

			it "should return the callback with false", ->
				@callback.calledWith(null, false).should.equal true

		describe "when the lock is free", ->
			beforeEach ->
				@rclient.exists = sinon.stub().callsArgWith(1, null, "0")
				@LockManager.checkLock @key, @callback

			it "should return the callback with true", ->
				@callback.calledWith(null, true).should.equal true


	describe "tryLock", ->
		describe "when the lock is taken", ->
			beforeEach ->
				@rclient.set = sinon.stub().callsArgWith(5, null, null)
				@LockManager.randomLock = sinon.stub().returns "locked-random-value"
				@LockManager.tryLock @key, @callback

			it "should check the lock in redis", ->
				@rclient.set
					.calledWith(@key, "locked-random-value", "EX", @LockManager.LOCK_TTL, "NX")
					.should.equal true

			it "should return the callback with false", ->
				@callback.calledWith(null, false).should.equal true

		describe "when the lock is free", ->
			beforeEach ->
				@rclient.set = sinon.stub().callsArgWith(5, null, "OK")
				@LockManager.tryLock @key, @callback

			it "should return the callback with true", ->
				@callback.calledWith(null, true).should.equal true

	describe "deleteLock", ->
		beforeEach -> 
			beforeEach ->
				@rclient.del = sinon.stub().callsArg(1)
				@LockManager.deleteLock @key, @callback

			it "should delete the lock in redis", ->
				@rclient.del
					.calledWith(key)
					.should.equal true

			it "should call the callback", ->
				@callback.called.should.equal true

	describe "getLock", ->
		describe "when the lock is not taken", ->
			beforeEach (done) ->
				@LockManager.tryLock = sinon.stub().callsArgWith(1, null, true)
				@LockManager.getLock @key, (args...) =>
					@callback(args...)
					done()

			it "should try to get the lock", ->
				@LockManager.tryLock
					.calledWith(@key)
					.should.equal true

			it "should only need to try once", ->
				@LockManager.tryLock.callCount.should.equal 1

			it "should return the callback", ->
				@callback.calledWith(null).should.equal true

		describe "when the lock is initially set", ->
			beforeEach (done) ->
				startTime = Date.now()
				@LockManager.LOCK_TEST_INTERVAL = 5
				@LockManager.tryLock = (doc_id, callback = (error, isFree) ->) ->
					if Date.now() - startTime < 100
						callback null, false
					else
						callback null, true
				sinon.spy @LockManager, "tryLock"

				@LockManager.getLock @key, (args...) =>
					@callback(args...)
					done()
{'fraction_non_alphanumeric': 0.14577966358539174, 'fraction_numerical': 0.003788452795878163, 'mean_word_length': 3.0791100123609394, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 75, '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': '5007350', 'n_tokens_mistral': 2595, 'n_tokens_neox': 2114, 'n_words': 550}