1# Copyright 2024 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5from __future__ import annotations 6 7import datetime as dt 8from typing import TYPE_CHECKING, Type 9 10from crossbench.action_runner.action.action import (ACTION_TIMEOUT, Action, 11 ActionT) 12from crossbench.action_runner.action.action_type import ActionType 13from crossbench.action_runner.action.enums import ReadyState 14 15if TYPE_CHECKING: 16 from crossbench.action_runner.base import ActionRunner 17 from crossbench.config import ConfigParser 18 from crossbench.runner.run import Run 19 from crossbench.types import JsonDict 20 21 22class WaitForReadyStateAction(Action): 23 TYPE: ActionType = ActionType.WAIT_FOR_READY_STATE 24 25 @classmethod 26 def config_parser(cls: Type[ActionT]) -> ConfigParser[ActionT]: 27 parser = super().config_parser() 28 parser.add_argument( 29 "ready_state", type=ReadyState.parse, default=ReadyState.COMPLETE) 30 return parser 31 32 def __init__(self, 33 timeout: dt.timedelta = ACTION_TIMEOUT, 34 ready_state: ReadyState = ReadyState.COMPLETE, 35 index: int = 0): 36 self._ready_state = ready_state 37 super().__init__(timeout, index) 38 39 @property 40 def ready_state(self) -> ReadyState: 41 return self._ready_state 42 43 def run_with(self, run: Run, action_runner: ActionRunner) -> None: 44 action_runner.wait_for_ready_state(run, self) 45 46 def to_json(self) -> JsonDict: 47 details = super().to_json() 48 details["ready_state"] = str(self.ready_state) 49 return details 50