1# Copyright (C) 2011 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"""Handle messages from the TestRunner and execute actual tests."""
30
31import logging
32import sys
33import time
34
35from webkitpy.common.system import stack_utils
36
37from webkitpy.layout_tests.layout_package import manager_worker_broker
38from webkitpy.layout_tests.layout_package import worker_mixin
39
40
41_log = logging.getLogger(__name__)
42
43
44class Worker(manager_worker_broker.AbstractWorker, worker_mixin.WorkerMixin):
45    def __init__(self, worker_connection, worker_number, options):
46        self._worker_connection = worker_connection
47        self._worker_number = worker_number
48        self._options = options
49        self._name = 'worker/%d' % worker_number
50        self._done = False
51        self._canceled = False
52        self._port = None
53
54    def __del__(self):
55        self.cleanup()
56
57    def cancel(self):
58        """Attempt to abort processing (best effort)."""
59        self._canceled = True
60
61    def is_done(self):
62        return self._done or self._canceled
63
64    def name(self):
65        return self._name
66
67    def run(self, port):
68        self.safe_init(port)
69
70        exception_msg = ""
71        _log.debug("%s starting" % self._name)
72
73        try:
74            self._worker_connection.run_message_loop()
75            if not self.is_done():
76                raise AssertionError("%s: ran out of messages in worker queue."
77                                     % self._name)
78        except KeyboardInterrupt:
79            exception_msg = ", interrupted"
80        except:
81            exception_msg = ", exception raised"
82        finally:
83            _log.debug("%s done%s" % (self._name, exception_msg))
84            if exception_msg:
85                exception_type, exception_value, exception_traceback = sys.exc_info()
86                stack_utils.log_traceback(_log.error, exception_traceback)
87                # FIXME: Figure out how to send a message with a traceback.
88                self._worker_connection.post_message('exception',
89                    (exception_type, exception_value, None))
90            self._worker_connection.post_message('done')
91
92    def handle_test_list(self, src, list_name, test_list):
93        if list_name == "tests_to_http_lock":
94            self.start_servers_with_lock()
95
96        start_time = time.time()
97        num_tests = 0
98        for test_input in test_list:
99            self._run_test(test_input)
100            num_tests += 1
101            self._worker_connection.yield_to_broker()
102
103        elapsed_time = time.time() - start_time
104        self._worker_connection.post_message('finished_list', list_name, num_tests, elapsed_time)
105
106        if self._has_http_lock:
107            self.stop_servers_with_lock()
108
109    def handle_stop(self, src):
110        self._done = True
111
112    def _run_test(self, test_input):
113        test_timeout_sec = self.timeout(test_input)
114        start = time.time()
115        self._worker_connection.post_message('started_test', test_input, test_timeout_sec)
116
117        result = self.run_test_with_timeout(test_input, test_timeout_sec)
118
119        elapsed_time = time.time() - start
120        self._worker_connection.post_message('finished_test', result, elapsed_time)
121
122        self.clean_up_after_test(test_input, result)
123