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.""" |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 68 | batch_size = 100 |
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( |
| 77 | batch_size=batch_size, multirequest_size=1)) |
| 78 | task_processor.batch_execute() |
| 79 | |
| 80 | # After batch_execute runs, the extra tasks should remain in the queue. |
| 81 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 82 | self.assertEqual(extra_num, len(tasks)) |
Xixuan Wu | a5a2944 | 2017-10-11 11:03:02 -0700 | [diff] [blame] | 83 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 84 | def testBatchExecuteTaskFailedFrontdoorTotally(self): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 85 | """Test task_executor fails at the beginning, and no tasks are deleted.""" |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 86 | batch_size = 100 |
| 87 | multirequest_size = 30 |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 88 | extra_num = 10 |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 89 | _push_tasks('fake_suite', multirequest_size + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 90 | # Before batch_execute |
| 91 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 92 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame^] | 93 | |
| 94 | task_processor = task_executor.TaskProcessor( |
| 95 | task_executor.SUITES_QUEUE, |
| 96 | test_platform_client=FakeFrontdoorClient(0, error_num=len(tasks)), |
| 97 | options=task_executor.Options( |
| 98 | batch_size=batch_size, multirequest_size=multirequest_size)) |
| 99 | task_processor.batch_execute() |
| 100 | |
| 101 | # After batch_execute, no tasks are deleted from task queue, due |
| 102 | # to they're all failed to kick off. |
| 103 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 104 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 105 | |
Xinan Lin | 9e4917d | 2019-11-04 10:58:47 -0800 | [diff] [blame] | 106 | def testBatchExecuteTaskFailedFrontdoorPartially(self): |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 107 | """Test task_executor fails halfway, and only executed tasks are deleted.""" |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 108 | # Batch large enough to cover all added tasks. |
| 109 | batch_size = 1000 |
| 110 | multirequest_size = 30 |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 111 | extra_num = 10 |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 112 | success_num = (multirequest_size + extra_num) / 2 |
| 113 | error_num = multirequest_size + extra_num |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 114 | _push_tasks('fake_suite', multirequest_size + extra_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 115 | |
| 116 | # Before batch_execute |
| 117 | tasks = self.taskqueue_stub.get_filtered_tasks() |
Prathmesh Prabhu | 7b961d5 | 2020-03-06 23:57:42 -0800 | [diff] [blame] | 118 | self.assertEqual(len(tasks), multirequest_size + extra_num) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame^] | 119 | |
| 120 | task_processor = task_executor.TaskProcessor( |
| 121 | task_executor.SUITES_QUEUE, |
| 122 | test_platform_client=FakeFrontdoorClient(success_num, error_num), |
| 123 | options=task_executor.Options( |
| 124 | batch_size=batch_size, multirequest_size=multirequest_size)) |
| 125 | task_processor.batch_execute() |
| 126 | |
| 127 | # After batch_execute, only failed suites and extra suites are |
| 128 | # kept in task queue. |
| 129 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 130 | self.assertEqual(len(tasks), error_num) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 131 | |
| 132 | def testBatchExecuteTaskFailedLeasing(self): |
| 133 | """Test task_executor fails to lease task.""" |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 134 | _push_tasks('fake_suite', 1) |
Prathmesh Prabhu | 8f43d31 | 2020-03-07 00:25:06 -0800 | [diff] [blame^] | 135 | |
| 136 | task_processor = task_executor.TaskProcessor( |
| 137 | 'nonExistentQueue', |
| 138 | test_platform_client=FakeFrontdoorClient(False), |
| 139 | options=task_executor.Options(10, 10)) |
| 140 | self.assertRaises(taskqueue.UnknownQueueError, |
| 141 | task_processor.batch_execute) |
| 142 | |
| 143 | # After batch_execute fails, no tasks are deleted from task queue. |
| 144 | tasks = self.taskqueue_stub.get_filtered_tasks() |
| 145 | self.assertEqual(len(tasks), 1) |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 146 | |
| 147 | |
Prathmesh Prabhu | 89c814e | 2020-03-07 00:12:43 -0800 | [diff] [blame] | 148 | def _push_tasks(suite, count): |
| 149 | """Push count tasks in the executor queue for suite.""" |
| 150 | for i in range(count): |
| 151 | task_executor.push( |
| 152 | task_executor.SUITES_QUEUE, tag=suite, suite=suite, num=i + 1) |
| 153 | |
| 154 | |
Xixuan Wu | 835dee2 | 2017-09-07 10:47:29 -0700 | [diff] [blame] | 155 | if __name__ == '__main__': |
| 156 | unittest.main() |