summaryrefslogtreecommitdiff
path: root/trunk/scripts/test-bgjobs
diff options
context:
space:
mode:
Diffstat (limited to 'trunk/scripts/test-bgjobs')
-rwxr-xr-xtrunk/scripts/test-bgjobs47
1 files changed, 47 insertions, 0 deletions
diff --git a/trunk/scripts/test-bgjobs b/trunk/scripts/test-bgjobs
new file mode 100755
index 0000000..1526ca3
--- /dev/null
+++ b/trunk/scripts/test-bgjobs
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+#
+# A small test script to excercise the bgjobs stuff. We create a number of
+# jobs, and make run them, and make sure the results are correct.
+#
+# By running this script a large number of times (say, 10 000) in a loop,
+# it may be possible to expose race conditions. (As of the time of committing,
+# no such are known.)
+#
+# If you want to run this in the source directory, do this:
+#
+# PYTHONPATH=. python scripts/test-bgjobs
+
+
+import Queue
+import time
+
+import dimbola
+
+
+N = 100000
+
+
+class SlowJob(dimbola.BackgroundJob):
+
+ def __init__(self, value):
+ self.value = value
+
+ def run(self):
+ return self.value
+
+
+manager = dimbola.BackgroundManager()
+for i in range(N):
+ manager.add_job(SlowJob(i))
+manager.start_jobs(maxproc=None)
+results = 0
+while manager.running:
+ try:
+ result = manager.results.get(block=False)
+ except Queue.Empty:
+ pass
+ else:
+ results += 1
+manager.stop_jobs()
+assert results == N, "results==%d (not %d)" % (results, N)
+