1 # Copyright (c) 2009 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
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
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.
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.
34 import unittest2 as unittest
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, TerminateQueue
41 class LoggingDelegate(QueueEngineDelegate):
42 def __init__(self, test):
45 self._run_before = False
46 self.stop_message = None
48 expected_callbacks = [
51 'should_continue_work_queue',
55 'should_continue_work_queue',
59 def record(self, method_name):
60 self._callbacks.append(method_name)
62 def queue_log_path(self):
63 self.record("queue_log_path")
64 return os.path.join(self._test.temp_dir, "queue_log_path")
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" % work_item)
70 def begin_work_queue(self):
71 self.record("begin_work_queue")
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
80 def next_work_item(self):
81 self.record("next_work_item")
84 def process_work_item(self, work_item):
85 self.record("process_work_item")
86 self._test.assertEqual(work_item, "work_item")
89 def handle_unexpected_error(self, work_item, message):
90 self.record("handle_unexpected_error")
91 self._test.assertEqual(work_item, "work_item")
93 def stop_work_queue(self, message):
94 self.record("stop_work_queue")
95 self.stop_message = message
98 class RaisingDelegate(LoggingDelegate):
99 def __init__(self, test, exception):
100 LoggingDelegate.__init__(self, test)
101 self._exception = exception
103 def process_work_item(self, work_item):
104 self.record("process_work_item")
105 raise self._exception
108 class FastQueueEngine(QueueEngine):
109 def __init__(self, delegate):
110 QueueEngine.__init__(self, "fast-queue", delegate, threading.Event())
112 # No sleep for the wicked.
115 def _sleep(self, message):
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_path")))
126 self.assertTrue(os.path.exists(os.path.join(self.temp_dir, "work_log_path", "work_item.log")))
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 starts
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)
138 def test_handled_error(self):
139 delegate = RaisingDelegate(self, ScriptError(exit_code=QueueEngine.handled_error_code))
140 self._run_engine(delegate)
141 self.assertEqual(delegate._callbacks, LoggingDelegate.expected_callbacks)
143 def _run_engine(self, delegate, engine=None, termination_message=None):
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)
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")
158 delegate = RaisingDelegate(self, exception)
159 self._run_engine(delegate, termination_message=termination_message)
161 self.assertEqual(delegate._callbacks, expected_callbacks)
162 self.assertEqual(delegate.stop_message, termination_message)
164 def test_terminating_error(self):
165 self._test_terminating_queue(KeyboardInterrupt(), "User terminated queue.")
166 self._test_terminating_queue(TerminateQueue(), "TerminateQueue exception received.")
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)
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 (2 mins)."
177 self.assertEqual(engine._sleep_message("MESSAGE"), expected_sleep_message)
180 self.temp_dir = tempfile.mkdtemp(suffix="work_queue_test_logs")
183 shutil.rmtree(self.temp_dir)