[Pkg-javascript-commits] [node-schlock] 01/02: Imported Upstream version 0.2.1
Thorsten Alteholz
alteholz at moszumanska.debian.org
Thu Feb 4 20:52:36 UTC 2016
This is an automated email from the git hooks/post-receive script.
alteholz pushed a commit to branch master
in repository node-schlock.
commit 2413e4b5cb636472307ad3bd30b562ff3fa2522c
Author: Thorsten Alteholz <debian at alteholz.de>
Date: Thu Feb 4 21:52:30 2016 +0100
Imported Upstream version 0.2.1
---
.gitignore | 19 +++
.travis.yml | 5 +
LICENSE | 202 +++++++++++++++++++++++++++++++
README.md | 151 +++++++++++++++++++++++
lib/schlock.js | 168 ++++++++++++++++++++++++++
package.json | 57 +++++++++
test/logging-test.js | 134 +++++++++++++++++++++
test/schlock-test.js | 333 +++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 1069 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..6f0e672
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,19 @@
+*~
+#*#
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+npm-debug.log
+
+
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..2ade581
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 0.8
+ - 0.10
+
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..88ce2c8
--- /dev/null
+++ b/README.md
@@ -0,0 +1,151 @@
+Schlock
+-------
+
+This is a poorly-crafted read-write lock system. It only works
+in-process, and doesn't lock things between processes.
+
+See also:
+
+> http://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock
+
+License
+=======
+
+Copyright 2012, E14N Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+> http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+Overview
+========
+
+The module exports a single constructor that I call `Schlock` but you might
+want to call something smarter.
+
+ var Schlock = require("schlock");
+
+You need to create an instance to use it. I guess you could do this if
+you had multiple sets of resources (files and cache elements, say)
+that might share the same name.
+
+ var fileSchlock = new Schlock();
+ var cacheSchlock = new Schlock();
+
+ // These won't block each other
+
+ fileSchlock.writeLock("foo", ...);
+ cacheSchlock.writeLock("foo", ...);
+
+Once you have a schlock, you use the `readLock`, `writeLock`,
+`readUnlock`, `writeUnlock` methods to manage resources.
+
+Read-only access to resources is pretty straightforward.
+
+ var fname = "/tmp/myfile.txt";
+
+ fileSchlock.readLock(fname, function(err) {
+ fs.readFile(fname, function(err, data) {
+ fileSchlock.readUnlock(fname, function(err) {
+ // do something with the data
+ });
+ });
+ });
+
+Read-write access is about the same:
+
+ var fname = "/tmp/myfile.txt";
+
+ fileSchlock.writeLock(fname, function(err) {
+ fs.readFile(fname, function(err, data) {
+ var newData = modify(data);
+ fs.writeFile(fname, newData, function(err) {
+ fileSchlock.writeUnlock(fname, function(err) {
+ // do something with the data
+ });
+ });
+ });
+ });
+
+I use [step](https://npmjs.org/package/step) to organize async stuff
+so it'd look more like this for me:
+
+ var fname = "/tmp/myfile.txt";
+
+ Step(
+ function() {
+ fileSchlock.writeLock(fname, this);
+ },
+ function(err) {
+ if (err) throw err;
+ fs.readFile(fname, this);
+ },
+ function(err, data) {
+ if (err) throw err;
+ var newData = modify(data);
+ fs.writeFile(fname, newData, this);
+ },
+ function(err) {
+ if (err) throw err;
+ fileSchlock.writeUnlock(fname, this);
+ },
+ function(err) {
+ next(err);
+ }
+ );
+
+...which is a little less crazy.
+
+Multiple reads on the same resource can happen at the same time. Only
+one write on the same resource can happen at one time, and no reads
+can happen while the write is happening.
+
+Writes get precedence to prevent starvation.
+
+Filesystem
+==========
+
+I made this so I could do atomic-ish changes to files, but note that
+it doesn't do `flock()` or any other operating-system-level locking
+automatically. You still have to do that yourself.
+
+API
+===
+
+* `new Schlock()`
+
+Creates a new `Schlock`. Doesn't pay attention to any parameters.
+
+* `readLock(name, callback)`
+
+Lock the resource `name` for reading and call `callback`. The callback
+takes a single parameter, an error.
+
+If the resource is currently write-locked, the callback will be queued
+to run once the write lock is unlocked.
+
+* `readUnlock(name, callback)`
+
+Unlock the resource `name` for reading and call `callback`. The
+callback takes a single parameter, an error.
+
+* `writeLock(name, callback)`
+
+Lock the resource `name` for reading and call `callback`. The callback
+takes a single parameter, an error.
+
+If the resource is currently write-locked or read-locked, the callback
+will be queued to run once the write lock is unlocked.
+
+* `writeUnlock(name, callback)`
+
+Unlock the resource `name` for writing and call `callback`. The
+callback takes a single parameter, an error.
diff --git a/lib/schlock.js b/lib/schlock.js
new file mode 100644
index 0000000..97afc78
--- /dev/null
+++ b/lib/schlock.js
@@ -0,0 +1,168 @@
+// schlock.js
+//
+// Cheaply-made in-process lock broker
+//
+// Copyright 2012, StatusNet Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+var SchlockDoc = function(name, log) {
+
+ var readers = 0,
+ writers = 0,
+ readQueue = [],
+ writeQueue = [];
+
+ var runLater = function(runme) {
+ process.nextTick(function() {
+ runme(null);
+ });
+ };
+
+ var runNext = function() {
+
+ var writer,
+ reader;
+
+ // XXX: assert(writers === 0);
+
+ if (writeQueue.length > 0) {
+ if (readers === 0) {
+ if (log) log.info("Running queued writer for " + name);
+ writer = writeQueue.shift();
+ writers++;
+ runLater(writer);
+ }
+ } else if (readQueue.length > 0) {
+ if (log) log.info("Running " + readQueue.length + " queued reader(s) for " + name);
+ while (readQueue.length > 0) {
+ reader = readQueue.shift();
+ readers++;
+ runLater(reader);
+ }
+ }
+ };
+
+ this.idle = function() {
+ return (readers === 0 &&
+ writers === 0 &&
+ readQueue.length === 0 &&
+ writeQueue.length === 0);
+ };
+
+ this.readLock = function(callback) {
+ if (log) log.info("Read lock request for " + name);
+ if (writers > 0 || writeQueue.length > 0) {
+ if (log) log.info("Queueing reader for " + name);
+ readQueue.push(callback);
+ } else {
+ if (log) log.info("Running reader immediately for " + name);
+ readers++;
+ runLater(callback);
+ }
+ };
+
+ this.writeLock = function(callback) {
+ if (log) log.info("Write lock request for " + name);
+ if (writers > 0 || readers > 0 || writeQueue.length > 0) {
+ if (log) log.info("Queueing writer for " + name);
+ writeQueue.push(callback);
+ } else {
+ if (log) log.info("Running writer immediately for " + name);
+ writers++;
+ runLater(callback);
+ }
+ };
+
+ this.readUnlock = function(callback) {
+
+ if (log) log.info("Read unlock request for " + name);
+
+ if (readers > 0) {
+ if (log) log.info("Decrementing readers for " + name);
+ readers--;
+ }
+
+ runLater(callback);
+
+ runNext();
+ };
+
+ this.writeUnlock = function(callback) {
+
+ if (log) log.info("Write unlock request for " + name);
+
+ if (writers > 0) {
+ if (log) log.info("Decrementing writers for " + name);
+ writers--;
+ }
+
+ runLater(callback);
+
+ runNext();
+ };
+};
+
+var Schlock = function(logParent) {
+
+ var that = this,
+ docs = {},
+ log = (logParent) ? logParent.child({component: "schlock"}) : null;
+
+ var getDoc = function(name) {
+
+ if (log) log.info("Getting lock record for " + name);
+
+ if (!docs.hasOwnProperty(name)) {
+ if (log) log.info("New lock record for " + name);
+ docs[name] = new SchlockDoc(name, log);
+ }
+
+ return docs[name];
+ };
+
+ var rmDoc = function(name) {
+ if (log) log.info("Removing lock record for " + name);
+ delete docs[name];
+ };
+
+ this.readLock = function(name, callback) {
+ var sd = getDoc(name);
+ sd.readLock(callback);
+ };
+
+ this.writeLock = function(name, callback) {
+ var sd = getDoc(name);
+ sd.writeLock(callback);
+ };
+
+ this.readUnlock = function(name, callback) {
+ var sd = getDoc(name);
+ sd.readUnlock(callback);
+ if (sd.idle()) {
+ if (log) log.info("Lock record idle for " + name);
+ rmDoc(name);
+ }
+ };
+
+ this.writeUnlock = function(name, callback) {
+ var sd = getDoc(name);
+ sd.writeUnlock(callback);
+ if (sd.idle()) {
+ if (log) log.info("Lock record idle for " + name);
+ rmDoc(name);
+ }
+ };
+};
+
+module.exports = Schlock;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..239cd32
--- /dev/null
+++ b/package.json
@@ -0,0 +1,57 @@
+{
+ "name": "schlock",
+
+ "description": "Poorly-crafted in-process lock broker",
+
+ "homepage": "http://github.com/e14n/schlock",
+
+ "version": "0.2.1",
+
+ "keywords": ["schlock",
+ "lock",
+ "synchronization",
+ "sync",
+ "mutex",
+ "readlock",
+ "writelock",
+ "read-write",
+ "producer",
+ "consumer"],
+
+ "engines": {
+ "node": ">= 0.8.0"
+ },
+
+ "author": "Evan Prodromou <evan at e14n.com>",
+
+ "scripts": {
+ "test": "vows -i test/*-test.js"
+ },
+
+ "main": "./lib/schlock.js",
+
+ "directories": {
+ "lib": "./lib/"
+ },
+
+ "devDependencies": {
+ "vows": "0.7.x",
+ "step": "0.0.x",
+ "bunyan": "0.19.x"
+ },
+
+ "dependencies": {
+ },
+
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/e14n/schlock.git"
+ },
+
+ "licenses": [
+ {
+ "type": "Apache 2.0",
+ "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
+ }
+ ]
+}
diff --git a/test/logging-test.js b/test/logging-test.js
new file mode 100644
index 0000000..fe12915
--- /dev/null
+++ b/test/logging-test.js
@@ -0,0 +1,134 @@
+// logging-test.js
+//
+// Test the logging
+//
+// Copyright 2012, E14N Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+var assert = require("assert"),
+ vows = require("vows"),
+ Step = require("step"),
+ stream = require("stream"),
+ util = require("util"),
+ Logger = require("bunyan");
+
+var suite = vows.describe("schlock logging interface");
+
+var StreamMock = function() {
+ this.writable = true;
+ this.callback = null;
+ this.output = null;
+};
+
+util.inherits(StreamMock, stream.Stream);
+
+StreamMock.prototype.write = function(data) {
+ var cb;
+ this.output = data;
+ if (this.callback) {
+ cb = this.callback;
+ process.nextTick(function() {
+ cb(null, data);
+ });
+ this.callback = null;
+ }
+ return true;
+};
+
+StreamMock.prototype.end = function(data) {
+ var cb;
+ this.output = data;
+ if (this.callback) {
+ cb = this.callback;
+ process.nextTick(function() {
+ cb(null, data);
+ });
+ this.callback = null;
+ }
+};
+
+StreamMock.prototype.setCallback = function(callback) {
+ var mock = this;
+ mock.callback = callback;
+};
+
+suite.addBatch({
+ "When we require the module": {
+ topic: function() {
+ return require("../lib/schlock");
+ },
+ "it works": function(Schlock) {
+ assert.ok(Schlock);
+ },
+ "it returns the Schlock class": function(Schlock) {
+ assert.isFunction(Schlock);
+ },
+ "and we create a new Schlock": {
+ topic: function(Schlock) {
+ var str = new StreamMock(),
+ callback = this.callback,
+ log = new Logger({name: "schlock-test",
+ stream: str});
+
+ callback(null, str, new Schlock(log));
+ },
+ "it works": function(err, str, schlock) {
+ assert.ok(schlock);
+ },
+ "and we do a read lock": {
+ topic: function(str, schlock) {
+ str.setCallback(this.callback);
+ schlock.readLock("target1", function(err) {});
+ },
+ "it writes to the log": function(err, written) {
+ assert.ifError(err);
+ assert.ok(written);
+ },
+ "and we do a read unlock": {
+ topic: function(written, str, schlock) {
+ str.setCallback(this.callback);
+ schlock.readUnlock("target1", function(err) {});
+ },
+ "it writes to the log": function(err, written) {
+ assert.ifError(err);
+ assert.ok(written);
+ }
+ }
+ },
+ "and we do a write lock": {
+ topic: function(str, schlock) {
+ str.setCallback(this.callback);
+ schlock.writeLock("target2", function(err) {});
+ },
+ "it writes to the log": function(err, written) {
+ assert.ifError(err);
+ assert.ok(written);
+ },
+ "and we do a write unlock": {
+ topic: function(written, str, schlock) {
+ str.setCallback(this.callback);
+ schlock.writeUnlock("target2", function(err) {});
+ },
+ "it writes to the log": function(err, written) {
+ assert.ifError(err);
+ assert.ok(written);
+ }
+ }
+ }
+ }
+ }
+});
+
+suite["export"](module);
+
diff --git a/test/schlock-test.js b/test/schlock-test.js
new file mode 100644
index 0000000..61ecd45
--- /dev/null
+++ b/test/schlock-test.js
@@ -0,0 +1,333 @@
+// schlock-test.js
+//
+// Test the schlock module
+//
+// Copyright 2012, StatusNet Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+var assert = require("assert"),
+ vows = require("vows"),
+ Step = require("step");
+
+var suite = vows.describe("schlock module");
+
+suite.addBatch({
+ "When we require the module": {
+ topic: function() {
+ return require("../lib/schlock");
+ },
+ "it works": function(Schlock) {
+ assert.ok(Schlock);
+ },
+ "it returns the Schlock class": function(Schlock) {
+ assert.isFunction(Schlock);
+ },
+ "and we create a new Schlock": {
+ topic: function(Schlock) {
+ return new Schlock();
+ },
+ "it has a readLock() method": function(schlock) {
+ assert.isFunction(schlock.readLock);
+ },
+ "it has a writeLock() method": function(schlock) {
+ assert.isFunction(schlock.writeLock);
+ },
+ "it has a readUnlock() method": function(schlock) {
+ assert.isFunction(schlock.readUnlock);
+ },
+ "it has a writeUnlock() method": function(schlock) {
+ assert.isFunction(schlock.writeUnlock);
+ },
+ "and we readLock a resource": {
+ topic: function(schlock) {
+ var resource1 = 42,
+ value,
+ callback = this.callback;
+
+ Step(
+ function() {
+ schlock.readLock("resource1", this);
+ },
+ function(err) {
+ if (err) throw err;
+ value = resource1;
+ schlock.readUnlock("resource1", this);
+ },
+ function(err) {
+ if (err) {
+ callback(err, null);
+ } else {
+ callback(null, value);
+ }
+ }
+ );
+ },
+ "it works": function(err, value) {
+ assert.ifError(err);
+ assert.equal(value, 42);
+ }
+ },
+ "and we writeLock a resource": {
+ topic: function(schlock) {
+ var resource2 = 23,
+ value,
+ callback = this.callback;
+
+ Step(
+ function() {
+ schlock.writeLock("resource2", this);
+ },
+ function(err) {
+ if (err) throw err;
+ resource2 = 16;
+ value = resource2;
+ schlock.writeUnlock("resource2", this);
+ },
+ function(err) {
+ if (err) {
+ callback(err, null);
+ } else {
+ callback(null, value);
+ }
+ }
+ );
+ },
+ "it works": function(err, value) {
+ assert.ifError(err);
+ assert.equal(value, 16);
+ }
+ },
+ "and we readLock a resource multiple times": {
+ topic: function(schlock) {
+ var callback = this.callback,
+ resource3 = 15;
+
+ Step(
+ function() {
+ var i,
+ group = this.group(),
+ reader = function(cb) {
+ return function(err) {
+ var value;
+ if (err) {
+ cb(err, null);
+ } else {
+ value = resource3;
+ schlock.readUnlock("resource3", function(err) {
+ if (err) {
+ cb(err, null);
+ } else {
+ cb(null, value);
+ }
+ });
+ }
+ };
+ };
+
+ for (i = 0; i < 10; i++) {
+ schlock.readLock("resource3", reader(group()));
+ }
+ },
+ function(err, results) {
+ if (err) {
+ callback(err, null);
+ } else {
+ callback(null, results);
+ }
+ }
+ );
+ },
+ "it works": function(err, results) {
+ var i;
+ assert.ifError(err);
+ assert.isArray(results);
+ assert.lengthOf(results, 10);
+ for (i = 0; i < 10; i++) {
+ assert.equal(results[i], 15);
+ }
+ }
+ },
+ "and we do a writer between readers": {
+ topic: function(schlock) {
+ var callback = this.callback,
+ resource4 = 8;
+
+ Step(
+ function() {
+ var i,
+ group = this.group(),
+ reader = function(cb) {
+ var value;
+ return function(err) {
+ if (err) {
+ cb(err, null);
+ } else {
+ value = resource4;
+ schlock.readUnlock("resource4", function(err) {
+ if (err) {
+ cb(err, null);
+ } else {
+ cb(null, value);
+ }
+ });
+ }
+ };
+ },
+ writer = function(cb) {
+ var value;
+ return function(err) {
+ if (err) {
+ cb(err, null);
+ } else {
+ resource4 = 4;
+ value = resource4;
+ schlock.writeUnlock("resource4", function(err) {
+ if (err) {
+ cb(err, null);
+ } else {
+ cb(null, value);
+ }
+ });
+ }
+ };
+ };
+
+ for (i = 0; i < 10; i++) {
+ schlock.readLock("resource4", reader(group()));
+ }
+
+ schlock.writeLock("resource4", writer(group()));
+
+ for (i = 11; i < 21; i++) {
+ schlock.readLock("resource4", reader(group()));
+ }
+ },
+ function(err, results) {
+ if (err) {
+ callback(err, null);
+ } else {
+ callback(null, results);
+ }
+ }
+ );
+ },
+ "it works": function(err, results) {
+ var i;
+ assert.ifError(err);
+ assert.isArray(results);
+ assert.lengthOf(results, 21);
+ for (i = 0; i < 10; i++) {
+ assert.equal(results[i], 8);
+ }
+ assert.equal(results[10], 4);
+ for (i = 11; i < 21; i++) {
+ assert.equal(results[i], 4);
+ }
+ }
+ },
+ "and we writeLock a resource multiple times": {
+ topic: function(schlock) {
+ var callback = this.callback,
+ resource5 = 0;
+
+ Step(
+ function() {
+ var i,
+ group = this.group(),
+ writer = function(cb) {
+ return function(err) {
+ var value;
+ if (err) {
+ cb(err, null);
+ } else {
+ resource5++;
+ value = resource5;
+ schlock.writeUnlock("resource5", function(err) {
+ if (err) {
+ cb(err, null);
+ } else {
+ cb(null, value);
+ }
+ });
+ }
+ };
+ };
+
+ for (i = 0; i < 10; i++) {
+ schlock.writeLock("resource5", writer(group()));
+ }
+ },
+ function(err, results) {
+ if (err) {
+ callback(err, null);
+ } else {
+ callback(null, results);
+ }
+ }
+ );
+ },
+ "it works": function(err, results) {
+ var i;
+ assert.ifError(err);
+ assert.isArray(results);
+ assert.lengthOf(results, 10);
+ for (i = 0; i < 10; i++) {
+ assert.equal(results[i], i+1);
+ }
+ }
+ },
+ "and we writeLock a resource twice in sequence": {
+ topic: function(schlock) {
+ var callback = this.callback,
+ resource6 = 0;
+
+ Step(
+ function() {
+ schlock.writeLock("resource6", this);
+ },
+ function(err) {
+ if (err) throw err;
+ resource6++;
+ schlock.writeUnlock("resource6", this);
+ },
+ function(err) {
+ if (err) throw err;
+ schlock.writeLock("resource6", this);
+ },
+ function(err) {
+ if (err) throw err;
+ resource6++;
+ schlock.writeUnlock("resource6", this);
+ },
+ function(err) {
+ if (err) {
+ callback(err, null);
+ } else {
+ callback(null, resource6);
+ }
+ }
+ );
+ },
+ "it works": function(err, results) {
+ assert.ifError(err);
+ assert.isNumber(results);
+ assert.equal(results, 2);
+ }
+ }
+
+ }
+ }
+});
+
+suite["export"](module);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-schlock.git
More information about the Pkg-javascript-commits
mailing list