MIL-007: resilience against network failures #46
@@ -3,10 +3,12 @@
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
|
||||
from snack_and_lift import constants
|
||||
from snack_and_lift.application.ports import GymPort
|
||||
from snack_and_lift.application.retry import Sleep, retry
|
||||
from snack_and_lift.domain.errors import GymError
|
||||
from snack_and_lift.domain.models import ButtonState, ClassCard, ClassResult
|
||||
from snack_and_lift.domain.rules import (
|
||||
@@ -41,12 +43,68 @@ def book_class(gym: GymPort, card: ClassCard) -> ClassResult:
|
||||
return ClassResult(card, outcome)
|
||||
|
||||
|
||||
def book_next_tuesday(gym: GymPort) -> ClassResult | None:
|
||||
def _read_current_card(gym: GymPort, card: ClassCard) -> ClassCard:
|
||||
"""@brief Read a class again from the page, to see what an earlier attempt did.
|
||||
|
||||
@param gym The gym site.
|
||||
@param card The class, as it was read before.
|
||||
@return The class with the state the page shows now.
|
||||
@exception GymError The schedule could not be read or the class is no longer on it.
|
||||
"""
|
||||
for current in gym.list_class_cards():
|
||||
if current.card_id == card.card_id:
|
||||
return current
|
||||
raise GymError(constants.MESSAGE_CLASS_NOT_ON_SCHEDULE.format(card_id=card.card_id))
|
||||
|
||||
|
||||
def book_class_with_retry(
|
||||
gym: GymPort,
|
||||
card: ClassCard,
|
||||
retries: int = constants.RETRY_COUNT,
|
||||
sleep: Sleep = time.sleep,
|
||||
) -> ClassResult:
|
||||
"""@brief Book a class, repeating it when the site drops the request.
|
||||
|
||||
Before each repeat the class is read from the page again. When the button now shows the
|
||||
state that the click should give, the click did go through although its confirmation
|
||||
failed, so the class counts as newly booked or waitlisted and is not clicked again.
|
||||
|
||||
@param gym The gym site.
|
||||
@param card The class, as read from the page.
|
||||
@param retries The most attempts to make.
|
||||
@param sleep Waits between two attempts.
|
||||
@return The class and what happened.
|
||||
@exception RetriesExhaustedError Every attempt failed.
|
||||
"""
|
||||
attempts = 0
|
||||
|
||||
def attempt() -> ClassResult:
|
||||
nonlocal attempts
|
||||
attempts += 1
|
||||
if attempts == 1:
|
||||
return book_class(gym, card)
|
||||
current = _read_current_card(gym, card)
|
||||
action = decide_outcome_action(card)
|
||||
if CONFIRMED_STATE.get(action) is current.state:
|
||||
outcome = OUTCOME_OF_ACTION[action]
|
||||
logger.info(format_outcome(outcome, card))
|
||||
return ClassResult(card, outcome)
|
||||
return book_class(gym, current)
|
||||
|
||||
description = constants.RETRY_DESCRIPTION_BOOK.format(name=card.name, day=card.day_label)
|
||||
return retry(attempt, retries, description, sleep=sleep)
|
||||
|
||||
|
||||
def book_next_tuesday(
|
||||
gym: GymPort, retries: int = constants.RETRY_COUNT, sleep: Sleep = time.sleep
|
||||
) -> ClassResult | None:
|
||||
"""@brief Book the next Tuesday class at the target time.
|
||||
|
||||
@param gym The gym site.
|
||||
@param retries The most attempts to book the class.
|
||||
@param sleep Waits between two attempts.
|
||||
@return The result, or None when the schedule has no such class.
|
||||
@exception GymError The page could not be read or the booking failed.
|
||||
@exception GymError The page could not be read or the booking failed on every attempt.
|
||||
"""
|
||||
card = find_next_class(gym.list_class_cards(), constants.NEXT_CLASS_WEEKDAY)
|
||||
if card is None:
|
||||
@@ -57,11 +115,14 @@ def book_next_tuesday(gym: GymPort) -> ClassResult | None:
|
||||
)
|
||||
)
|
||||
return None
|
||||
return book_class(gym, card)
|
||||
return book_class_with_retry(gym, card, retries, sleep)
|
||||
|
||||
|
||||
def book_target_classes(
|
||||
gym: GymPort, on_result: Callable[[ClassResult], None] | None = None
|
||||
gym: GymPort,
|
||||
on_result: Callable[[ClassResult], None] | None = None,
|
||||
retries: int = constants.RETRY_COUNT,
|
||||
sleep: Sleep = time.sleep,
|
||||
) -> list[ClassResult]:
|
||||
"""@brief Book every Tuesday and Thursday class at the target time, in page order.
|
||||
|
||||
@@ -69,8 +130,10 @@ def book_target_classes(
|
||||
|
||||
@param gym The gym site.
|
||||
@param on_result Called after each class was handled, for example to take a screenshot.
|
||||
@param retries The most attempts to book one class.
|
||||
@param sleep Waits between two attempts.
|
||||
@return What happened to each class; empty when the schedule has none.
|
||||
@exception GymError The page could not be read or a booking failed.
|
||||
@exception GymError The page could not be read or a booking failed on every attempt.
|
||||
"""
|
||||
cards = find_target_classes(gym.list_class_cards())
|
||||
if not cards:
|
||||
@@ -78,7 +141,7 @@ def book_target_classes(
|
||||
return []
|
||||
results: list[ClassResult] = []
|
||||
for card in cards:
|
||||
result = book_class(gym, card)
|
||||
result = book_class_with_retry(gym, card, retries, sleep)
|
||||
results.append(result)
|
||||
if on_result is not None:
|
||||
on_result(result)
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
"""@file retry.py
|
||||
@brief Lecture step 9: repeat a step that failed because the site dropped a request.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
|
||||
from snack_and_lift import constants
|
||||
from snack_and_lift.domain.errors import GymError, RetriesExhaustedError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
## Waits for a number of seconds; tests pass a recorder so that they never really sleep.
|
||||
Sleep = Callable[[float], None]
|
||||
|
||||
|
||||
def retry[T](
|
||||
func: Callable[[], T],
|
||||
retries: int = constants.RETRY_COUNT,
|
||||
description: str | None = None,
|
||||
*,
|
||||
delay: float = constants.RETRY_DELAY_SECONDS,
|
||||
sleep: Sleep = time.sleep,
|
||||
) -> T:
|
||||
"""@brief Call a function until it succeeds, at most `retries` times.
|
||||
|
||||
Only a `GymError` is repeated: it is what a dropped request looks like. Any other error,
|
||||
such as missing credentials, is a defect and passes through at once.
|
||||
|
||||
@param func The step, called without arguments.
|
||||
@param retries The most attempts to make, at least 1.
|
||||
@param description What the step does, for the log (never put a secret in it).
|
||||
@param delay Seconds to wait between two attempts.
|
||||
@param sleep Waits for a number of seconds.
|
||||
@return What the function returned on its first successful attempt.
|
||||
@exception ValueError `retries` is below 1.
|
||||
@exception RetriesExhaustedError Every attempt raised a `GymError`; the last one is the cause.
|
||||
"""
|
||||
if retries < 1:
|
||||
raise ValueError(constants.MESSAGE_RETRIES_INVALID.format(retries=retries))
|
||||
name = description or constants.RETRY_DEFAULT_DESCRIPTION
|
||||
for attempt in range(1, retries + 1):
|
||||
try:
|
||||
result = func()
|
||||
except GymError as error:
|
||||
if attempt == retries:
|
||||
message = constants.MESSAGE_RETRIES_EXHAUSTED.format(
|
||||
description=name, retries=retries, error=error
|
||||
)
|
||||
raise RetriesExhaustedError(message) from error
|
||||
logger.warning(
|
||||
constants.MESSAGE_RETRY_FAILED.format(
|
||||
attempt=attempt,
|
||||
retries=retries,
|
||||
description=name,
|
||||
error=str(error).rstrip("."),
|
||||
)
|
||||
)
|
||||
sleep(delay)
|
||||
else:
|
||||
if attempt > 1:
|
||||
logger.info(
|
||||
constants.MESSAGE_RETRY_SUCCEEDED.format(description=name, attempts=attempt)
|
||||
)
|
||||
return result
|
||||
raise AssertionError("unreachable: the last attempt returns or raises") # pragma: no cover
|
||||
@@ -3,9 +3,12 @@
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
|
||||
from snack_and_lift import constants
|
||||
from snack_and_lift.application.credentials import Credentials
|
||||
from snack_and_lift.application.ports import GymPort
|
||||
from snack_and_lift.application.retry import Sleep, retry
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -19,3 +22,25 @@ def login(gym: GymPort, credentials: Credentials) -> None:
|
||||
"""
|
||||
gym.log_in(credentials.email, credentials.password)
|
||||
logger.info("✓ Logged in: the class schedule is shown")
|
||||
|
||||
|
||||
def login_with_retry(
|
||||
gym: GymPort,
|
||||
credentials: Credentials,
|
||||
retries: int = constants.RETRY_COUNT,
|
||||
sleep: Sleep = time.sleep,
|
||||
) -> None:
|
||||
"""@brief Log in, repeating the login when the site drops the request.
|
||||
|
||||
@param gym The gym site.
|
||||
@param credentials The account to log in with.
|
||||
@param retries The most attempts to make.
|
||||
@param sleep Waits between two attempts.
|
||||
@exception RetriesExhaustedError Every attempt failed.
|
||||
"""
|
||||
retry(
|
||||
lambda: login(gym, credentials),
|
||||
retries,
|
||||
constants.RETRY_DESCRIPTION_LOGIN,
|
||||
sleep=sleep,
|
||||
)
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
"""
|
||||
|
||||
import logging
|
||||
import time
|
||||
from collections.abc import Sequence
|
||||
|
||||
from snack_and_lift import constants
|
||||
from snack_and_lift.application.ports import GymPort
|
||||
from snack_and_lift.application.retry import Sleep, retry
|
||||
from snack_and_lift.domain.booking_entry import BookingEntry, is_target_entry
|
||||
from snack_and_lift.domain.errors import NoBookingsFoundError
|
||||
from snack_and_lift.domain.models import ClassResult
|
||||
@@ -30,18 +32,42 @@ def get_my_bookings(gym: GymPort) -> list[BookingEntry]:
|
||||
return entries
|
||||
|
||||
|
||||
def verify_bookings(gym: GymPort, results: Sequence[ClassResult]) -> VerificationResult:
|
||||
def get_my_bookings_with_retry(
|
||||
gym: GymPort, retries: int = constants.RETRY_COUNT, sleep: Sleep = time.sleep
|
||||
) -> list[BookingEntry]:
|
||||
"""@brief Read the My Bookings page, repeating it when the page fails to load.
|
||||
|
||||
@param gym The gym site.
|
||||
@param retries The most attempts to make.
|
||||
@param sleep Waits between two attempts.
|
||||
@return Every confirmed booking and waitlist entry.
|
||||
@exception RetriesExhaustedError Every attempt failed.
|
||||
"""
|
||||
return retry(
|
||||
lambda: get_my_bookings(gym), retries, constants.RETRY_DESCRIPTION_BOOKINGS, sleep=sleep
|
||||
)
|
||||
|
||||
|
||||
def verify_bookings(
|
||||
gym: GymPort,
|
||||
results: Sequence[ClassResult],
|
||||
retries: int = constants.RETRY_COUNT,
|
||||
sleep: Sleep = time.sleep,
|
||||
) -> VerificationResult:
|
||||
"""@brief Compare what the bot booked with the My Bookings page.
|
||||
|
||||
The page is not opened when the bot handled no class.
|
||||
|
||||
@param gym The gym site.
|
||||
@param results What happened to each class.
|
||||
@param retries The most attempts to read the page.
|
||||
@param sleep Waits between two attempts.
|
||||
@return The comparison of the expected entries with the Tuesday and Thursday 6pm entries.
|
||||
@exception NoBookingsFoundError Classes were handled but the page shows no entry.
|
||||
@exception GymError The page could not be opened or read.
|
||||
@exception RetriesExhaustedError The page could not be read on any attempt.
|
||||
"""
|
||||
if not results:
|
||||
return build_verification(results, [])
|
||||
entries = [entry for entry in get_my_bookings(gym) if is_target_entry(entry)]
|
||||
entries = [
|
||||
entry for entry in get_my_bookings_with_retry(gym, retries, sleep) if is_target_entry(entry)
|
||||
]
|
||||
return build_verification(results, entries)
|
||||
|
||||
+52
-13
@@ -6,6 +6,7 @@ import argparse
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from collections.abc import Callable, Sequence
|
||||
from pathlib import Path
|
||||
|
||||
@@ -13,7 +14,8 @@ from snack_and_lift import constants
|
||||
from snack_and_lift.application.booking import book_next_tuesday, book_target_classes
|
||||
from snack_and_lift.application.credentials import Credentials, load_credentials
|
||||
from snack_and_lift.application.ports import GymPort
|
||||
from snack_and_lift.application.session import login
|
||||
from snack_and_lift.application.retry import Sleep
|
||||
from snack_and_lift.application.session import login_with_retry
|
||||
from snack_and_lift.application.verification import verify_bookings
|
||||
from snack_and_lift.domain.errors import GymError, LoginFailedError, MissingCredentialsError
|
||||
from snack_and_lift.domain.models import ClassResult
|
||||
@@ -40,6 +42,22 @@ def selenium_gym_factory(headless: bool, profile_dir: Path, keep_open: bool) ->
|
||||
return SeleniumGym(create_driver(headless, profile_dir, keep_open))
|
||||
|
||||
|
||||
def _at_least_one(text: str) -> int:
|
||||
"""@brief Read a whole number of at least 1 for an option.
|
||||
|
||||
@param text The text of the option.
|
||||
@return The number.
|
||||
@exception argparse.ArgumentTypeError The text is not a whole number of at least 1.
|
||||
"""
|
||||
try:
|
||||
number = int(text)
|
||||
except ValueError:
|
||||
number = 0
|
||||
if number < 1:
|
||||
raise argparse.ArgumentTypeError(f"{text!r} is not a whole number of at least 1")
|
||||
return number
|
||||
|
||||
|
||||
def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
|
||||
"""@brief Read the command line.
|
||||
|
||||
@@ -50,8 +68,8 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
|
||||
prog="snack-and-lift",
|
||||
description="Books the Tuesday and Thursday 6pm classes at the Snack and Lift gym.",
|
||||
epilog=(
|
||||
"exit codes: 0 done, 1 a step failed, 2 credentials missing, "
|
||||
"3 the My Bookings page does not match the bookings made"
|
||||
"exit codes: 0 done, 1 a step failed (also when every retry failed), "
|
||||
"2 credentials missing, 3 the My Bookings page does not match the bookings made"
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
@@ -82,6 +100,16 @@ def parse_args(argv: Sequence[str] | None = None) -> argparse.Namespace:
|
||||
action="store_true",
|
||||
help="book only the next Tuesday 6pm class (lecture steps 3 and 4); skips the verification",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--retries",
|
||||
type=_at_least_one,
|
||||
default=constants.RETRY_COUNT,
|
||||
metavar="N",
|
||||
help=(
|
||||
"attempts for the login, each booking and the My Bookings page when the site "
|
||||
f"drops a request (default: {constants.RETRY_COUNT}, at least 1)"
|
||||
),
|
||||
)
|
||||
args = parser.parse_args(argv)
|
||||
if args.keep_open is None:
|
||||
args.keep_open = not args.headless
|
||||
@@ -121,7 +149,11 @@ def _save_console(gym: GymPort, screenshot_dir: Path | None, text: str, name: st
|
||||
|
||||
|
||||
def _run(
|
||||
gym: GymPort, args: argparse.Namespace, recorder: ConsoleRecorder, credentials: Credentials
|
||||
gym: GymPort,
|
||||
args: argparse.Namespace,
|
||||
recorder: ConsoleRecorder,
|
||||
credentials: Credentials,
|
||||
sleep: Sleep,
|
||||
) -> bool:
|
||||
"""@brief Open the site, log in, book the classes and verify them, saving the step images.
|
||||
|
||||
@@ -133,15 +165,16 @@ def _run(
|
||||
@param args The parsed options.
|
||||
@param recorder The recorded console lines, drawn for step 4.
|
||||
@param credentials The account to log in with.
|
||||
@param sleep Waits between two attempts of a retried step.
|
||||
@return False when the My Bookings page does not match the bookings made, else True.
|
||||
@exception GymError A step failed.
|
||||
@exception GymError A step failed on every attempt.
|
||||
"""
|
||||
gym.open_site()
|
||||
_save_step(gym, args.screenshot_dir, "step-1.png")
|
||||
login(gym, credentials)
|
||||
login_with_retry(gym, credentials, args.retries, sleep)
|
||||
_save_step(gym, args.screenshot_dir, "step-2.png")
|
||||
if args.first_tuesday_only:
|
||||
book_next_tuesday(gym)
|
||||
book_next_tuesday(gym, args.retries, sleep)
|
||||
_save_step(gym, args.screenshot_dir, constants.SCREENSHOT_STEP_3)
|
||||
_save_console(gym, args.screenshot_dir, recorder.text, constants.SCREENSHOT_STEP_4)
|
||||
return True
|
||||
@@ -153,7 +186,7 @@ def _run(
|
||||
_save_step(gym, args.screenshot_dir, constants.SCREENSHOT_STEP_3)
|
||||
window_saved = True
|
||||
|
||||
results = book_target_classes(gym, save_window_once)
|
||||
results = book_target_classes(gym, save_window_once, args.retries, sleep)
|
||||
if not window_saved:
|
||||
_save_step(gym, args.screenshot_dir, constants.SCREENSHOT_STEP_3)
|
||||
_save_console(gym, args.screenshot_dir, recorder.text, constants.SCREENSHOT_STEP_4)
|
||||
@@ -163,22 +196,25 @@ def _run(
|
||||
detail = format_detail_list(results)
|
||||
logger.info("%s", detail)
|
||||
_save_console(gym, args.screenshot_dir, f"{summary}\n{detail}", constants.SCREENSHOT_STEP_6)
|
||||
verification = verify_bookings(gym, results)
|
||||
verification = verify_bookings(gym, results, args.retries, sleep)
|
||||
_save_step(gym, args.screenshot_dir, constants.SCREENSHOT_STEP_7)
|
||||
verification_text = format_verification(verification)
|
||||
logger.info("%s", verification_text)
|
||||
_save_console(gym, args.screenshot_dir, verification_text, constants.SCREENSHOT_STEP_8)
|
||||
_save_console(gym, args.screenshot_dir, recorder.text, constants.SCREENSHOT_STEP_9)
|
||||
return verification.is_success
|
||||
|
||||
|
||||
def main(
|
||||
argv: Sequence[str] | None = None,
|
||||
gym_factory: GymFactory = selenium_gym_factory,
|
||||
sleep: Sleep = time.sleep,
|
||||
) -> int:
|
||||
"""@brief Start the bot: open the site, log in and book the next Tuesday 6pm class.
|
||||
"""@brief Start the bot: open the site, log in, book the classes and verify them.
|
||||
|
||||
@param argv Arguments without the program name; None reads `sys.argv`.
|
||||
@param gym_factory Builds the gym site; tests pass a fake.
|
||||
@param sleep Waits between two attempts of a retried step; tests pass a recorder.
|
||||
@return Process exit code: 0 done, 1 a step failed, 2 credentials missing, 3 mismatch.
|
||||
"""
|
||||
logging.basicConfig(level=logging.INFO, format="%(message)s")
|
||||
@@ -187,17 +223,20 @@ def main(
|
||||
recorder = ConsoleRecorder()
|
||||
root.addHandler(recorder)
|
||||
try:
|
||||
return _main(argv, gym_factory, recorder)
|
||||
return _main(argv, gym_factory, recorder, sleep)
|
||||
finally:
|
||||
root.removeHandler(recorder)
|
||||
|
||||
|
||||
def _main(argv: Sequence[str] | None, gym_factory: GymFactory, recorder: ConsoleRecorder) -> int:
|
||||
def _main(
|
||||
argv: Sequence[str] | None, gym_factory: GymFactory, recorder: ConsoleRecorder, sleep: Sleep
|
||||
) -> int:
|
||||
"""@brief The body of `main`, run while the recorder is attached.
|
||||
|
||||
@param argv Arguments without the program name.
|
||||
@param gym_factory Builds the gym site.
|
||||
@param recorder The console recorder.
|
||||
@param sleep Waits between two attempts of a retried step.
|
||||
@return Process exit code.
|
||||
"""
|
||||
_use_utf8_output()
|
||||
@@ -209,7 +248,7 @@ def _main(argv: Sequence[str] | None, gym_factory: GymFactory, recorder: Console
|
||||
return 2
|
||||
gym = gym_factory(args.headless, args.profile_dir, args.keep_open)
|
||||
try:
|
||||
verified = _run(gym, args, recorder, credentials)
|
||||
verified = _run(gym, args, recorder, credentials, sleep)
|
||||
except LoginFailedError as error:
|
||||
logger.error("Login failed: %s", error)
|
||||
return 1
|
||||
|
||||
@@ -18,6 +18,31 @@ WAIT_TIMEOUT_SECONDS: Final[int] = 10
|
||||
## Attempts of the `retry` wrapper before it gives up (lecture step 9).
|
||||
RETRY_COUNT: Final[int] = 7
|
||||
|
||||
## Seconds to wait between two attempts of the `retry` wrapper.
|
||||
RETRY_DELAY_SECONDS: Final[float] = 0.5
|
||||
|
||||
## Name of a retried step that was given no description.
|
||||
RETRY_DEFAULT_DESCRIPTION: Final[str] = "operation"
|
||||
|
||||
## Descriptions of the retried steps, as they appear in the log.
|
||||
RETRY_DESCRIPTION_LOGIN: Final[str] = "login"
|
||||
RETRY_DESCRIPTION_BOOK: Final[str] = "book {name} on {day}"
|
||||
RETRY_DESCRIPTION_BOOKINGS: Final[str] = "get my bookings"
|
||||
|
||||
## Messages of the `retry` wrapper.
|
||||
MESSAGE_RETRY_FAILED: Final[str] = (
|
||||
"⚠ Attempt {attempt}/{retries} failed: {description}: {error}. Retrying..."
|
||||
)
|
||||
MESSAGE_RETRY_SUCCEEDED: Final[str] = "✓ {description} succeeded after {attempts} attempts"
|
||||
MESSAGE_RETRIES_EXHAUSTED: Final[str] = "{description} failed after {retries} attempts: {error}"
|
||||
MESSAGE_RETRIES_INVALID: Final[str] = "retries must be at least 1, got {retries}"
|
||||
|
||||
## Error raised when a class card is no longer on the schedule while a booking is repeated.
|
||||
MESSAGE_CLASS_NOT_ON_SCHEDULE: Final[str] = "class {card_id} not found on the schedule"
|
||||
|
||||
## File name of the screenshot of lecture step 9 (the console with the retry warnings).
|
||||
SCREENSHOT_STEP_9: Final[str] = "step-9.png"
|
||||
|
||||
## Weekdays of the classes the bot books, as the site abbreviates them.
|
||||
TARGET_WEEKDAYS: Final[tuple[str, ...]] = ("Tue", "Thu")
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ class PageFormatError(GymError):
|
||||
"""@brief A text read from the page (a day label or a time) has an unexpected format."""
|
||||
|
||||
|
||||
class RetriesExhaustedError(GymError):
|
||||
"""@brief A step failed on every attempt; the cause is the error of the last attempt."""
|
||||
|
||||
|
||||
class NoBookingsFoundError(GymError):
|
||||
"""@brief The My Bookings page shows no booking where the bot expects some.
|
||||
|
||||
|
||||
@@ -88,11 +88,18 @@ class SeleniumGym:
|
||||
@exception LoginFailedError The site shows a login error.
|
||||
@exception GymError The page did not behave as expected.
|
||||
"""
|
||||
self._wait.until(ec.element_to_be_clickable((By.ID, constants.NAV_LOGIN_BUTTON_ID))).click()
|
||||
# After a failed login the form is still shown, so a repeated login skips the nav button.
|
||||
if not self._driver.find_elements(By.ID, constants.LOGIN_EMAIL_INPUT_ID):
|
||||
self._wait.until(
|
||||
ec.element_to_be_clickable((By.ID, constants.NAV_LOGIN_BUTTON_ID))
|
||||
).click()
|
||||
email_input = self._wait.until(
|
||||
ec.presence_of_element_located((By.ID, constants.LOGIN_EMAIL_INPUT_ID))
|
||||
).send_keys(email)
|
||||
self._driver.find_element(By.ID, constants.LOGIN_PASSWORD_INPUT_ID).send_keys(password)
|
||||
)
|
||||
password_input = self._driver.find_element(By.ID, constants.LOGIN_PASSWORD_INPUT_ID)
|
||||
for field, value in ((email_input, email), (password_input, password)):
|
||||
field.clear()
|
||||
field.send_keys(value)
|
||||
self._driver.find_element(By.ID, constants.LOGIN_SUBMIT_BUTTON_ID).click()
|
||||
self._wait.until(
|
||||
ec.any_of(
|
||||
|
||||
@@ -12,8 +12,19 @@
|
||||
// Dummy account of this fixture only; it is not a real account.
|
||||
const ACCEPTED_EMAIL = "fixture@example.invalid";
|
||||
const ACCEPTED_PASSWORD = "fixture-password";
|
||||
// Imitates the network simulation: ?fail=N drops the first N submits after a short delay.
|
||||
let failuresLeft = parseInt(new URLSearchParams(window.location.search).get("fail") || "0", 10);
|
||||
document.getElementById("login-form").addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
document.getElementById("message-area").innerHTML = "";
|
||||
if (failuresLeft > 0) {
|
||||
failuresLeft -= 1;
|
||||
setTimeout(function () {
|
||||
document.getElementById("message-area").innerHTML =
|
||||
'<div id="error-message">Network request failed. Please try again.</div>';
|
||||
}, 100);
|
||||
return;
|
||||
}
|
||||
const email = document.getElementById("email-input").value;
|
||||
const password = document.getElementById("password-input").value;
|
||||
if (email === ACCEPTED_EMAIL && password === ACCEPTED_PASSWORD) {
|
||||
|
||||
@@ -11,7 +11,13 @@
|
||||
<script>
|
||||
// Imitates the site: a loading text, then the page for the state in the query
|
||||
// (?state=empty or ?state=error; anything else shows two bookings and a waitlist entry).
|
||||
const state = new URLSearchParams(window.location.search).get("state");
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const state = params.get("state");
|
||||
// ?fail=N shows the error for the first N loads of the page in this tab (the network
|
||||
// simulation); the count lives in sessionStorage because every load starts a new page.
|
||||
const loads = parseInt(sessionStorage.getItem("loads") || "0", 10);
|
||||
const dropped = loads < parseInt(params.get("fail") || "0", 10);
|
||||
sessionStorage.setItem("loads", String(loads + 1));
|
||||
// The nav link keeps the state, so opening the page from here shows the same state again.
|
||||
document.getElementById("my-bookings-link").href = "my_bookings.html" + window.location.search;
|
||||
const page = document.getElementById("my-bookings-page");
|
||||
@@ -19,7 +25,7 @@
|
||||
setTimeout(function () {
|
||||
document.getElementById("loading-placeholder").remove();
|
||||
page.dataset.loading = "false";
|
||||
if (state === "error") {
|
||||
if (state === "error" || dropped) {
|
||||
content.innerHTML = '<div id="error-message">Failed to load bookings</div>';
|
||||
} else if (state === "empty") {
|
||||
content.innerHTML = '<div id="empty-bookings-state">You have no bookings yet</div>';
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
<p id="class-time-301"><strong>Time:</strong> 7:00 AM</p>
|
||||
<button id="book-button-301">Book Class</button>
|
||||
</div>
|
||||
<div id="class-card-302" data-class-type="spin" data-user-booked="false" data-fail-once="true">
|
||||
<div id="class-card-302" data-class-type="spin" data-user-booked="false" data-fail-times="1">
|
||||
<h3 id="class-name-302">Spin Class</h3>
|
||||
<p id="class-time-302"><strong>Time:</strong> 6:00 PM</p>
|
||||
<button id="book-button-302">Book Class</button>
|
||||
@@ -55,6 +55,11 @@
|
||||
</div>
|
||||
<script>
|
||||
// Imitates the site: a busy text, then the new state; card 302 fails on its first click.
|
||||
// ?fail=N makes every button fail on its first N clicks (the network simulation).
|
||||
const failParam = parseInt(new URLSearchParams(window.location.search).get("fail") || "0", 10);
|
||||
document.querySelectorAll("div[id^='class-card-']").forEach(function (card) {
|
||||
card.dataset.failLeft = String(Math.max(failParam, parseInt(card.dataset.failTimes || "0", 10)));
|
||||
});
|
||||
document.querySelectorAll("button[id^='book-button-']").forEach(function (button) {
|
||||
button.addEventListener("click", function () {
|
||||
const id = button.id.replace("book-button-", "");
|
||||
@@ -66,8 +71,8 @@
|
||||
button.textContent = waitlist ? "Joining..." : "Booking...";
|
||||
button.disabled = true;
|
||||
setTimeout(function () {
|
||||
if (card.dataset.failOnce === "true") {
|
||||
card.dataset.failOnce = "false";
|
||||
if (parseInt(card.dataset.failLeft, 10) > 0) {
|
||||
card.dataset.failLeft = String(parseInt(card.dataset.failLeft, 10) - 1);
|
||||
button.textContent = oldText;
|
||||
button.disabled = false;
|
||||
const box = document.createElement("div");
|
||||
|
||||
@@ -8,10 +8,17 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from snack_and_lift.application.booking import book_target_classes
|
||||
from snack_and_lift.application.verification import get_my_bookings
|
||||
from snack_and_lift.application.booking import book_class_with_retry, book_target_classes
|
||||
from snack_and_lift.application.credentials import Credentials
|
||||
from snack_and_lift.application.session import login_with_retry
|
||||
from snack_and_lift.application.verification import get_my_bookings, get_my_bookings_with_retry
|
||||
from snack_and_lift.domain.booking_entry import BookingEntry
|
||||
from snack_and_lift.domain.errors import GymError, LoginFailedError, NoBookingsFoundError
|
||||
from snack_and_lift.domain.errors import (
|
||||
GymError,
|
||||
LoginFailedError,
|
||||
NoBookingsFoundError,
|
||||
RetriesExhaustedError,
|
||||
)
|
||||
from snack_and_lift.domain.models import Outcome
|
||||
from snack_and_lift.infrastructure.browser import create_driver
|
||||
from snack_and_lift.infrastructure.selenium_gym import SeleniumGym
|
||||
@@ -119,13 +126,11 @@ def test_save_text_screenshot_writes_a_png_and_keeps_the_page(
|
||||
def test_book_target_classes_books_every_tuesday_and_thursday_class(
|
||||
schedule_gym: SeleniumGym,
|
||||
) -> None:
|
||||
with pytest.raises(GymError, match="Network error"):
|
||||
book_target_classes(schedule_gym)
|
||||
|
||||
results = book_target_classes(schedule_gym)
|
||||
# Card 302 drops its first click; the booking repeats it and the page confirms the second.
|
||||
results = book_target_classes(schedule_gym, sleep=lambda _: None)
|
||||
|
||||
assert [(r.card.card_id, r.outcome) for r in results] == [
|
||||
("201", Outcome.ALREADY_WAITLISTED),
|
||||
("201", Outcome.NEW_WAITLIST),
|
||||
("302", Outcome.NEW_BOOKING),
|
||||
]
|
||||
|
||||
@@ -188,3 +193,80 @@ def test_list_my_bookings_raises_with_the_text_of_the_error_box(
|
||||
) -> None:
|
||||
with pytest.raises(GymError, match="Failed to load bookings"):
|
||||
open_my_bookings_page("?state=error").list_my_bookings()
|
||||
|
||||
|
||||
FIXTURE_LOGIN = (Path(__file__).parent / "fixtures" / "login.html").as_uri()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def open_fixture(tmp_path: Path) -> Iterator[Callable[[str], SeleniumGym]]:
|
||||
"""@brief Opens a fixture page by its address, with the query that sets its failures."""
|
||||
driver = create_driver(headless=True, profile_dir=tmp_path / "profile", keep_open=False)
|
||||
|
||||
def open_page(url: str) -> SeleniumGym:
|
||||
driver.get(url)
|
||||
return SeleniumGym(driver, base_url=url)
|
||||
|
||||
try:
|
||||
yield open_page
|
||||
finally:
|
||||
driver.quit()
|
||||
|
||||
|
||||
def test_login_with_retry_logs_in_after_two_dropped_requests(
|
||||
open_fixture: Callable[[str], SeleniumGym],
|
||||
) -> None:
|
||||
gym = open_fixture(FIXTURE_LOGIN + "?fail=2")
|
||||
sleeps: list[float] = []
|
||||
|
||||
login_with_retry(gym, Credentials(FIXTURE_EMAIL, FIXTURE_PASSWORD), sleep=sleeps.append)
|
||||
|
||||
assert len(sleeps) == 2
|
||||
|
||||
|
||||
def test_login_with_retry_gives_up_when_every_request_is_dropped(
|
||||
open_fixture: Callable[[str], SeleniumGym],
|
||||
) -> None:
|
||||
gym = open_fixture(FIXTURE_LOGIN + "?fail=5")
|
||||
|
||||
with pytest.raises(RetriesExhaustedError, match="Network request failed"):
|
||||
login_with_retry(
|
||||
gym, Credentials(FIXTURE_EMAIL, FIXTURE_PASSWORD), retries=3, sleep=lambda _: None
|
||||
)
|
||||
|
||||
|
||||
def test_book_class_with_retry_books_after_two_dropped_clicks(
|
||||
open_fixture: Callable[[str], SeleniumGym],
|
||||
) -> None:
|
||||
gym = open_fixture(FIXTURE_SCHEDULE + "?fail=2")
|
||||
card = next(card for card in gym.list_class_cards() if card.card_id == "302")
|
||||
|
||||
result = book_class_with_retry(gym, card, sleep=lambda _: None)
|
||||
|
||||
assert result.outcome is Outcome.NEW_BOOKING
|
||||
assert next(c for c in gym.list_class_cards() if c.card_id == "302").button_text == "Booked"
|
||||
|
||||
|
||||
def test_book_target_classes_completes_when_every_class_drops_two_clicks(
|
||||
open_fixture: Callable[[str], SeleniumGym],
|
||||
) -> None:
|
||||
gym = open_fixture(FIXTURE_SCHEDULE + "?fail=2")
|
||||
|
||||
results = book_target_classes(gym, sleep=lambda _: None)
|
||||
|
||||
assert [(r.card.card_id, r.outcome) for r in results] == [
|
||||
("201", Outcome.NEW_WAITLIST),
|
||||
("302", Outcome.NEW_BOOKING),
|
||||
]
|
||||
|
||||
|
||||
def test_get_my_bookings_with_retry_reads_the_page_after_two_failed_loads(
|
||||
open_fixture: Callable[[str], SeleniumGym],
|
||||
) -> None:
|
||||
gym = open_fixture(FIXTURE_MY_BOOKINGS + "?fail=2")
|
||||
sleeps: list[float] = []
|
||||
|
||||
entries = get_my_bookings_with_retry(gym, sleep=sleeps.append)
|
||||
|
||||
assert [entry.name for entry in entries] == ["Spin Class", "Yoga Class", "HIIT Class"]
|
||||
assert sleeps
|
||||
|
||||
+70
-3
@@ -2,6 +2,8 @@
|
||||
@brief A fake of the GymPort for the unit tests.
|
||||
"""
|
||||
|
||||
import random
|
||||
from dataclasses import replace
|
||||
from pathlib import Path
|
||||
|
||||
from snack_and_lift import constants
|
||||
@@ -32,11 +34,20 @@ def _entry_of(card: ClassCard) -> BookingEntry | None:
|
||||
return BookingEntry(card.name, f"{card.date_text}, {time_text}", flag)
|
||||
|
||||
|
||||
NETWORK_ERROR_TEXT = "Network request failed. Please try again."
|
||||
|
||||
|
||||
class FakeGym:
|
||||
"""@brief Records the calls; can refuse the login, serve cards and fail a click.
|
||||
|
||||
Unless `bookings` is given, the My Bookings page shows every card that is, or by the
|
||||
click becomes, booked or waitlisted.
|
||||
click becomes, booked or waitlisted. A successful click turns the card's button into its
|
||||
new state, so a card read again shows what the click did.
|
||||
|
||||
`fail_first` is a deterministic failure schedule: it maps the name of a port method
|
||||
(`log_in`, `press_class_button`, `open_my_bookings`, `list_my_bookings`) to the number of
|
||||
its first calls that fail like a dropped request. With `click_goes_through`, a failing
|
||||
`press_class_button` still turns the button (the click arrived, the answer did not).
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
@@ -47,6 +58,8 @@ class FakeGym:
|
||||
press_error: str | None = None,
|
||||
bookings: list[BookingEntry] | None = None,
|
||||
bookings_error: str | None = None,
|
||||
fail_first: dict[str, int] | None = None,
|
||||
click_goes_through: bool = False,
|
||||
) -> None:
|
||||
self.login_error = login_error
|
||||
self.cards = cards or []
|
||||
@@ -54,6 +67,8 @@ class FakeGym:
|
||||
self.press_error = press_error
|
||||
self.bookings = bookings
|
||||
self.bookings_error = bookings_error
|
||||
self.click_goes_through = click_goes_through
|
||||
self._failures_left = dict(fail_first or {})
|
||||
self.calls: list[str] = []
|
||||
self.logins: list[tuple[str, str]] = []
|
||||
self.screenshots: list[Path] = []
|
||||
@@ -64,11 +79,29 @@ class FakeGym:
|
||||
def open_site(self) -> None:
|
||||
self.calls.append("open_site")
|
||||
|
||||
def _should_fail(self, name: str) -> bool:
|
||||
"""@brief Whether this call is one of the scheduled failures; counts it."""
|
||||
left = self._failures_left.get(name, 0)
|
||||
if left > 0:
|
||||
self._failures_left[name] = left - 1
|
||||
return True
|
||||
return False
|
||||
|
||||
def _turn_button(self, card_id: str) -> str:
|
||||
"""@brief Turn the button of a card to the state its click gives; return the new text."""
|
||||
index = next(i for i, card in enumerate(self.cards) if card.card_id == card_id)
|
||||
card = self.cards[index]
|
||||
new_text = _PRESS_RESULTS[card.button_text]
|
||||
self.cards[index] = replace(card, button_text=new_text)
|
||||
return new_text
|
||||
|
||||
def log_in(self, email: str, password: str) -> None:
|
||||
self.calls.append("log_in")
|
||||
self.logins.append((email, password))
|
||||
if self.login_error is not None:
|
||||
raise LoginFailedError(self.login_error)
|
||||
if self._should_fail("log_in"):
|
||||
raise LoginFailedError(NETWORK_ERROR_TEXT)
|
||||
|
||||
def list_class_cards(self) -> list[ClassCard]:
|
||||
self.calls.append("list_class_cards")
|
||||
@@ -81,14 +114,21 @@ class FakeGym:
|
||||
raise GymError(self.press_error)
|
||||
if self.press_result is not None:
|
||||
return self.press_result
|
||||
card = next(card for card in self.cards if card.card_id == card_id)
|
||||
return _PRESS_RESULTS[card.button_text]
|
||||
if self._should_fail("press_class_button"):
|
||||
if self.click_goes_through:
|
||||
self._turn_button(card_id)
|
||||
raise GymError(NETWORK_ERROR_TEXT)
|
||||
return self._turn_button(card_id)
|
||||
|
||||
def open_my_bookings(self) -> None:
|
||||
self.calls.append("open_my_bookings")
|
||||
if self._should_fail("open_my_bookings"):
|
||||
raise GymError(NETWORK_ERROR_TEXT)
|
||||
|
||||
def list_my_bookings(self) -> list[BookingEntry]:
|
||||
self.calls.append("list_my_bookings")
|
||||
if self._should_fail("list_my_bookings"):
|
||||
raise GymError(NETWORK_ERROR_TEXT)
|
||||
if self.bookings_error is not None:
|
||||
raise GymError(self.bookings_error)
|
||||
if self.bookings is not None:
|
||||
@@ -107,3 +147,30 @@ class FakeGym:
|
||||
def close(self) -> None:
|
||||
self.calls.append("close")
|
||||
self.closed = True
|
||||
|
||||
|
||||
class ChaosGym(FakeGym):
|
||||
"""@brief A gym whose login, click and bookings read each fail with a set probability.
|
||||
|
||||
The site's network simulation fails up to 50% of these requests and writes nothing when
|
||||
one fails. The random numbers come from a seeded generator, so a run is repeatable.
|
||||
"""
|
||||
|
||||
_FAILING_CALLS = frozenset({"log_in", "press_class_button", "list_my_bookings"})
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
rng: random.Random,
|
||||
failure_probability: float = 0.5,
|
||||
cards: list[ClassCard] | None = None,
|
||||
) -> None:
|
||||
super().__init__(cards=cards)
|
||||
self._rng = rng
|
||||
self._failure_probability = failure_probability
|
||||
|
||||
def _should_fail(self, name: str) -> bool:
|
||||
return name in self._FAILING_CALLS and self._rng.random() < self._failure_probability
|
||||
|
||||
|
||||
def no_sleep(seconds: float) -> None:
|
||||
"""@brief A `sleep` that returns at once, so a retry test never waits."""
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from tests.unit.fakes import FakeGym
|
||||
from tests.unit.fakes import FakeGym, no_sleep
|
||||
from tests.unit.test_booking_entry import make_entry
|
||||
from tests.unit.test_models import make_card
|
||||
|
||||
from snack_and_lift.application.booking import book_target_classes
|
||||
from snack_and_lift.application.verification import get_my_bookings, verify_bookings
|
||||
from snack_and_lift.domain.errors import GymError, NoBookingsFoundError
|
||||
from snack_and_lift.domain.errors import GymError, NoBookingsFoundError, RetriesExhaustedError
|
||||
from snack_and_lift.domain.models import ClassResult, Outcome
|
||||
|
||||
|
||||
@@ -67,8 +67,10 @@ def test_verify_bookings_raises_when_classes_were_booked_but_the_page_is_empty()
|
||||
gym = FakeGym(bookings=[])
|
||||
results = [ClassResult(make_card("Tue, Aug 12"), Outcome.NEW_BOOKING)]
|
||||
|
||||
with pytest.raises(NoBookingsFoundError):
|
||||
verify_bookings(gym, results)
|
||||
with pytest.raises(RetriesExhaustedError) as caught:
|
||||
verify_bookings(gym, results, sleep=no_sleep)
|
||||
|
||||
assert isinstance(caught.value.__cause__, NoBookingsFoundError)
|
||||
|
||||
|
||||
def test_verify_bookings_skips_the_page_when_nothing_is_expected() -> None:
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
from tests.unit.fakes import FakeGym
|
||||
from tests.unit.fakes import FakeGym, no_sleep
|
||||
from tests.unit.test_models import make_card
|
||||
|
||||
from snack_and_lift.application.booking import (
|
||||
@@ -147,4 +147,4 @@ def test_book_target_classes_propagates_a_failing_class() -> None:
|
||||
gym = FakeGym(cards=[make_card("Thu, Aug 14")], press_error="Network down")
|
||||
|
||||
with pytest.raises(GymError, match="Network down"):
|
||||
book_target_classes(gym)
|
||||
book_target_classes(gym, sleep=no_sleep)
|
||||
|
||||
+15
-6
@@ -6,7 +6,7 @@ import logging
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from tests.unit.fakes import FakeGym
|
||||
from tests.unit.fakes import FakeGym, no_sleep
|
||||
from tests.unit.test_booking_entry import make_entry
|
||||
from tests.unit.test_models import make_card
|
||||
|
||||
@@ -75,6 +75,7 @@ def test_main_saves_both_screenshots_on_success(tmp_path: Path) -> None:
|
||||
tmp_path / "step-5.png",
|
||||
tmp_path / "step-6.png",
|
||||
tmp_path / "step-8.png",
|
||||
tmp_path / "step-9.png",
|
||||
]
|
||||
assert gym.calls == [
|
||||
"open_site",
|
||||
@@ -88,6 +89,7 @@ def test_main_saves_both_screenshots_on_success(tmp_path: Path) -> None:
|
||||
"save_text_screenshot",
|
||||
"save_screenshot",
|
||||
"save_text_screenshot",
|
||||
"save_text_screenshot",
|
||||
]
|
||||
|
||||
|
||||
@@ -149,12 +151,18 @@ def test_main_saves_step_3_after_the_first_class_and_the_report_images(tmp_path:
|
||||
"list_my_bookings",
|
||||
"save_screenshot",
|
||||
"save_text_screenshot",
|
||||
"save_text_screenshot",
|
||||
]
|
||||
assert gym.screenshots[-2:] == [tmp_path / "step-3.png", tmp_path / "step-7.png"]
|
||||
(_, _), (step_5, _), (step_6, _), (step_8, step_8_path) = gym.text_screenshots
|
||||
(_, _), (step_5, _), (step_6, _), (step_8, step_8_path), (step_9, step_9_path) = (
|
||||
gym.text_screenshots
|
||||
)
|
||||
assert step_8_path == tmp_path / "step-8.png"
|
||||
assert step_8.startswith("--- VERIFYING ON MY BOOKINGS PAGE ---")
|
||||
assert step_8.endswith("✅ SUCCESS: All bookings verified!")
|
||||
assert step_9_path == tmp_path / "step-9.png"
|
||||
assert step_9.startswith("✓ Logged in")
|
||||
assert step_9.endswith("✅ SUCCESS: All bookings verified!")
|
||||
assert step_5.startswith("--- BOOKING SUMMARY ---\nNew bookings: 2")
|
||||
assert "--- DETAILED CLASS LIST ---" not in step_5
|
||||
assert step_6.startswith(step_5)
|
||||
@@ -185,7 +193,8 @@ def test_main_returns_3_on_a_mismatch(tmp_path: Path, caplog: pytest.LogCaptureF
|
||||
assert "❌ MISMATCH: Missing 1 bookings" in caplog.text
|
||||
assert " ✗ Missing: Spin Class on Tue, Aug 19" in caplog.text
|
||||
assert gym.screenshots[-1] == tmp_path / "step-7.png"
|
||||
assert gym.text_screenshots[-1][1] == tmp_path / "step-8.png"
|
||||
assert gym.text_screenshots[-2][1] == tmp_path / "step-8.png"
|
||||
assert gym.text_screenshots[-1][1] == tmp_path / "step-9.png"
|
||||
assert gym.closed is True
|
||||
|
||||
|
||||
@@ -193,7 +202,7 @@ def test_main_returns_3_on_a_mismatch(tmp_path: Path, caplog: pytest.LogCaptureF
|
||||
def test_main_returns_1_when_the_bookings_page_is_empty() -> None:
|
||||
gym = FakeGym(cards=[make_card("Tue, Aug 19")], bookings=[])
|
||||
|
||||
assert main([], lambda *_: gym) == 1
|
||||
assert main([], lambda *_: gym, no_sleep) == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("credentials_env")
|
||||
@@ -239,7 +248,7 @@ def test_first_tuesday_only_flag_is_read() -> None:
|
||||
def test_main_returns_1_when_the_booking_fails() -> None:
|
||||
gym = FakeGym(cards=[make_card("Tue, Aug 19")], press_error="Network down")
|
||||
|
||||
assert main(["--headless"], lambda *_: gym) == 1
|
||||
assert main(["--headless"], lambda *_: gym, no_sleep) == 1
|
||||
assert gym.closed is True
|
||||
|
||||
|
||||
@@ -269,7 +278,7 @@ def test_main_returns_2_when_credentials_are_missing(
|
||||
def test_main_returns_1_when_login_fails() -> None:
|
||||
gym = FakeGym(login_error="Invalid credentials")
|
||||
|
||||
assert main(["--headless"], lambda *_: gym) == 1
|
||||
assert main(["--headless"], lambda *_: gym, no_sleep) == 1
|
||||
assert gym.closed is True
|
||||
|
||||
|
||||
|
||||
@@ -29,3 +29,7 @@ def test_retry_count_is_seven() -> None:
|
||||
|
||||
def test_bot_books_tuesday_and_thursday() -> None:
|
||||
assert constants.TARGET_WEEKDAYS == ("Tue", "Thu")
|
||||
|
||||
|
||||
def test_retry_delay_is_half_a_second() -> None:
|
||||
assert constants.RETRY_DELAY_SECONDS == 0.5
|
||||
|
||||
@@ -0,0 +1,316 @@
|
||||
"""@file test_resilience.py
|
||||
@brief Lecture step 9: login, booking and bookings read survive a site that drops requests.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import random
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from tests.unit.fakes import ChaosGym, FakeGym, no_sleep
|
||||
from tests.unit.test_booking_entry import make_entry
|
||||
from tests.unit.test_models import make_card
|
||||
|
||||
from snack_and_lift import constants
|
||||
from snack_and_lift.application.booking import (
|
||||
book_class_with_retry,
|
||||
book_next_tuesday,
|
||||
book_target_classes,
|
||||
)
|
||||
from snack_and_lift.application.credentials import Credentials
|
||||
from snack_and_lift.application.session import login_with_retry
|
||||
from snack_and_lift.application.verification import get_my_bookings_with_retry, verify_bookings
|
||||
from snack_and_lift.cli import main, parse_args
|
||||
from snack_and_lift.domain.errors import GymError, LoginFailedError, RetriesExhaustedError
|
||||
from snack_and_lift.domain.models import ButtonState, ClassResult, Outcome
|
||||
|
||||
DUMMY_EMAIL = "dummy@example.invalid"
|
||||
DUMMY_PASSWORD = "dummy-password"
|
||||
CREDENTIALS = Credentials(DUMMY_EMAIL, DUMMY_PASSWORD)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def credentials_env(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setenv(constants.ACCOUNT_EMAIL_ENV, DUMMY_EMAIL)
|
||||
monkeypatch.setenv(constants.ACCOUNT_PASSWORD_ENV, DUMMY_PASSWORD)
|
||||
|
||||
|
||||
def test_login_with_retry_logs_in_after_two_dropped_requests(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
gym = FakeGym(fail_first={"log_in": 2})
|
||||
sleeps: list[float] = []
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
login_with_retry(gym, CREDENTIALS, sleep=sleeps.append)
|
||||
|
||||
assert gym.calls == ["log_in"] * 3
|
||||
assert sleeps == [constants.RETRY_DELAY_SECONDS] * 2
|
||||
assert "⚠ Attempt 2/7 failed: login: Network request failed. Please try again." in caplog.text
|
||||
assert "✓ login succeeded after 3 attempts" in caplog.text
|
||||
|
||||
|
||||
def test_login_with_retry_never_logs_the_password(caplog: pytest.LogCaptureFixture) -> None:
|
||||
gym = FakeGym(fail_first={"log_in": 2})
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
login_with_retry(gym, CREDENTIALS, sleep=no_sleep)
|
||||
|
||||
assert DUMMY_PASSWORD not in caplog.text
|
||||
assert DUMMY_EMAIL not in caplog.text
|
||||
|
||||
|
||||
def test_login_with_retry_gives_up_after_the_set_attempts() -> None:
|
||||
gym = FakeGym(fail_first={"log_in": 10})
|
||||
|
||||
with pytest.raises(RetriesExhaustedError, match="login failed after 3 attempts") as caught:
|
||||
login_with_retry(gym, CREDENTIALS, retries=3, sleep=no_sleep)
|
||||
|
||||
assert isinstance(caught.value.__cause__, LoginFailedError)
|
||||
assert len(gym.logins) == 3
|
||||
|
||||
|
||||
def test_book_class_with_retry_repeats_a_dropped_click() -> None:
|
||||
card = make_card()
|
||||
gym = FakeGym(cards=[card], fail_first={"press_class_button": 2})
|
||||
|
||||
result = book_class_with_retry(gym, card, sleep=no_sleep)
|
||||
|
||||
assert result.outcome is Outcome.NEW_BOOKING
|
||||
assert gym.pressed == ["123", "123", "123"]
|
||||
assert gym.calls.count("list_class_cards") == 2
|
||||
|
||||
|
||||
def test_book_class_with_retry_reads_nothing_again_when_the_first_attempt_works() -> None:
|
||||
card = make_card()
|
||||
gym = FakeGym(cards=[card])
|
||||
|
||||
book_class_with_retry(gym, card, sleep=no_sleep)
|
||||
|
||||
assert gym.calls == ["press_class_button"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("button_text", "outcome", "final_state"),
|
||||
[
|
||||
("Book Class", Outcome.NEW_BOOKING, ButtonState.BOOKED),
|
||||
("Join Waitlist", Outcome.NEW_WAITLIST, ButtonState.WAITLISTED),
|
||||
],
|
||||
)
|
||||
def test_book_class_with_retry_counts_a_click_that_went_through_as_new(
|
||||
button_text: str,
|
||||
outcome: Outcome,
|
||||
final_state: ButtonState,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
card = make_card(button_text=button_text)
|
||||
gym = FakeGym(cards=[card], fail_first={"press_class_button": 1}, click_goes_through=True)
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
result = book_class_with_retry(gym, card, sleep=no_sleep)
|
||||
|
||||
assert result.outcome is outcome
|
||||
assert gym.pressed == ["123"]
|
||||
assert gym.cards[0].state is final_state
|
||||
assert "Already" not in caplog.text
|
||||
|
||||
|
||||
def test_book_class_with_retry_keeps_already_booked_as_it_is() -> None:
|
||||
card = make_card(button_text="Booked")
|
||||
gym = FakeGym(cards=[card])
|
||||
|
||||
result = book_class_with_retry(gym, card, sleep=no_sleep)
|
||||
|
||||
assert result.outcome is Outcome.ALREADY_BOOKED
|
||||
assert gym.pressed == []
|
||||
|
||||
|
||||
def test_book_class_with_retry_does_not_count_a_click_without_confirmation() -> None:
|
||||
card = make_card()
|
||||
gym = FakeGym(cards=[card], press_result="Book Class")
|
||||
|
||||
with pytest.raises(RetriesExhaustedError, match="click not confirmed"):
|
||||
book_class_with_retry(gym, card, retries=2, sleep=no_sleep)
|
||||
|
||||
assert gym.pressed == ["123", "123"]
|
||||
|
||||
|
||||
def test_book_class_with_retry_fails_when_the_class_left_the_schedule() -> None:
|
||||
card = make_card()
|
||||
gym = FakeGym(cards=[card], fail_first={"press_class_button": 1})
|
||||
gym.cards.clear()
|
||||
|
||||
with pytest.raises(RetriesExhaustedError, match="not found on the schedule"):
|
||||
book_class_with_retry(gym, card, retries=2, sleep=no_sleep)
|
||||
|
||||
|
||||
def test_book_class_with_retry_gives_up_after_the_set_attempts() -> None:
|
||||
card = make_card()
|
||||
gym = FakeGym(cards=[card], fail_first={"press_class_button": 9})
|
||||
|
||||
with pytest.raises(RetriesExhaustedError, match="book Spin Class on Tomorrow"):
|
||||
book_class_with_retry(gym, card, retries=3, sleep=no_sleep)
|
||||
|
||||
assert len(gym.pressed) == 3
|
||||
|
||||
|
||||
def test_book_next_tuesday_repeats_a_dropped_click() -> None:
|
||||
gym = FakeGym(cards=[make_card("Tue, Aug 19")], fail_first={"press_class_button": 1})
|
||||
|
||||
result = book_next_tuesday(gym, sleep=no_sleep)
|
||||
|
||||
assert result is not None
|
||||
assert result.outcome is Outcome.NEW_BOOKING
|
||||
|
||||
|
||||
def test_book_target_classes_repeats_each_class_on_its_own() -> None:
|
||||
gym = FakeGym(
|
||||
cards=[make_card("Thu, Aug 14", card_id="1"), make_card("Tue, Aug 19", card_id="2")],
|
||||
fail_first={"press_class_button": 3},
|
||||
)
|
||||
|
||||
results = book_target_classes(gym, sleep=no_sleep)
|
||||
|
||||
assert [result.outcome for result in results] == [Outcome.NEW_BOOKING] * 2
|
||||
assert gym.pressed == ["1", "1", "1", "1", "2"]
|
||||
|
||||
|
||||
def test_get_my_bookings_with_retry_opens_the_page_again_after_a_failure() -> None:
|
||||
entries = [make_entry("Wed, Aug 13, 7:00 AM")]
|
||||
gym = FakeGym(bookings=entries, fail_first={"open_my_bookings": 1, "list_my_bookings": 1})
|
||||
|
||||
assert get_my_bookings_with_retry(gym, sleep=no_sleep) == entries
|
||||
assert gym.calls == [
|
||||
"open_my_bookings",
|
||||
"open_my_bookings",
|
||||
"list_my_bookings",
|
||||
"open_my_bookings",
|
||||
"list_my_bookings",
|
||||
]
|
||||
|
||||
|
||||
def test_get_my_bookings_with_retry_gives_up_after_the_set_attempts() -> None:
|
||||
gym = FakeGym(bookings_error="Failed to load bookings")
|
||||
|
||||
with pytest.raises(RetriesExhaustedError, match="Failed to load bookings"):
|
||||
get_my_bookings_with_retry(gym, retries=2, sleep=no_sleep)
|
||||
|
||||
|
||||
def test_verify_bookings_reads_the_page_through_retry() -> None:
|
||||
card = make_card("Tue, Aug 12")
|
||||
gym = FakeGym(cards=[card], fail_first={"list_my_bookings": 2})
|
||||
results = [ClassResult(card, Outcome.NEW_BOOKING)]
|
||||
|
||||
verification = verify_bookings(gym, results, sleep=no_sleep)
|
||||
|
||||
assert verification.is_success
|
||||
|
||||
|
||||
def test_main_completes_a_run_with_dropped_requests_and_draws_them_in_step_9(
|
||||
credentials_env: None, tmp_path: Path, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
gym = FakeGym(
|
||||
cards=[make_card("Thu, Aug 14", card_id="1"), make_card("Tue, Aug 19", card_id="2")],
|
||||
fail_first={"log_in": 1, "press_class_button": 2, "list_my_bookings": 1},
|
||||
)
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
code = main(["--screenshot-dir", str(tmp_path)], lambda *_: gym, no_sleep)
|
||||
|
||||
assert code == 0
|
||||
step_9, step_9_path = gym.text_screenshots[-1]
|
||||
assert step_9_path == tmp_path / "step-9.png"
|
||||
assert "⚠ Attempt 1/7 failed: login" in step_9
|
||||
assert "succeeded after 2 attempts" in step_9
|
||||
assert step_9.endswith("✅ SUCCESS: All bookings verified!")
|
||||
|
||||
|
||||
def test_main_returns_1_when_every_retry_failed(
|
||||
credentials_env: None, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
gym = FakeGym(fail_first={"log_in": 5})
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
code = main(["--retries", "2"], lambda *_: gym, no_sleep)
|
||||
|
||||
assert code == 1
|
||||
assert len(gym.logins) == 2
|
||||
assert "Failed: login failed after 2 attempts" in caplog.text
|
||||
|
||||
|
||||
def test_main_uses_one_attempt_when_asked() -> None:
|
||||
assert parse_args(["--retries", "1"]).retries == 1
|
||||
|
||||
|
||||
def test_retries_option_defaults_to_the_constant() -> None:
|
||||
assert parse_args([]).retries == constants.RETRY_COUNT
|
||||
|
||||
|
||||
@pytest.mark.parametrize("value", ["0", "-3", "many", "1.5"])
|
||||
def test_retries_option_rejects_less_than_one(value: str) -> None:
|
||||
with pytest.raises(SystemExit):
|
||||
parse_args(["--retries", value])
|
||||
|
||||
|
||||
def test_help_epilog_mentions_retries(capsys: pytest.CaptureFixture[str]) -> None:
|
||||
with pytest.raises(SystemExit):
|
||||
parse_args(["--help"])
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert "--retries N" in out
|
||||
assert "every retry failed" in out
|
||||
|
||||
|
||||
## Chance that a request fails in the chaos test: the site's maximum (50%).
|
||||
CHAOS_FAILURE_PROBABILITY = 0.5
|
||||
|
||||
## Chaos runs, and how many must complete. With 7 attempts an operation fails for good with
|
||||
## probability 0.5**7 (about 0.8%). A run has four such operations (login, two clicks, the
|
||||
## bookings read), so about 3% of runs fail and about 97 of 100 complete; 90 leaves room for
|
||||
## the spread of the seeds without hiding a real defect. The seeds are fixed, so it is stable.
|
||||
CHAOS_RUNS = 100
|
||||
CHAOS_MIN_COMPLETED = 90
|
||||
|
||||
## Seeds that were checked to complete fully (seed 18 is the first that runs out of attempts);
|
||||
## a change that breaks one of them is a defect.
|
||||
CHAOS_SEEDS_THAT_MUST_PASS = tuple(range(18))
|
||||
|
||||
|
||||
def _run_chaos(seed: int) -> tuple[int, ChaosGym]:
|
||||
gym = ChaosGym(
|
||||
random.Random(seed),
|
||||
CHAOS_FAILURE_PROBABILITY,
|
||||
cards=[make_card("Thu, Aug 14", card_id="1"), make_card("Tue, Aug 19", card_id="2")],
|
||||
)
|
||||
return main(["--retries", "7"], lambda *_: gym, no_sleep), gym
|
||||
|
||||
|
||||
def _is_consistent(gym: ChaosGym) -> bool:
|
||||
"""@brief True when every target class is booked on the page after the run."""
|
||||
return all(card.state is ButtonState.BOOKED for card in gym.cards)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("credentials_env")
|
||||
def test_chaos_runs_with_half_of_the_requests_failing_mostly_complete() -> None:
|
||||
completed = 0
|
||||
for seed in range(CHAOS_RUNS):
|
||||
code, gym = _run_chaos(seed)
|
||||
assert code in (0, 1), f"seed {seed}: a verified run never ends in a mismatch"
|
||||
if code == 0:
|
||||
assert _is_consistent(gym), f"seed {seed}: verified but not booked"
|
||||
completed += 1
|
||||
|
||||
assert completed >= CHAOS_MIN_COMPLETED
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("credentials_env")
|
||||
@pytest.mark.parametrize("seed", CHAOS_SEEDS_THAT_MUST_PASS)
|
||||
def test_chaos_seed_completes_with_a_consistent_verified_result(seed: int) -> None:
|
||||
code, gym = _run_chaos(seed)
|
||||
|
||||
assert code == 0
|
||||
assert _is_consistent(gym)
|
||||
|
||||
|
||||
def test_retry_error_is_a_gym_error_for_the_exit_code() -> None:
|
||||
assert issubclass(RetriesExhaustedError, GymError)
|
||||
@@ -0,0 +1,167 @@
|
||||
"""@file test_retry.py
|
||||
@brief The retry wrapper: success, repeats, exhaustion, what it logs and what it never retries.
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from snack_and_lift import constants
|
||||
from snack_and_lift.application.retry import retry
|
||||
from snack_and_lift.domain.errors import (
|
||||
GymError,
|
||||
LoginFailedError,
|
||||
MissingCredentialsError,
|
||||
PageFormatError,
|
||||
RetriesExhaustedError,
|
||||
UnknownButtonError,
|
||||
)
|
||||
|
||||
|
||||
class Flaky:
|
||||
"""@brief A step that raises the given errors in turn and then returns a value."""
|
||||
|
||||
def __init__(self, errors: list[Exception], value: str = "done") -> None:
|
||||
self._errors = list(errors)
|
||||
self._value = value
|
||||
self.calls = 0
|
||||
|
||||
def __call__(self) -> str:
|
||||
self.calls += 1
|
||||
if self._errors:
|
||||
raise self._errors.pop(0)
|
||||
return self._value
|
||||
|
||||
|
||||
def test_retry_returns_the_result_on_the_first_attempt() -> None:
|
||||
step = Flaky([])
|
||||
sleeps: list[float] = []
|
||||
|
||||
assert retry(step, sleep=sleeps.append) == "done"
|
||||
assert step.calls == 1
|
||||
assert sleeps == []
|
||||
|
||||
|
||||
def test_retry_succeeds_after_some_failures(caplog: pytest.LogCaptureFixture) -> None:
|
||||
step = Flaky([GymError("a"), GymError("b")])
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
result = retry(step, 7, "login", sleep=lambda _: None)
|
||||
|
||||
assert result == "done"
|
||||
assert step.calls == 3
|
||||
assert "✓ login succeeded after 3 attempts" in caplog.text
|
||||
|
||||
|
||||
def test_retry_does_not_log_success_after_a_single_attempt(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
with caplog.at_level(logging.INFO):
|
||||
retry(Flaky([]), 7, "login", sleep=lambda _: None)
|
||||
|
||||
assert "succeeded" not in caplog.text
|
||||
|
||||
|
||||
def test_retry_raises_after_the_last_attempt_with_the_last_error_as_cause() -> None:
|
||||
last = GymError("third")
|
||||
step = Flaky([GymError("first"), GymError("second"), last])
|
||||
|
||||
with pytest.raises(RetriesExhaustedError, match="login failed after 3 attempts") as caught:
|
||||
retry(step, 3, "login", sleep=lambda _: None)
|
||||
|
||||
assert caught.value.__cause__ is last
|
||||
assert "third" in str(caught.value)
|
||||
assert step.calls == 3
|
||||
|
||||
|
||||
def test_retries_exhausted_is_a_gym_error() -> None:
|
||||
with pytest.raises(GymError):
|
||||
retry(Flaky([GymError("x")]), 1, sleep=lambda _: None)
|
||||
|
||||
|
||||
def test_retry_with_one_attempt_does_not_repeat_or_sleep() -> None:
|
||||
step = Flaky([GymError("x"), GymError("y")])
|
||||
sleeps: list[float] = []
|
||||
|
||||
with pytest.raises(RetriesExhaustedError):
|
||||
retry(step, 1, sleep=sleeps.append)
|
||||
|
||||
assert step.calls == 1
|
||||
assert sleeps == []
|
||||
|
||||
|
||||
def test_retry_with_one_attempt_returns_on_success() -> None:
|
||||
assert retry(Flaky([]), 1, sleep=lambda _: None) == "done"
|
||||
|
||||
|
||||
def test_retry_waits_the_delay_between_attempts_but_not_after_the_last() -> None:
|
||||
sleeps: list[float] = []
|
||||
|
||||
with pytest.raises(RetriesExhaustedError):
|
||||
retry(Flaky([GymError("x")] * 4), 4, delay=1.5, sleep=sleeps.append)
|
||||
|
||||
assert sleeps == [1.5, 1.5, 1.5]
|
||||
|
||||
|
||||
def test_retry_uses_the_default_delay_and_attempts() -> None:
|
||||
sleeps: list[float] = []
|
||||
step = Flaky([GymError("x")] * constants.RETRY_COUNT)
|
||||
|
||||
with pytest.raises(RetriesExhaustedError):
|
||||
retry(step, sleep=sleeps.append)
|
||||
|
||||
assert step.calls == constants.RETRY_COUNT
|
||||
assert sleeps == [constants.RETRY_DELAY_SECONDS] * (constants.RETRY_COUNT - 1)
|
||||
|
||||
|
||||
def test_retry_logs_a_warning_with_attempt_description_and_error(
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
step = Flaky([GymError("Network request failed. Please try again.")])
|
||||
|
||||
with caplog.at_level(logging.WARNING):
|
||||
retry(step, 7, "login", sleep=lambda _: None)
|
||||
|
||||
assert (
|
||||
"⚠ Attempt 1/7 failed: login: Network request failed. Please try again. Retrying..."
|
||||
in caplog.text
|
||||
)
|
||||
|
||||
|
||||
def test_retry_names_a_step_without_description(caplog: pytest.LogCaptureFixture) -> None:
|
||||
with caplog.at_level(logging.WARNING):
|
||||
retry(Flaky([GymError("x")]), sleep=lambda _: None)
|
||||
|
||||
assert f"failed: {constants.RETRY_DEFAULT_DESCRIPTION}: x" in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error",
|
||||
[LoginFailedError("no"), UnknownButtonError("busy"), PageFormatError("format")],
|
||||
)
|
||||
def test_retry_repeats_every_kind_of_gym_error(error: GymError) -> None:
|
||||
step = Flaky([error])
|
||||
|
||||
assert retry(step, 2, sleep=lambda _: None) == "done"
|
||||
assert step.calls == 2
|
||||
|
||||
|
||||
def test_retry_lets_other_errors_pass_untouched() -> None:
|
||||
error = MissingCredentialsError("missing")
|
||||
step = Flaky([error])
|
||||
|
||||
with pytest.raises(MissingCredentialsError) as caught:
|
||||
retry(step, 7, sleep=lambda _: None)
|
||||
|
||||
assert caught.value is error
|
||||
assert step.calls == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("retries", [0, -1])
|
||||
def test_retry_rejects_fewer_than_one_attempt(retries: int) -> None:
|
||||
step = Flaky([])
|
||||
|
||||
with pytest.raises(ValueError, match="at least 1"):
|
||||
retry(step, retries)
|
||||
|
||||
assert step.calls == 0
|
||||
Reference in New Issue
Block a user