예제 #1
0
 def testException(self):
     int_list = []
     calls = [(_AppendLength, (int_list, ), {}), (_RaiseValueError, (), {}),
              (_AppendLength, (int_list, ), {})]
     with self.assertRaises(errors.VmUtil.ThreadException):
         background_tasks.RunParallelThreads(calls, max_concurrency=1)
     self.assertEqual(int_list, [0, 1])
예제 #2
0
 def testInterrupt(self):
     # Uses RunParallelThreads to try to run four threads:
     #   0: Waits 5 seconds and adds 0 to int_list.
     #   1: Adds 1 to int_list.
     #   2: Sends a SIGINT to the current process.
     #   3: Waits 5 seconds and adds 3 to int_list.
     # Since the max_concurrency is set to 2, what should happen is that thread 0
     # waits, thread 1 succeeds, thread 2 sends the SIGINT, and then neither
     # thread 1 nor 3 is able to append to int_list.
     int_list = []
     event = threading.Event()
     calls = [(_WaitAndAppendInt, (int_list, 0, event, 5), {}),
              (_WaitAndAppendInt, (int_list, 1), {}),
              (os.kill, (os.getpid(), signal.SIGINT), {}),
              (_WaitAndAppendInt, (int_list, 3, event, 5), {})]
     with self.assertRaises(KeyboardInterrupt):
         background_tasks.RunParallelThreads(calls, max_concurrency=2)
     self.assertEqual(int_list, [1])
예제 #3
0
 def testFewerThreadsThanConcurrencyLimit(self):
     calls = [(_ReturnArgs, ('a', ), {'b': i}) for i in range(2)]
     result = background_tasks.RunParallelThreads(calls, max_concurrency=4)
     self.assertEqual(result, [(0, 'a'), (1, 'a')])