blob: 1dadc9e50e41db20dd9b880a9d731fa9acf824b2 [file] [log] [blame]
Xixuan Wu835dee22017-09-07 10:47:29 -07001# 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 Lin3ba18a02019-08-13 15:44:55 -07007# pylint: disable=unused-argument
Xixuan Wu835dee22017-09-07 10:47:29 -07008
Xinan Lin9e4917d2019-11-04 10:58:47 -08009import collections
Xixuan Wu835dee22017-09-07 10:47:29 -070010import mock
11import os
12import sys
13import unittest
14
15import task_executor
16
17from google.appengine.api import taskqueue
18from google.appengine.ext import testbed
19
20
Xinan Lin3ba18a02019-08-13 15:44:55 -070021class 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 Lin9e4917d2019-11-04 10:58:47 -080027 self.frontdoor_run_count = collections.defaultdict(list)
Xinan Lin3ba18a02019-08-13 15:44:55 -070028
Xinan Lin9e4917d2019-11-04 10:58:47 -080029 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 Lin3ba18a02019-08-13 15:44:55 -070039
40
Xixuan Wu835dee22017-09-07 10:47:29 -070041class TaskExecutorTestCase(unittest.TestCase):
42
43 def setUp(self):
Prathmesh Prabhu7b961d52020-03-06 23:57:42 -080044 super(TaskExecutorTestCase, self).setUp()
Xixuan Wu835dee22017-09-07 10:47:29 -070045 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 Prabhu40f1e1e2020-03-07 00:04:41 -080056 p = mock.patch('global_config.GAE_TESTING', return_value=True)
57 p.start()
58 self.addCleanup(p.stop)
59
Xixuan Wu835dee22017-09-07 10:47:29 -070060 def testPushTask(self):
Prathmesh Prabhu89c814e2020-03-07 00:12:43 -080061 _push_tasks('fake_suite', 1)
Xixuan Wu835dee22017-09-07 10:47:29 -070062 tasks = self.taskqueue_stub.get_filtered_tasks()
63 self.assertEqual(len(tasks), 1)
Prathmesh Prabhu89c814e2020-03-07 00:12:43 -080064 self.assertEqual('fake_suite', tasks[0].extract_params()['suite'])
Xixuan Wu835dee22017-09-07 10:47:29 -070065
Xinan Lin3ba18a02019-08-13 15:44:55 -070066 def testBatchExecuteTask(self):
Xinan Lina18d9882019-12-18 11:16:33 -080067 """Test task_executor executes tasks not greater than the BATCH_SIZE."""
Prathmesh Prabhu7b961d52020-03-06 23:57:42 -080068 batch_size = 100
Xinan Lin9e4917d2019-11-04 10:58:47 -080069 extra_num = 10
Prathmesh Prabhu89c814e2020-03-07 00:12:43 -080070 _push_tasks('foo_suite', batch_size / 2)
71 _push_tasks('hoo_suite', batch_size / 2 + extra_num)
Prathmesh Prabhu8f43d312020-03-07 00:25:06 -080072
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 Wua5a29442017-10-11 11:03:02 -070083
Xinan Lin9e4917d2019-11-04 10:58:47 -080084 def testBatchExecuteTaskFailedFrontdoorTotally(self):
Xixuan Wu835dee22017-09-07 10:47:29 -070085 """Test task_executor fails at the beginning, and no tasks are deleted."""
Prathmesh Prabhu7b961d52020-03-06 23:57:42 -080086 batch_size = 100
87 multirequest_size = 30
Xixuan Wu835dee22017-09-07 10:47:29 -070088 extra_num = 10
Prathmesh Prabhu89c814e2020-03-07 00:12:43 -080089 _push_tasks('fake_suite', multirequest_size + extra_num)
Xixuan Wu835dee22017-09-07 10:47:29 -070090 # Before batch_execute
91 tasks = self.taskqueue_stub.get_filtered_tasks()
Prathmesh Prabhu7b961d52020-03-06 23:57:42 -080092 self.assertEqual(len(tasks), multirequest_size + extra_num)
Prathmesh Prabhu8f43d312020-03-07 00:25:06 -080093
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 Wu835dee22017-09-07 10:47:29 -0700105
Xinan Lin9e4917d2019-11-04 10:58:47 -0800106 def testBatchExecuteTaskFailedFrontdoorPartially(self):
Xixuan Wu835dee22017-09-07 10:47:29 -0700107 """Test task_executor fails halfway, and only executed tasks are deleted."""
Prathmesh Prabhu7b961d52020-03-06 23:57:42 -0800108 # Batch large enough to cover all added tasks.
109 batch_size = 1000
110 multirequest_size = 30
Xixuan Wu835dee22017-09-07 10:47:29 -0700111 extra_num = 10
Prathmesh Prabhu7b961d52020-03-06 23:57:42 -0800112 success_num = (multirequest_size + extra_num) / 2
113 error_num = multirequest_size + extra_num
Prathmesh Prabhu89c814e2020-03-07 00:12:43 -0800114 _push_tasks('fake_suite', multirequest_size + extra_num)
Xixuan Wu835dee22017-09-07 10:47:29 -0700115
116 # Before batch_execute
117 tasks = self.taskqueue_stub.get_filtered_tasks()
Prathmesh Prabhu7b961d52020-03-06 23:57:42 -0800118 self.assertEqual(len(tasks), multirequest_size + extra_num)
Prathmesh Prabhu8f43d312020-03-07 00:25:06 -0800119
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 Wu835dee22017-09-07 10:47:29 -0700131
132 def testBatchExecuteTaskFailedLeasing(self):
133 """Test task_executor fails to lease task."""
Prathmesh Prabhu89c814e2020-03-07 00:12:43 -0800134 _push_tasks('fake_suite', 1)
Prathmesh Prabhu8f43d312020-03-07 00:25:06 -0800135
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 Wu835dee22017-09-07 10:47:29 -0700146
147
Prathmesh Prabhu89c814e2020-03-07 00:12:43 -0800148def _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 Wu835dee22017-09-07 10:47:29 -0700155if __name__ == '__main__':
156 unittest.main()