KVM test: Add helpers to control the TAP/bridge
[autotest-zwu.git] / cli / threads_unittest.py
blobd1ad3447c6731d45a726efe31a509211cf05e3fa
1 #!/usr/bin/python
3 # Copyright 2008 Google Inc. All Rights Reserved.
5 """Tests for thread."""
7 import unittest, sys, os
9 import threading, Queue
11 import common
12 from autotest_lib.cli import cli_mock, threads
15 class thread_unittest(cli_mock.cli_unittest):
16 results = Queue.Queue()
18 def _workload(self, i):
19 self.results.put(i*i)
22 def test_starting(self):
23 self.god.stub_class_method(threading.Thread, 'start')
24 threading.Thread.start.expect_call().and_return(None)
25 threading.Thread.start.expect_call().and_return(None)
26 threading.Thread.start.expect_call().and_return(None)
27 threading.Thread.start.expect_call().and_return(None)
28 threading.Thread.start.expect_call().and_return(None)
29 th = threads.ThreadPool(self._workload, numthreads=5)
30 self.god.check_playback()
33 def test_one_thread(self):
34 th = threads.ThreadPool(self._workload, numthreads=1)
35 th.queue_work(range(10))
36 th.wait()
37 res = []
38 while not self.results.empty():
39 res.append(self.results.get())
40 self.assertEqualNoOrder([0, 1, 4, 9, 16, 25, 36, 49, 64, 81], res)
43 def _threading(self, numthreads, count):
44 th = threads.ThreadPool(self._workload, numthreads=numthreads)
45 th.queue_work(range(count))
46 th.wait()
47 res = []
48 while not self.results.empty():
49 res.append(self.results.get())
50 self.assertEqualNoOrder([i*i for i in xrange(count)], res)
53 def test_threading(self):
54 self._threading(10, 10)
57 def test_threading_lots(self):
58 self._threading(100, 100)
61 def test_threading_multi_queueing(self):
62 th = threads.ThreadPool(self._workload, numthreads=5)
63 th.queue_work(range(5))
64 th.queue_work(range(5, 10))
65 th.wait()
66 res = []
67 while not self.results.empty():
68 res.append(self.results.get())
69 self.assertEqualNoOrder([i*i for i in xrange(10)], res)
72 if __name__ == '__main__':
73 unittest.main()