OLD | NEW |
| (Empty) |
1 # Copyright (c) 2009 Google Inc. All rights reserved. | |
2 # | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
7 # * Redistributions of source code must retain the above copyright | |
8 # notice, this list of conditions and the following disclaimer. | |
9 # * Redistributions in binary form must reproduce the above | |
10 # copyright notice, this list of conditions and the following disclaimer | |
11 # in the documentation and/or other materials provided with the | |
12 # distribution. | |
13 # * Neither the name of Google Inc. nor the names of its | |
14 # contributors may be used to endorse or promote products derived from | |
15 # this software without specific prior written permission. | |
16 # | |
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 import datetime | |
30 import os | |
31 import shutil | |
32 import tempfile | |
33 import threading | |
34 import unittest2 as unittest | |
35 | |
36 from webkitpy.common.system.executive import ScriptError | |
37 from webkitpy.common.system.outputcapture import OutputCapture | |
38 from webkitpy.tool.bot.queueengine import QueueEngine, QueueEngineDelegate, Term
inateQueue | |
39 | |
40 | |
41 class LoggingDelegate(QueueEngineDelegate): | |
42 def __init__(self, test): | |
43 self._test = test | |
44 self._callbacks = [] | |
45 self._run_before = False | |
46 self.stop_message = None | |
47 | |
48 expected_callbacks = [ | |
49 'queue_log_path', | |
50 'begin_work_queue', | |
51 'should_continue_work_queue', | |
52 'next_work_item', | |
53 'work_item_log_path', | |
54 'process_work_item', | |
55 'should_continue_work_queue', | |
56 'stop_work_queue', | |
57 ] | |
58 | |
59 def record(self, method_name): | |
60 self._callbacks.append(method_name) | |
61 | |
62 def queue_log_path(self): | |
63 self.record("queue_log_path") | |
64 return os.path.join(self._test.temp_dir, "queue_log_path") | |
65 | |
66 def work_item_log_path(self, work_item): | |
67 self.record("work_item_log_path") | |
68 return os.path.join(self._test.temp_dir, "work_log_path", "%s.log" % wor
k_item) | |
69 | |
70 def begin_work_queue(self): | |
71 self.record("begin_work_queue") | |
72 | |
73 def should_continue_work_queue(self): | |
74 self.record("should_continue_work_queue") | |
75 if not self._run_before: | |
76 self._run_before = True | |
77 return True | |
78 return False | |
79 | |
80 def next_work_item(self): | |
81 self.record("next_work_item") | |
82 return "work_item" | |
83 | |
84 def process_work_item(self, work_item): | |
85 self.record("process_work_item") | |
86 self._test.assertEqual(work_item, "work_item") | |
87 return True | |
88 | |
89 def handle_unexpected_error(self, work_item, message): | |
90 self.record("handle_unexpected_error") | |
91 self._test.assertEqual(work_item, "work_item") | |
92 | |
93 def stop_work_queue(self, message): | |
94 self.record("stop_work_queue") | |
95 self.stop_message = message | |
96 | |
97 | |
98 class RaisingDelegate(LoggingDelegate): | |
99 def __init__(self, test, exception): | |
100 LoggingDelegate.__init__(self, test) | |
101 self._exception = exception | |
102 | |
103 def process_work_item(self, work_item): | |
104 self.record("process_work_item") | |
105 raise self._exception | |
106 | |
107 | |
108 class FastQueueEngine(QueueEngine): | |
109 def __init__(self, delegate): | |
110 QueueEngine.__init__(self, "fast-queue", delegate, threading.Event()) | |
111 | |
112 # No sleep for the wicked. | |
113 seconds_to_sleep = 0 | |
114 | |
115 def _sleep(self, message): | |
116 pass | |
117 | |
118 | |
119 class QueueEngineTest(unittest.TestCase): | |
120 def test_trivial(self): | |
121 delegate = LoggingDelegate(self) | |
122 self._run_engine(delegate) | |
123 self.assertEqual(delegate.stop_message, "Delegate terminated queue.") | |
124 self.assertEqual(delegate._callbacks, LoggingDelegate.expected_callbacks
) | |
125 self.assertTrue(os.path.exists(os.path.join(self.temp_dir, "queue_log_pa
th"))) | |
126 self.assertTrue(os.path.exists(os.path.join(self.temp_dir, "work_log_pat
h", "work_item.log"))) | |
127 | |
128 def test_unexpected_error(self): | |
129 delegate = RaisingDelegate(self, ScriptError(exit_code=3)) | |
130 self._run_engine(delegate) | |
131 expected_callbacks = LoggingDelegate.expected_callbacks[:] | |
132 work_item_index = expected_callbacks.index('process_work_item') | |
133 # The unexpected error should be handled right after process_work_item s
tarts | |
134 # but before any other callback. Otherwise callbacks should be normal. | |
135 expected_callbacks.insert(work_item_index + 1, 'handle_unexpected_error'
) | |
136 self.assertEqual(delegate._callbacks, expected_callbacks) | |
137 | |
138 def test_handled_error(self): | |
139 delegate = RaisingDelegate(self, ScriptError(exit_code=QueueEngine.handl
ed_error_code)) | |
140 self._run_engine(delegate) | |
141 self.assertEqual(delegate._callbacks, LoggingDelegate.expected_callbacks
) | |
142 | |
143 def _run_engine(self, delegate, engine=None, termination_message=None): | |
144 if not engine: | |
145 engine = QueueEngine("test-queue", delegate, threading.Event()) | |
146 if not termination_message: | |
147 termination_message = "Delegate terminated queue." | |
148 expected_logs = "\n%s\n" % termination_message | |
149 OutputCapture().assert_outputs(self, engine.run, expected_logs=expected_
logs) | |
150 | |
151 def _test_terminating_queue(self, exception, termination_message): | |
152 work_item_index = LoggingDelegate.expected_callbacks.index('process_work
_item') | |
153 # The terminating error should be handled right after process_work_item. | |
154 # There should be no other callbacks after stop_work_queue. | |
155 expected_callbacks = LoggingDelegate.expected_callbacks[:work_item_index
+ 1] | |
156 expected_callbacks.append("stop_work_queue") | |
157 | |
158 delegate = RaisingDelegate(self, exception) | |
159 self._run_engine(delegate, termination_message=termination_message) | |
160 | |
161 self.assertEqual(delegate._callbacks, expected_callbacks) | |
162 self.assertEqual(delegate.stop_message, termination_message) | |
163 | |
164 def test_terminating_error(self): | |
165 self._test_terminating_queue(KeyboardInterrupt(), "User terminated queue
.") | |
166 self._test_terminating_queue(TerminateQueue(), "TerminateQueue exception
received.") | |
167 | |
168 def test_now(self): | |
169 """Make sure there are no typos in the QueueEngine.now() method.""" | |
170 engine = QueueEngine("test", None, None) | |
171 self.assertIsInstance(engine._now(), datetime.datetime) | |
172 | |
173 def test_sleep_message(self): | |
174 engine = QueueEngine("test", None, None) | |
175 engine._now = lambda: datetime.datetime(2010, 1, 1) | |
176 expected_sleep_message = "MESSAGE Sleeping until 2010-01-01 00:02:00 (12
0 seconds)." | |
177 self.assertEqual(engine._sleep_message("MESSAGE"), expected_sleep_messag
e) | |
178 | |
179 def setUp(self): | |
180 self.temp_dir = tempfile.mkdtemp(suffix="work_queue_test_logs") | |
181 | |
182 def tearDown(self): | |
183 shutil.rmtree(self.temp_dir) | |
OLD | NEW |