Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eric Koleda
committed
Mar 10, 2018
1 parent
08d9d45
commit d8f4df8
Showing
8 changed files
with
61 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,44 @@ | ||
/** | ||
* @file Mocks out Apps Script's LockService.Lock, using the fibers library to | ||
* emulate concurrent executions. Like Apps Script's locks, only one execution | ||
* can hold the lock at a time. | ||
*/ | ||
|
||
var Fiber = require('fibers'); | ||
|
||
var locked = false; | ||
var waitingFibers = []; | ||
|
||
var MockLock = function() { | ||
this.gotLock = false; | ||
this.hasLock_ = false; | ||
this.id = Math.random(); | ||
}; | ||
|
||
MockLock.prototype.waitLock = function(timeoutInMillis) { | ||
var start = new Date(); | ||
do { | ||
if (!locked || this.gotLock) { | ||
if (!locked || this.hasLock_) { | ||
locked = true; | ||
this.gotLock = true; | ||
this.hasLock_ = true; | ||
return; | ||
} else { | ||
waitingFibers.push(Fiber.current); | ||
Fiber.yield(); | ||
} | ||
} while (timeDiffInMillis(new Date(), start) < timeoutInMillis); | ||
} while (new Date().getTime() - start.getTime() < timeoutInMillis); | ||
throw new Error('Unable to get lock'); | ||
}; | ||
|
||
MockLock.prototype.releaseLock = function() { | ||
locked = false; | ||
this.gotLock = false; | ||
this.hasLock_ = false; | ||
if (waitingFibers.length) { | ||
waitingFibers.pop().run(); | ||
} | ||
}; | ||
|
||
MockLock.prototype.hasLock = function() { | ||
return this.gotLock; | ||
return this.hasLock_; | ||
}; | ||
|
||
function timeDiffInMillis(a, b) { | ||
return a.getTime() - b.getTime(); | ||
} | ||
|
||
module.exports = MockLock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters