summaryrefslogtreecommitdiff
path: root/jstests/libs/concurrent.js
diff options
context:
space:
mode:
authorTony Hannan <tony@10gen.com>2011-02-03 17:23:38 -0500
committerTony Hannan <tony@10gen.com>2011-02-03 17:23:38 -0500
commit41e665c98da1a54d931ee575b2b6113b5e2b4c67 (patch)
tree7cdbeefc48c36afe882ded994c480f0607bf14a0 /jstests/libs/concurrent.js
parentc1835259d0c1d59d3f4b978bbbb9b318f5e6a8fc (diff)
downloadmongo-41e665c98da1a54d931ee575b2b6113b5e2b4c67.tar.gz
multi-threaded replset with durability test, requires V8 in mongo shell
Diffstat (limited to 'jstests/libs/concurrent.js')
-rw-r--r--jstests/libs/concurrent.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/jstests/libs/concurrent.js b/jstests/libs/concurrent.js
new file mode 100644
index 00000000000..d5db7fb94ad
--- /dev/null
+++ b/jstests/libs/concurrent.js
@@ -0,0 +1,30 @@
+/* NOTE: Request mongo shell to be built with V8 javascript engine,
+which implements concurrent threads via fork() */
+
+// Fork and start
+function fork_(thunk) {
+ thread = fork(thunk)
+ thread.start()
+ return thread
+}
+
+// In functional form, useful for high-order functions like map in fun.js
+function join_(thread) {thread.join()}
+
+// Fork a loop on each one-arg block and wait for all of them to terminate. Foreground blocks are executed n times, background blocks are executed repeatedly until all forground loops finish. If any fail, stop all loops and reraise exception in main thread
+function parallel(n, foregroundBlock1s, backgroundBlock1s) {
+ var err = null
+ var stop = false
+ function loop(m) {return function(block1) {return function() {
+ for (var i = 0; i < m; i++) {if (stop) break; block1(i)} }}}
+ function watch(block) {return function() {
+ try {block()} catch(e) {err = e; stop = true}}}
+ foreThunks = map(watch, map(loop(n), foregroundBlock1s))
+ backThunks = map(watch, map(loop(Infinity), backgroundBlock1s))
+ foreThreads = map(fork_, foreThunks)
+ backThreads = map(fork_, backThunks)
+ map(join_, foreThreads)
+ stop = true
+ map(join_, backThreads)
+ if (err != null) throw err
+}