1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import dataclasses
import json
import logging
import os
import time
from typing import Any
# vpython-provided modules.
import bs4 # pylint: disable=import-error
import gpu_path_util
from gpu_tests import common_typing as ct
from gpu_tests import skia_gold_integration_test_base as sgitb
from gpu_tests import webgl_test_util
from gpu_tests.util import websocket_server as wss
from gpu_tests.util import websocket_utils
TEST_PAGE_RELPATH = os.path.join(webgl_test_util.extensions_relpath,
'pixel_test_page.html')
DEFAULT_HEARTBEAT_TIMEOUT = 15
SLOW_HEARTBEAT_MULTIPLIER = 8
DEFAULT_CONTROLLER_MESSAGE_TIMEOUT = 5
SLOW_CONTROLLER_MESSAGE_MULTIPLIER = 5
ASAN_MESSAGE_LOOP_MULTIPLIER = 1.5
DEFAULT_GLOBAL_TIMEOUT = 300
@dataclasses.dataclass
class LoopState:
"""Stores the state of the websocket heartbeat loop.
Used to allow nested loopes, e.g. during pixel test page actions.
"""
test_started: bool = False
test_finished: bool = False
@dataclasses.dataclass
class TabData:
"""Stores all the components for interacting with a tab."""
tab: ct.Tab
websocket_server: wss.WebsocketServer
is_default_tab: bool = False
class TestAction():
"""Defines some action to run before capturing a screenshot.
Tests are able to define custom lists of actions if additional steps are
necessary to run the test after loading the test page.
"""
def Run(self, test_case: 'SkiaGoldHeartbeatTestCase', tab_data: TabData,
loop_state: LoopState,
test_instance: 'SkiaGoldHeartbeatIntegrationTestBase') -> None:
raise NotImplementedError()
class _TestActionHandleMessageLoop(TestAction):
def __init__(self, timeout: float):
super().__init__()
self.timeout = timeout
def Run(self, test_case: 'SkiaGoldHeartbeatTestCase', tab_data: TabData,
loop_state: LoopState,
test_instance: 'SkiaGoldHeartbeatIntegrationTestBase') -> None:
test_instance.HandleMessageLoop(self.timeout, tab_data, loop_state)
class TestActionWaitForContinue(_TestActionHandleMessageLoop):
"""Handles the heartbeat message loop and waits for a CONTINUE signal."""
def Run(self, test_case: 'SkiaGoldHeartbeatTestCase', tab_data: TabData,
loop_state: LoopState,
test_instance: 'SkiaGoldHeartbeatIntegrationTestBase') -> None:
super().Run(test_case, tab_data, loop_state, test_instance)
test_instance.assertFalse(loop_state.test_finished)
class TestActionWaitForFinish(_TestActionHandleMessageLoop):
"""Handles the heartbeat message loop and waits for the test to finish."""
def Run(self, test_case: 'SkiaGoldHeartbeatTestCase', tab_data: TabData,
loop_state: LoopState,
test_instance: 'SkiaGoldHeartbeatIntegrationTestBase') -> None:
super().Run(test_case, tab_data, loop_state, test_instance)
test_instance.assertTrue(loop_state.test_finished)
class TestActionRunJavaScript(TestAction):
"""Evaluates the given JavaScript in the test page's iframe."""
def __init__(self, javascript: str):
super().__init__()
self.javascript = javascript
def Run(self, test_case: 'SkiaGoldHeartbeatTestCase', tab_data: TabData,
loop_state: LoopState,
test_instance: 'SkiaGoldHeartbeatIntegrationTestBase') -> None:
EvalInTestIframe(tab_data.tab, self.javascript)
class TestActionWaitForInnerTestPageLoad(TestAction):
"""Waits for the test page's iframe to completely load.
Most of the time, this isn't necessary since the test page will notify the
test harness when it is either done or ready to continue. However, if a test
needs to be manually started via a JavaScript function, then we need to wait
for a full page load before doing that.
"""
def __init__(self, timeout: float = 10):
super().__init__()
self.timeout = timeout
def Run(self, test_case: 'SkiaGoldHeartbeatTestCase', tab_data: TabData,
loop_state: LoopState,
test_instance: 'SkiaGoldHeartbeatIntegrationTestBase') -> None:
tab_data.tab.WaitForJavaScriptCondition('testIframeLoaded',
timeout=self.timeout)
class SkiaGoldHeartbeatTestCase(sgitb.SkiaGoldTestCase):
def __init__(self,
name: str,
*args,
test_actions: list[TestAction] | None = None,
**kwargs):
super().__init__(name, *args, **kwargs)
if test_actions:
self.test_actions = test_actions
self.used_custom_test_actions = True
else:
self.test_actions = [TestActionWaitForFinish(DEFAULT_GLOBAL_TIMEOUT)]
self.used_custom_test_actions = False
class SkiaGoldHeartbeatIntegrationTestBase(sgitb.SkiaGoldIntegrationTestBase):
"""Base class for tests that upload results to Skia Gold and use heartbeats.
Tests are run using a fixed test page that the actual test page is loaded into
using an iframe.
"""
websocket_server: wss.WebsocketServer | None = None
page_loaded = False
reload_page_for_next_navigation = False
@classmethod
def SetUpProcess(cls) -> None:
super().SetUpProcess()
# Logging every time a connection is opened/closed is spammy, so decrease
# the default log level.
logging.getLogger('websockets.server').setLevel(logging.WARNING)
cls.websocket_server = wss.WebsocketServer()
cls.websocket_server.StartServer()
@classmethod
def TearDownProcess(cls) -> None:
cls.websocket_server.StopServer()
cls.websocket_server = None
super().TearDownProcess()
@classmethod
def StartBrowser(cls) -> None:
cls.page_loaded = False
super().StartBrowser()
@classmethod
def StopBrowser(cls) -> None:
if cls.websocket_server:
cls.websocket_server.ClearCurrentConnection()
super().StopBrowser()
@classmethod
def _ForceRefreshForNextNavigation(cls) -> None:
if cls.websocket_server:
cls.websocket_server.ClearCurrentConnection()
cls.page_loaded = False
def RunActualGpuTest(self, test_path: str, args: ct.TestArgs) -> None:
cls = self.__class__
if cls.reload_page_for_next_navigation:
self._ForceRefreshForNextNavigation()
test_case = args[0]
cls.reload_page_for_next_navigation = test_case.refresh_after_finish
def NavigateTo(self, test_path: str, tab_data: TabData) -> None:
"""Navigates the given |test_path| URL in the test iframe.
Will navigate the browser to the test page with the iframe first under the
following conditions:
* This is being run in the primary tab and the page has not been loaded
since the last browser restart.
* This is being run in a secondary tab
Args:
test_path: A string containing the test file to navigate to.
tab_data: The tab and websocket server to use when loading the test page.
"""
tab = tab_data.tab
websocket_server = tab_data.websocket_server
if not tab_data.is_default_tab or (tab_data.is_default_tab
and not self.__class__.page_loaded):
# If we haven't loaded the test page that we use to run tests within an
# iframe, load it and establish the websocket connection.
url = self.UrlOfStaticFilePath(TEST_PAGE_RELPATH)
tab.Navigate(
url,
script_to_evaluate_on_commit=self._dom_automation_controller_script)
tab.WaitForDocumentReadyStateToBeComplete(timeout=5)
tab.action_runner.EvaluateJavaScript(
f'connectWebsocket("{websocket_server.server_port}")', timeout=5)
websocket_server.WaitForConnection(
websocket_utils.GetScaledConnectionTimeout(self.child.jobs))
response = websocket_server.Receive(5)
response = json.loads(response)
assert response['type'] == 'CONNECTION_ACK'
if tab_data.is_default_tab:
self.__class__.page_loaded = True
url = self.UrlOfStaticFilePath(test_path)
initial_scaling = PageHasViewportInitialScaling(test_path)
tab.action_runner.EvaluateJavaScript(f'runTest("{url}", {initial_scaling})')
# pylint: disable=too-many-branches
def HandleMessageLoop(self, test_timeout: float, tab_data: TabData,
loop_state: LoopState) -> None:
"""Handles the websocket message loop until an error or requested break.
Args:
test_timeout: How long the loop is allowed to run for in seconds before
raising an error.
tab_data: If set, the provided tab/websocket server will be used
instead of the ones associated with the primary tab.
loop_state: The LoopState to use. Will be modified in place.
"""
test_timeout = test_timeout * self._GetMessageLoopTimeoutMultiplier()
tab = tab_data.tab
websocket_server = tab_data.websocket_server
start_time = time.time()
try:
while True:
response = websocket_server.Receive(self._GetHeartbeatTimeout())
response = json.loads(response)
response_type = response['type']
if time.time() - start_time > test_timeout:
raise RuntimeError(
f'Hit {test_timeout:.3f} second global timeout, but page '
f'continued to send messages over the websocket, i.e. was not '
f'due to a renderer crash.')
if response_type == 'TEST_STARTED':
VerifyMessageOrderTestStarted(loop_state)
continue
if response_type == 'TEST_HEARTBEAT':
VerifyMessageOrderTestHeartbeat(loop_state)
continue
# Used by several tests that have custom logic. Indicates that the
# JavaScript code has reached some pre-determined point and is ready for
# the Python code to do something.
# PERFORM_PAGE_ACTION is a legacy response from when only a single
# interrupt during a test was allowed, but is now equivalent to
# TEST_CONTINUE
if response_type in ('TEST_CONTINUE', 'PERFORM_PAGE_ACTION'):
VerifyMessageOrderTestContinue(loop_state)
break
if response_type == 'TEST_FINISHED':
VerifyMessageOrderTestFinished(loop_state)
success = response['success']
if not success:
self.fail('Page reported failure')
break
raise RuntimeError(f'Received unknown message type {response_type}')
except wss.WebsocketReceiveMessageTimeoutError:
websocket_utils.HandleWebsocketReceiveTimeoutError(tab, start_time)
raise
except wss.ClientClosedConnectionError as e:
websocket_utils.HandlePrematureSocketClose(e, start_time)
finally:
try:
test_messages = tab.EvaluateJavaScript(
'domAutomationController._messages',
timeout=self._GetControllerMessageTimeout())
if test_messages:
logging.info('Logging messages from the test:\n%s', test_messages)
except Exception: # pylint:disable=broad-except
logging.warning('Could not retrieve messages from test page.')
# pylint: enable=too-many-branches
def _GetMessageLoopTimeoutMultiplier(self) -> float:
multiplier = 1
if self._is_asan:
multiplier = ASAN_MESSAGE_LOOP_MULTIPLIER
return multiplier
def _GetHeartbeatTimeout(self) -> int:
multiplier = 1
if self._IsSlowTest():
multiplier = SLOW_HEARTBEAT_MULTIPLIER
return DEFAULT_HEARTBEAT_TIMEOUT * multiplier
def _GetControllerMessageTimeout(self) -> int:
multiplier = 1
if self._IsSlowTest():
multiplier = SLOW_CONTROLLER_MESSAGE_MULTIPLIER
return DEFAULT_CONTROLLER_MESSAGE_TIMEOUT * multiplier
def _IsSlowTest(self) -> bool:
# We access the expectations directly instead of using
# self.GetExpectationsForTest since we need the raw results, but that method
# only returns the parsed results and whether the test should be retried.
expectation = self.child.expectations.expectations_for(self.shortName())
return 'Slow' in expectation.raw_results
# Pytype erroneously claims that bs4 elements can't be accessed with [].
# pytype: disable=unsupported-operands
def PageHasViewportInitialScaling(test_path: str) -> float:
"""Determines initial viewport scaling, if any, from |test_path|.
Args:
test_path: A src-relative filepath to a test HTML file to check.
Returns:
The initial scaling value specified by |test_path|. Returns 0 if no relevant
viewport scaling information can be found.
"""
# Some test paths include URL arguments, so strip those off.
test_path = test_path.split('?', 1)[0]
filepath = os.path.join(gpu_path_util.CHROMIUM_SRC_DIR, test_path)
with open(filepath, encoding='utf-8') as infile:
contents = infile.read()
soup = bs4.BeautifulSoup(contents, 'html.parser')
for meta_tag in soup.find_all('meta'):
if meta_tag['name'] != 'viewport':
continue
for content_piece in meta_tag['content'].split():
if not content_piece.startswith('initial-scale'):
continue
return float(content_piece.split('=')[1])
return 0
# pytype: enable=unsupported-operands
def EvalInTestIframe(tab: ct.Tab, expression: str) -> Any:
escaped_expression = expression.replace('`', '\\`')
eval_expression = f'evalInIframe(`{escaped_expression}`)'
return tab.EvaluateJavaScript(eval_expression)
def VerifyMessageOrderTestStarted(loop_state: LoopState) -> None:
if loop_state.test_started:
raise RuntimeError('Received multiple start messages for one test')
loop_state.test_started = True
def VerifyMessageOrderTestHeartbeat(loop_state: LoopState) -> None:
if not loop_state.test_started:
raise RuntimeError('Received heartbeat before test start')
if loop_state.test_finished:
raise RuntimeError('Received heartbeat after test finished')
def VerifyMessageOrderTestContinue(loop_state: LoopState) -> None:
if not loop_state.test_started:
raise RuntimeError('Received continue before test start')
if loop_state.test_finished:
raise RuntimeError('Received continue after test finished')
def VerifyMessageOrderTestFinished(loop_state: LoopState) -> None:
if not loop_state.test_started:
raise RuntimeError('Received test finish before test start')
if loop_state.test_finished:
raise RuntimeError('Received multiple finish messages for one test')
loop_state.test_finished = True
|