Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 1 | # Copyright 2017 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module for task_executor unittests.""" |
| 6 | # pylint: disable=g-bad-import-order |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 7 | # pylint: disable=unused-argument |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 8 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 9 | import collections |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 10 | import mock |
| 11 | import os |
| 12 | import sys |
| 13 | import unittest |
| 14 | |
| 15 | import task_executor |
| 16 | |
| 17 | from google.appengine.api import taskqueue |
| 18 | from google.appengine.ext import testbed |
| 19 | |
| 20 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 21 | class FakeFrontdoorClient(object): |
| 22 | |
| 23 | # pylint: disable=g-deprecated-member-used |
| 24 | def __init__(self, success_num=sys.maxint, error_num=0): |
| 25 | self.success_num = success_num |
| 26 | self.error_num = error_num |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 27 | self.frontdoor_run_count = collections.defaultdict(list) |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 28 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 29 | def multirequest_run(self, tasks, suite): |
| 30 | executed_tasks = [] |
| 31 | for task in tasks: |
| 32 | params = task.extract_params() |
| 33 | num = int(params.get('num', 0)) |
| 34 | if num > self.success_num and num <= self.success_num + self.error_num: |
| 35 | raise ValueError('test') |
| 36 | executed_tasks.append(task) |
| 37 | self.frontdoor_run_count[suite].append(executed_tasks) |
| 38 | return executed_tasks |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 39 | |
| 40 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 41 | class TaskExecutorTestCase(unittest.TestCase): |
| 42 | |
| 43 | def setUp(self): |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 44 | super(TaskExecutorTestCase, self).setUp() |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 45 | self.testbed = testbed.Testbed() |
| 46 | self.testbed.activate() |
| 47 | self.addCleanup(self.testbed.deactivate) |
| 48 | |
| 49 | # root_path must be set the location of queue.yaml. |
| 50 | # Otherwise, only the 'default' queue will be available. |
| 51 | self.testbed.init_taskqueue_stub( |
| 52 | root_path=os.path.join(os.path.dirname(__file__))) |
| 53 | self.taskqueue_stub = self.testbed.get_stub( |
| 54 | testbed.TASKQUEUE_SERVICE_NAME) |
| 55 | |
Prathmesh Prabhu | 40f1e1e | 2020-03-07 00:04:41 -0800 | [diff] [blame] | 56 | p = mock.patch('global_config.GAE_TESTING', return_value=True) |
| 57 | p.start() |
| 58 | self.addCleanup(p.stop) |
| 59 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 60 | def testPushTask(self): |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 61 | _push_tasks('fake_suite', 1) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 62 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 63 | self.assertEqual(len(tasks), 1) |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 64 | self.assertEqual('fake_suite', tasks[0].extract_params()['suite']) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 65 | |
Xinan Lin | 3ba18a0 | 2019-08-13 15:44:55 -0700 | [diff] [blame] | 66 | def testBatchExecuteTask(self): |
Xinan Lin | a18d988 | 2019-12-18 11:16:33 -0800 | [diff] [blame] | 67 | """Test task_executor executes tasks not greater than the BATCH_SIZE.""" |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 68 | batch_size = 10 |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 69 | extra_num = 10 |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 70 | _push_tasks('foo_suite', batch_size / 2) |
| 71 | _push_tasks('hoo_suite', batch_size / 2 + extra_num) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 72 | |
| 73 | task_processor = task_executor.TaskProcessor( |
| 74 | task_executor.SUITES_QUEUE, |
| 75 | test_platform_client=FakeFrontdoorClient(), |
| 76 | options=task_executor.Options( |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 77 | batch_size=batch_size, |
| 78 | multirequest_size=1, |
| 79 | per_suite_multirequest_size={})) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 80 | task_processor.batch_execute() |
| 81 | |
| 82 | # After batch_execute runs, the extra tasks should remain in the queue. |
| 83 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 84 | self.assertEqual(extra_num, len(tasks)) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 85 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 86 | def testBatchExecuteTaskFailedFrontdoorTotally(self): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 87 | """Test task_executor fails at the beginning, and no tasks are deleted.""" |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 88 | batch_size = 10 |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 89 | multirequest_size = 30 |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 90 | extra_num = 10 |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 91 | _push_tasks('fake_suite', multirequest_size + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 92 | # Before batch_execute |
| 93 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 94 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 95 | |
| 96 | task_processor = task_executor.TaskProcessor( |
| 97 | task_executor.SUITES_QUEUE, |
| 98 | test_platform_client=FakeFrontdoorClient(0, error_num=len(tasks)), |
| 99 | options=task_executor.Options( |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 100 | batch_size=batch_size, |
| 101 | multirequest_size=multirequest_size, |
| 102 | per_suite_multirequest_size={})) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 103 | task_processor.batch_execute() |
| 104 | |
| 105 | # After batch_execute, no tasks are deleted from task queue, due |
| 106 | # to they're all failed to kick off. |
| 107 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 108 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 109 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 110 | def testBatchExecuteTaskFailedFrontdoorPartially(self): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 111 | """Test task_executor fails halfway, and only executed tasks are deleted.""" |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 112 | # Batch large enough to cover all added tasks. |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 113 | batch_size = 10 |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 114 | multirequest_size = 30 |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 115 | extra_num = 10 |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 116 | success_num = (multirequest_size + extra_num) / 2 |
| 117 | error_num = multirequest_size + extra_num |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 118 | _push_tasks('fake_suite', multirequest_size + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 119 | |
| 120 | # Before batch_execute |
| 121 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 122 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 123 | |
| 124 | task_processor = task_executor.TaskProcessor( |
| 125 | task_executor.SUITES_QUEUE, |
| 126 | test_platform_client=FakeFrontdoorClient(success_num, error_num), |
| 127 | options=task_executor.Options( |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 128 | batch_size=batch_size, |
| 129 | multirequest_size=multirequest_size, |
| 130 | per_suite_multirequest_size={})) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 131 | task_processor.batch_execute() |
| 132 | |
| 133 | # After batch_execute, only failed suites and extra suites are |
| 134 | # kept in task queue. |
| 135 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 136 | self.assertEqual(len(tasks), error_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 137 | |
| 138 | def testBatchExecuteTaskFailedLeasing(self): |
| 139 | """Test task_executor fails to lease task.""" |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 140 | _push_tasks('fake_suite', 1) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 141 | |
| 142 | task_processor = task_executor.TaskProcessor( |
| 143 | 'nonExistentQueue', |
| 144 | test_platform_client=FakeFrontdoorClient(False), |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 145 | options=task_executor.Options(10, 10, {})) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame] | 146 | self.assertRaises(taskqueue.UnknownQueueError, |
| 147 | task_processor.batch_execute) |
| 148 | |
| 149 | # After batch_execute fails, no tasks are deleted from task queue. |
| 150 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 151 | self.assertEqual(len(tasks), 1) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 152 | |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 153 | def testBatchExecuteTaskWithPerSuiteLimit(self): |
| 154 | """Test task_executor executes respects per-suite limit.""" |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 155 | batch_size = 10 |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 156 | multirequest_size = 10 |
| 157 | foo_suite_limit = 3 |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 158 | extra_num = 5 |
| 159 | _push_tasks('foo_suite', batch_size * foo_suite_limit + extra_num) |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 160 | |
| 161 | frontdoor_client = FakeFrontdoorClient() |
| 162 | task_processor = task_executor.TaskProcessor( |
| 163 | task_executor.SUITES_QUEUE, |
| 164 | test_platform_client=frontdoor_client, |
| 165 | options=task_executor.Options( |
| 166 | batch_size=batch_size, |
| 167 | multirequest_size=multirequest_size, |
| 168 | per_suite_multirequest_size={'foo_suite': foo_suite_limit})) |
| 169 | task_processor.batch_execute() |
| 170 | |
| 171 | executed_count = 0 |
| 172 | for tasks in frontdoor_client.frontdoor_run_count['foo_suite']: |
| 173 | self.assertEqual(foo_suite_limit, len(tasks)) |
| 174 | executed_count += len(tasks) |
Xinan Lin | 3b9b6f4 | 2020-03-16 16:10:37 -0700 | [diff] [blame] | 175 | self.assertEqual(executed_count, batch_size * foo_suite_limit) |
| 176 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 177 | self.assertEqual(extra_num, len(tasks)) |
Prathmesh Prabhu | 55c4003 | 2020-03-07 00:45:15 -0800 | [diff] [blame] | 178 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 179 | |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 180 | def _push_tasks(suite, count): |
| 181 | """Push count tasks in the executor queue for suite.""" |
| 182 | for i in range(count): |
| 183 | task_executor.push( |
| 184 | task_executor.SUITES_QUEUE, tag=suite, suite=suite, num=i + 1) |
| 185 | |
| 186 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 187 | if __name__ == '__main__': |
| 188 | unittest.main() |