• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.parse import DurationParser
14
15if TYPE_CHECKING:
16  from crossbench.config import ConfigParser
17  from crossbench.types import JsonDict
18
19
20class BaseDurationAction(Action):
21
22  def __init__(self,
23               duration: dt.timedelta,
24               timeout: dt.timedelta = ACTION_TIMEOUT,
25               index: int = 0) -> None:
26    self._duration: dt.timedelta = duration
27    super().__init__(timeout, index)
28
29  @property
30  def duration(self) -> dt.timedelta:
31    return self._duration
32
33  def validate(self) -> None:
34    super().validate()
35    self.validate_duration()
36
37  def validate_duration(self) -> None:
38    if self.duration.total_seconds() <= 0:
39      raise ValueError(
40          f"{self}.duration should be positive, but got {self.duration}")
41
42  def to_json(self) -> JsonDict:
43    details = super().to_json()
44    details["duration"] = self.duration.total_seconds()
45    return details
46
47
48class DurationAction(BaseDurationAction):
49  TYPE: ActionType = ActionType.WAIT
50
51  @classmethod
52  def config_parser(cls: Type[ActionT]) -> ConfigParser[ActionT]:
53    parser = super().config_parser()
54    parser.add_argument(
55        "duration", type=DurationParser.positive_duration, required=True)
56    return parser
57