| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /* eslint-disable
- camelcase,
- handle-callback-err,
- mocha/no-nested-tests,
- no-return-assign,
- no-undef,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
- * DS102: Remove unnecessary code created because of implicit returns
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon');
- const chai = require('chai');
- const should = chai.should();
- const { expect } = chai;
- const modulePath = "../../../../app/js/LockManager.js";
- const SandboxedModule = require('sandboxed-module');
- describe("LockManager", function() {
- beforeEach(function() {
- this.Settings = {
- redis: {
- lock:{}
- }
- };
- this.LockManager = SandboxedModule.require(modulePath, { requires: {
- "redis-sharelatex": {
- createClient: () => { return this.rclient =
- {auth: sinon.stub()}; }
- },
- "settings-sharelatex": this.Settings,
- "logger-sharelatex": {error() {}}
- }
- });
- this.key = "lock-key";
- return this.callback = sinon.stub();
- });
- describe("checkLock", function() {
- describe("when the lock is taken", function() {
- beforeEach(function() {
- this.rclient.exists = sinon.stub().callsArgWith(1, null, "1");
- return this.LockManager.checkLock(this.key, this.callback);
- });
- it("should check the lock in redis", function() {
- return this.rclient.exists
- .calledWith(this.key)
- .should.equal(true);
- });
- return it("should return the callback with false", function() {
- return this.callback.calledWith(null, false).should.equal(true);
- });
- });
- return describe("when the lock is free", function() {
- beforeEach(function() {
- this.rclient.exists = sinon.stub().callsArgWith(1, null, "0");
- return this.LockManager.checkLock(this.key, this.callback);
- });
- return it("should return the callback with true", function() {
- return this.callback.calledWith(null, true).should.equal(true);
- });
- });
- });
- describe("tryLock", function() {
- describe("when the lock is taken", function() {
- beforeEach(function() {
- this.rclient.set = sinon.stub().callsArgWith(5, null, null);
- this.LockManager.randomLock = sinon.stub().returns("locked-random-value");
- return this.LockManager.tryLock(this.key, this.callback);
- });
- it("should check the lock in redis", function() {
- return this.rclient.set
- .calledWith(this.key, "locked-random-value", "EX", this.LockManager.LOCK_TTL, "NX")
- .should.equal(true);
- });
- return it("should return the callback with false", function() {
- return this.callback.calledWith(null, false).should.equal(true);
- });
- });
- return describe("when the lock is free", function() {
- beforeEach(function() {
- this.rclient.set = sinon.stub().callsArgWith(5, null, "OK");
- return this.LockManager.tryLock(this.key, this.callback);
- });
- return it("should return the callback with true", function() {
- return this.callback.calledWith(null, true).should.equal(true);
- });
- });
- });
- describe("deleteLock", function() { return beforeEach(function() {
- beforeEach(function() {
- this.rclient.del = sinon.stub().callsArg(1);
- return this.LockManager.deleteLock(this.key, this.callback);
- });
- it("should delete the lock in redis", function() {
- return this.rclient.del
- .calledWith(key)
- .should.equal(true);
- });
- return it("should call the callback", function() {
- return this.callback.called.should.equal(true);
- });
- }); }
- );
- describe("getLock", function() {
- describe("when the lock is not taken", function() {
- beforeEach(function(done) {
- this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, true);
- return this.LockManager.getLock(this.key, (...args) => {
- this.callback(...Array.from(args || []));
- return done();
- });
- });
- it("should try to get the lock", function() {
- return this.LockManager.tryLock
- .calledWith(this.key)
- .should.equal(true);
- });
- it("should only need to try once", function() {
- return this.LockManager.tryLock.callCount.should.equal(1);
- });
- return it("should return the callback", function() {
- return this.callback.calledWith(null).should.equal(true);
- });
- });
- describe("when the lock is initially set", function() {
- beforeEach(function(done) {
- const startTime = Date.now();
- this.LockManager.LOCK_TEST_INTERVAL = 5;
- this.LockManager.tryLock = function(doc_id, callback) {
- if (callback == null) { callback = function(error, isFree) {}; }
- if ((Date.now() - startTime) < 100) {
- return callback(null, false);
- } else {
- return callback(null, true);
- }
- };
- sinon.spy(this.LockManager, "tryLock");
- return this.LockManager.getLock(this.key, (...args) => {
- this.callback(...Array.from(args || []));
- return done();
- });
- });
- it("should call tryLock multiple times until free", function() {
- return (this.LockManager.tryLock.callCount > 1).should.equal(true);
- });
- return it("should return the callback", function() {
- return this.callback.calledWith(null).should.equal(true);
- });
- });
- return describe("when the lock times out", function() {
- beforeEach(function(done) {
- const time = Date.now();
- this.LockManager.MAX_LOCK_WAIT_TIME = 5;
- this.LockManager.tryLock = sinon.stub().callsArgWith(1, null, false);
- return this.LockManager.getLock(this.key, (...args) => {
- this.callback(...Array.from(args || []));
- return done();
- });
- });
- return it("should return the callback with an error", function() {
- return this.callback.calledWith(sinon.match.instanceOf(Error)).should.equal(true);
- });
- });
- });
- return describe("runWithLock", function() {
- describe("with successful run", function() {
- beforeEach(function() {
- this.runner = function(releaseLock) {
- if (releaseLock == null) { releaseLock = function(error) {}; }
- return releaseLock();
- };
- sinon.spy(this, "runner");
- this.LockManager.getLock = sinon.stub().callsArg(1);
- this.LockManager.releaseLock = sinon.stub().callsArg(2);
- return this.LockManager.runWithLock(this.key, this.runner, this.callback);
- });
- it("should get the lock", function() {
- return this.LockManager.getLock
- .calledWith(this.key)
- .should.equal(true);
- });
- it("should run the passed function", function() {
- return this.runner.called.should.equal(true);
- });
- it("should release the lock", function() {
- return this.LockManager.releaseLock
- .calledWith(this.key)
- .should.equal(true);
- });
- return it("should call the callback", function() {
- return this.callback.called.should.equal(true);
- });
- });
- describe("when the runner function returns an error", function() {
- beforeEach(function() {
- this.error = new Error("oops");
- this.runner = releaseLock => {
- if (releaseLock == null) { releaseLock = function(error) {}; }
- return releaseLock(this.error);
- };
- sinon.spy(this, "runner");
- this.LockManager.getLock = sinon.stub().callsArg(1);
- this.LockManager.releaseLock = sinon.stub().callsArg(2);
- return this.LockManager.runWithLock(this.key, this.runner, this.callback);
- });
- it("should release the lock", function() {
- return this.LockManager.releaseLock
- .calledWith(this.key)
- .should.equal(true);
- });
- return it("should call the callback with the error", function() {
- return this.callback.calledWith(this.error).should.equal(true);
- });
- });
- return describe("releaseLock", function() {
- describe("when the lock is current", function() {
- beforeEach(function() {
- this.rclient.eval = sinon.stub().yields(null, 1);
- return this.LockManager.releaseLock(this.key, this.lockValue, this.callback);
- });
- it('should clear the data from redis', function() {
- return this.rclient.eval.calledWith(this.LockManager.unlockScript, 1, this.key, this.lockValue).should.equal(true);
- });
- return it('should call the callback', function() {
- return this.callback.called.should.equal(true);
- });
- });
- return describe("when the lock has expired", function() {
- beforeEach(function() {
- this.rclient.eval = sinon.stub().yields(null, 0);
- return this.LockManager.releaseLock(this.key, this.lockValue, this.callback);
- });
- return it('should return an error if the lock has expired', function() {
- return this.callback.calledWith(sinon.match.has('message', "tried to release timed out lock")).should.equal(true);
- });
- });
- });
- });
- });
|