summaryrefslogtreecommitdiffstats
path: root/pym/portage/tests/process
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2008-07-12 06:22:26 +0000
committerZac Medico <zmedico@gentoo.org>2008-07-12 06:22:26 +0000
commit31b18e72b3ed4077f31be583d9a83ad2de53df29 (patch)
tree29cb7823c5fc4fce43d29b5cb0e438f0f95e30ed /pym/portage/tests/process
parentf9dc49ffd4486777e96b2e045c0b4523c479fef4 (diff)
downloadportage-31b18e72b3ed4077f31be583d9a83ad2de53df29.tar.gz
portage-31b18e72b3ed4077f31be583d9a83ad2de53df29.tar.bz2
portage-31b18e72b3ed4077f31be583d9a83ad2de53df29.zip
Create a test case for the poll loop which uses the loop to read data from a
pipe and assert that the data written to the pipe is identical to the data read from the pipe. In order to implement this test, several useful classes have been added: * PipeReader Reads output from one or more files and saves it in memory, for retrieval via the getvalue() method. This is driven by the scheduler's poll() loop, so it runs entirely within the current process. * QueueScheduler Add instances of SequentialTaskQueue and then call run(). The run() method returns when no tasks remain. * TaskScheduler A simple way to handle scheduling of AsynchrousTask instances. Simply add tasks and call run(). The run() method returns when no tasks remain. svn path=/main/trunk/; revision=11022
Diffstat (limited to 'pym/portage/tests/process')
-rw-r--r--pym/portage/tests/process/__init__.py3
-rw-r--r--pym/portage/tests/process/__test__0
-rw-r--r--pym/portage/tests/process/test_poll.py43
3 files changed, 46 insertions, 0 deletions
diff --git a/pym/portage/tests/process/__init__.py b/pym/portage/tests/process/__init__.py
new file mode 100644
index 000000000..a4a87a461
--- /dev/null
+++ b/pym/portage/tests/process/__init__.py
@@ -0,0 +1,3 @@
+# Copyright 1998-2008 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id: __init__.py 6870 2007-06-19 07:22:18Z zmedico $
diff --git a/pym/portage/tests/process/__test__ b/pym/portage/tests/process/__test__
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/pym/portage/tests/process/__test__
diff --git a/pym/portage/tests/process/test_poll.py b/pym/portage/tests/process/test_poll.py
new file mode 100644
index 000000000..45c0ad03e
--- /dev/null
+++ b/pym/portage/tests/process/test_poll.py
@@ -0,0 +1,43 @@
+# Copyright 1998-2008 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id: test_spawn.py 8474 2007-11-09 03:35:38Z zmedico $
+
+import errno, os, sys
+from portage.tests import TestCase
+from _emerge import PipeReader, SpawnProcess, TaskScheduler
+
+class PollTestCase(TestCase):
+
+ def testPipeReader(self):
+ """
+ Use a poll loop to read data from a pipe and assert that
+ the data written to the pipe is identical to the data
+ read from the pipe.
+ """
+
+ test_string = 2 * "blah blah blah\n"
+
+ master_fd, slave_fd = os.pipe()
+ master_file = os.fdopen(master_fd, 'r')
+
+ task_scheduler = TaskScheduler(max_jobs=2)
+ scheduler = task_scheduler.sched_iface
+
+ producer = SpawnProcess(
+ args=["bash", "-c", "echo -n '%s'" % test_string],
+ fd_pipes={1:slave_fd}, scheduler=scheduler)
+
+ consumer = PipeReader(
+ input_files={"producer" : master_file},
+ scheduler=scheduler)
+
+ task_scheduler.add(producer)
+ task_scheduler.add(consumer)
+
+ def producer_start_cb(task):
+ os.close(slave_fd)
+
+ producer.addStartListener(producer_start_cb)
+ task_scheduler.run()
+
+ self.assertEqual(test_string, consumer.getvalue())