MCPcopy
hub / github.com/python-trio/trio / run_scenario

Function run_scenario

src/trio/_tests/test_highlevel_open_tcp_stream.py:344–388  ·  view source on GitHub ↗
(
    # The port to connect to
    port: int,
    # A list of
    #  (ip, delay, result)
    # tuples, where delay is in seconds and result is "success" or "error"
    # The ip's will be returned from getaddrinfo in this order, and then
    # connect() calls to them will have the given result.
    ip_list: Sequence[tuple[str, float, str]],
    *,
    # If False, AF_INET4/6 sockets error out on creation, before connect is
    # even called.
    ipv4_supported: bool = True,
    ipv6_supported: bool = True,
    # Normally, we return (winning_sock, scenario object)
    # If this is True, we require there to be an exception, and return
    #   (exception, scenario object)
    expect_error: tuple[type[BaseException], ...] | type[BaseException] = (),
    happy_eyeballs_delay: float | None = 0.25,
    local_address: str | None = None,
)

Source from the content-addressed store, hash-verified

342
343
344async def run_scenario(
345 # The port to connect to
346 port: int,
347 # A list of
348 # (ip, delay, result)
349 # tuples, where delay is in seconds and result is "success" or "error"
350 # The ip's will be returned from getaddrinfo in this order, and then
351 # connect() calls to them will have the given result.
352 ip_list: Sequence[tuple[str, float, str]],
353 *,
354 # If False, AF_INET4/6 sockets error out on creation, before connect is
355 # even called.
356 ipv4_supported: bool = True,
357 ipv6_supported: bool = True,
358 # Normally, we return (winning_sock, scenario object)
359 # If this is True, we require there to be an exception, and return
360 # (exception, scenario object)
361 expect_error: tuple[type[BaseException], ...] | type[BaseException] = (),
362 happy_eyeballs_delay: float | None = 0.25,
363 local_address: str | None = None,
364) -> tuple[SocketType, Scenario] | tuple[BaseException, Scenario]:
365 supported_families = set()
366 if ipv4_supported:
367 supported_families.add(trio.socket.AF_INET)
368 if ipv6_supported:
369 supported_families.add(trio.socket.AF_INET6)
370 scenario = Scenario(port, ip_list, supported_families)
371 trio.socket.set_custom_hostname_resolver(scenario)
372 trio.socket.set_custom_socket_factory(scenario)
373
374 try:
375 stream = await open_tcp_stream(
376 "test.example.com",
377 port,
378 happy_eyeballs_delay=happy_eyeballs_delay,
379 local_address=local_address,
380 )
381 assert expect_error == ()
382 scenario.check(stream.socket)
383 return (stream.socket, scenario)
384 except AssertionError: # pragma: no cover
385 raise
386 except expect_error as exc:
387 scenario.check(None)
388 return (exc, scenario)
389
390
391async def test_one_host_quick_success(autojump_clock: MockClock) -> None:

Callers 15

test_one_host_quick_failFunction · 0.85
test_one_host_slow_failFunction · 0.85
test_basic_fallthroughFunction · 0.85
test_early_successFunction · 0.85
test_custom_delayFunction · 0.85
test_none_defaultFunction · 0.85
test_all_failFunction · 0.85
test_multi_successFunction · 0.85

Calls 4

checkMethod · 0.95
open_tcp_streamFunction · 0.90
ScenarioClass · 0.85
addMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…