-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaiomisc_pytest.py
839 lines (645 loc) · 23.7 KB
/
aiomisc_pytest.py
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
import abc
import asyncio
import concurrent.futures
import logging
import os
import platform
import socket
import sys
import warnings
from asyncio.events import get_event_loop
from contextlib import contextmanager, suppress
from functools import partial, wraps
from inspect import isasyncgenfunction
from types import ModuleType
from typing import (
Any, AsyncGenerator, Awaitable, Callable, Dict, Generator, Iterable, List,
Mapping, NamedTuple, Optional, Set, Tuple, Type, Union,
)
from unittest.mock import MagicMock
import aiomisc
import pytest
from aiomisc.compat import set_current_loop, sock_set_reuseport
from aiomisc.utils import TimeoutType, bind_socket
from aiomisc_log import LOG_LEVEL, basic_config
log = logging.getLogger("aiomisc_pytest")
USE_UVLOOP = (
os.getenv("AIOMISC_USE_UVLOOP", "1").lower() in ("1", "yes", "true")
)
uvloop_module: Optional[ModuleType]
try:
if USE_UVLOOP:
import uvloop as _uvloop
uvloop_module = _uvloop
except ImportError:
uvloop_module = None
log = logging.getLogger(__name__)
ProxyProcessorType = Callable[[bytes], Awaitable[bytes]]
class Delay:
__slots__ = "__timeout", "future", "lock"
def __init__(self) -> None:
self.__timeout: Union[int, float] = 0
self.future: Optional[asyncio.Future] = None
self.lock = asyncio.Lock()
@property
def timeout(self) -> Union[int, float]:
return self.__timeout
@timeout.setter
def timeout(self, value: TimeoutType) -> None:
assert isinstance(value, (int, float))
assert value >= 0
self.__timeout = value
if self.future and not self.future.done():
self.future.set_result(True)
async def wait(self) -> None:
if self.__timeout == 0:
return
async with self.lock:
try:
self.future = delayed_future(self.__timeout)
await self.future
finally:
self.future = None
def delayed_future(
timeout: Union[int, float], result: bool = True,
) -> asyncio.Future:
loop = asyncio.get_event_loop()
def resolve(f: asyncio.Future) -> None:
nonlocal result
if f.done():
return
f.set_result(result)
future = loop.create_future()
handle = loop.call_later(timeout, resolve, future)
future.add_done_callback(lambda _: handle.cancel())
return future
class TCPProxy:
DEFAULT_TIMEOUT = 30
__slots__ = (
"proxy_port", "clients", "server", "proxy_host",
"target_port", "target_host", "listen_host", "read_delay",
"write_delay", "read_processor", "write_processor", "buffered",
)
def __init__(
self, target_host: str, target_port: int,
listen_host: str = "127.0.0.1", buffered: bool = True,
):
self.target_port = target_port
self.target_host = target_host
self.proxy_host = listen_host
self.proxy_port = unused_port()
self.read_delay: TimeoutType = 0
self.write_delay: TimeoutType = 0
self.buffered = buffered
self.clients: Set[TCPProxyClient] = set()
self.server: Optional[asyncio.AbstractServer] = None
self.read_processor: Optional[ProxyProcessorType] = None
self.write_processor: Optional[ProxyProcessorType] = None
def __repr__(self) -> str:
return "<{}[{:x}]: tcp://{}:{} => tcp://{}:{}>".format(
self.__class__.__name__, id(self),
self.proxy_host, self.proxy_port,
self.target_host, self.target_port,
)
async def start(
self, timeout: Optional[TimeoutType] = None,
) -> asyncio.AbstractServer:
log.debug("Starting %r", self)
server = await asyncio.wait_for(
asyncio.start_server(
self._handle_client,
host=self.proxy_host,
port=self.proxy_port,
), timeout=timeout,
)
self.server = server
return server
ClientType = Tuple[asyncio.StreamReader, asyncio.StreamWriter]
async def create_client(self) -> ClientType:
log.debug("Creating client for %r", self)
return await asyncio.open_connection(
self.proxy_host, self.proxy_port,
)
async def __aenter__(self) -> "TCPProxy":
if self.server is None:
await self.start(timeout=self.DEFAULT_TIMEOUT)
return self
async def __aexit__(
self, exc_type: Type[Exception], exc_val: Exception, exc_tb: Any,
) -> None:
await self.close(timeout=self.DEFAULT_TIMEOUT)
async def close(self, timeout: Optional[TimeoutType] = None) -> None:
async def close() -> None:
await self.disconnect_all()
if self.server is None:
return
self.server.close()
await self.server.wait_closed()
await asyncio.wait_for(close(), timeout=timeout)
def set_delay(
self, read_delay: TimeoutType, write_delay: TimeoutType = 0,
) -> None:
log.debug("Setting delay [R/W %f %f]", read_delay, write_delay)
for client in self.clients:
log.debug(
"Applying delays [R/W: %f %f] for %r",
read_delay, write_delay, client,
)
client.read_delay.timeout = read_delay
client.write_delay.timeout = write_delay
self.read_delay = read_delay
self.write_delay = write_delay
def set_content_processors(
self, read: Optional[ProxyProcessorType],
write: Optional[ProxyProcessorType],
) -> None:
log.debug(
"Setting content processors for %r: read=%r write=%r",
self, read, write,
)
for client in self.clients:
log.debug(
"Applying context processors for %r: read=%r write=%r",
client, read, write,
)
client.read_processor = read # type: ignore
client.write_processor = write # type: ignore
self.read_processor = read
self.write_processor = write
def disconnect_all(self) -> asyncio.Future:
log.debug(
"Disconnecting %s clients of %r", len(self.clients), self,
)
return asyncio.ensure_future(
asyncio.gather(
*[client.close() for client in self.clients],
return_exceptions=True,
),
)
async def _handle_client(
self, reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
) -> None:
client = TCPProxyClient(reader, writer, buffered=self.buffered)
self.clients.add(client)
client.read_delay.timeout = self.read_delay
client.write_delay.timeout = self.write_delay
client.read_processor = self.read_processor # type: ignore
client.write_processor = self.write_processor # type: ignore
await client.connect(self.target_host, self.target_port)
client.closing.add_done_callback(
lambda _: self.clients.remove(client),
)
@contextmanager
def slowdown(
self, read_delay: TimeoutType = 0, write_delay: TimeoutType = 0,
) -> Generator[None, None, None]:
old_read_delay = self.read_delay
old_write_delay = self.write_delay
self.set_delay(read_delay, write_delay)
try:
yield
finally:
self.set_delay(old_read_delay, old_write_delay)
class TCPProxyClient:
__slots__ = (
"client_reader", "client_writer",
"server_reader", "server_writer",
"chunk_size", "tasks", "loop",
"read_delay", "write_delay", "closing",
"__processors", "__client_repr", "__server_repr",
"buffered",
)
@staticmethod
async def _blank_processor(body: bytes) -> bytes:
return body
def __init__(
self, client_reader: asyncio.StreamReader,
client_writer: asyncio.StreamWriter,
chunk_size: int = 64 * 1024, buffered: bool = False,
):
self.loop = asyncio.get_event_loop()
self.client_reader: asyncio.StreamReader = client_reader
self.client_writer: asyncio.StreamWriter = client_writer
self.server_reader: Optional[asyncio.StreamReader] = None
self.server_writer: Optional[asyncio.StreamWriter] = None
self.tasks: Iterable[asyncio.Task] = ()
self.chunk_size = chunk_size # type: int
self.read_delay = Delay()
self.write_delay = Delay()
self.closing: asyncio.Future = self.loop.create_future()
self.__processors: Dict[str, ProxyProcessorType] = {
"read": self._blank_processor, "write": self._blank_processor,
}
self.buffered = bool(buffered)
self.__client_repr = ""
self.__server_repr = ""
@property
def read_processor(self) -> ProxyProcessorType:
return self.__processors["read"]
@read_processor.setter
def read_processor(self, value: Optional[ProxyProcessorType]) -> None:
if value is None:
self.__processors["read"] = self._blank_processor
return
self.__processors["read"] = aiomisc.awaitable(value)
@property
def write_processor(self) -> ProxyProcessorType:
return self.__processors["write"]
@write_processor.setter
def write_processor(self, value: Optional[ProxyProcessorType]) -> None:
if value is None:
self.__processors["write"] = self._blank_processor
return
self.__processors["write"] = aiomisc.awaitable(value)
def __repr__(self) -> str:
return "<{}[{:x}]: {} => {}>".format(
self.__class__.__name__, id(self),
self.__client_repr, self.__server_repr,
)
@staticmethod
async def _close_writer(writer: asyncio.StreamWriter) -> None:
writer.close()
await writer.wait_closed()
async def pipe(
self, reader: asyncio.StreamReader,
writer: asyncio.StreamWriter,
processor: str,
delay: Delay,
) -> None:
try:
while not reader.at_eof():
chunk = await reader.read(self.chunk_size)
if not chunk:
break
if delay.timeout > 0:
log.debug(
"%r sleeping %.3f seconds on %s",
self, delay.timeout, processor,
)
await delay.wait()
writer.write(await self.__processors[processor](chunk))
if not self.buffered:
await writer.drain()
finally:
await self._close_writer(writer)
async def connect(self, target_host: str, target_port: int) -> None:
log.debug("Establishing connection for %r", self)
self.server_reader, self.server_writer = await asyncio.open_connection(
host=target_host, port=target_port,
)
self.__client_repr = ":".join(
map(str, self.client_writer.get_extra_info("peername")[:2]),
)
self.__server_repr = ":".join(
map(str, self.server_writer.get_extra_info("peername")[:2]),
)
self.tasks = (
self.loop.create_task(
self.pipe(
self.server_reader,
self.client_writer,
"write",
self.write_delay,
),
),
self.loop.create_task(
self.pipe(
self.client_reader,
self.server_writer,
"read",
self.read_delay,
),
),
)
async def close(self) -> None:
log.debug("Closing %r", self)
if self.closing.done():
return
await aiomisc.cancel_tasks(self.tasks)
self.loop.call_soon(self.closing.set_result, True)
await self.closing
def unused_port(*args: Any) -> int:
with socket.socket(*args) as sock:
sock.bind(("", 0))
port = sock.getsockname()[1]
return port
@contextmanager
def mock_get_event_loop() -> Generator[Any, MagicMock, None]:
loop_getter = get_event_loop
getter_mock = MagicMock(asyncio.get_event_loop)
getter_mock.side_effect = loop_getter
try:
asyncio.get_event_loop = getter_mock
yield getter_mock
finally:
asyncio.get_event_loop = get_event_loop
@pytest.fixture(scope="session")
def tcp_proxy() -> Type[TCPProxy]:
return TCPProxy
def isasyncgenerator(func: Callable[..., Any]) -> Optional[bool]:
if isasyncgenfunction(func):
return True
elif asyncio.iscoroutinefunction(func):
return False
return None
def pytest_configure(config: pytest.Config) -> None:
config.addinivalue_line(
"markers",
"forbid_get_event_loop: "
"fail when asyncio.get_event_loop will be called",
)
config.addinivalue_line(
"markers",
"catch_loop_exceptions: "
"fails when unhandled loop exception "
"will be raised",
)
def pytest_addoption(parser: pytest.Parser) -> None:
group = parser.getgroup("aiomisc plugin options")
group.addoption(
"--aiomisc-debug", action="store_true", default=False,
help="Set debug for event loop",
)
group.addoption(
"--aiomisc-pool-size", type=int, default=4,
help="Default thread pool size",
)
group.addoption(
"--aiomisc-test-timeout", type=float, default=None,
help="Test timeout",
)
def pytest_fixture_setup(fixturedef, request): # type: ignore
func = fixturedef.func
is_async_gen = isasyncgenerator(func)
if is_async_gen is None:
return
strip_request = False
if "request" not in fixturedef.argnames:
fixturedef.argnames += ("request",)
strip_request = True
if "event_loop" not in request.fixturenames:
raise Exception("`event_loop` fixture required")
# noinspection PyProtectedMember
loop_fixturedef = request._get_active_fixturedef("event_loop")
def wrapper(*args, **kwargs): # type: ignore
if strip_request:
request = kwargs.pop("request")
else:
request = kwargs["request"]
event_loop = request.getfixturevalue("event_loop")
if not is_async_gen:
return event_loop.run_until_complete(func(*args, **kwargs))
gen = func(*args, **kwargs)
def finalizer(): # type: ignore
try:
return event_loop.run_until_complete(gen.__anext__())
except StopAsyncIteration: # NOQA
pass
loop_fixturedef.addfinalizer(
partial(fixturedef.finish, request=request),
)
request.addfinalizer(finalizer)
return event_loop.run_until_complete(gen.__anext__())
fixturedef.func = wrapper
@pytest.fixture(scope="session")
def localhost() -> str:
params = (
(socket.AF_INET, "127.0.0.1"),
(socket.AF_INET6, "::1"),
)
for family, addr in params:
with socket.socket(family, socket.SOCK_STREAM) as sock:
try:
sock.bind((addr, 0))
except Exception:
pass
else:
return addr
raise RuntimeError("localhost unavailable")
@pytest.fixture(scope="session")
def loop_debug(pytestconfig: pytest.Config) -> bool:
return pytestconfig.getoption("--aiomisc-debug")
@pytest.fixture(scope="session")
def aiomisc_test_timeout(
pytestconfig: pytest.Config,
) -> Optional[Union[int, float]]:
return pytestconfig.getoption("--aiomisc-test-timeout")
@pytest.fixture(autouse=True)
def aiomisc_func_wrap() -> Callable:
return aiomisc.awaitable
def pytest_pycollect_makeitem(collector, name, obj): # type: ignore
if collector.funcnamefilter(name) and asyncio.iscoroutinefunction(obj):
return list(collector._genfunctions(name, obj))
@pytest.mark.tryfirst
def pytest_pyfunc_call(pyfuncitem): # type: ignore
if not asyncio.iscoroutinefunction(pyfuncitem.function):
return
event_loop = pyfuncitem.funcargs.get("event_loop", None)
func_wraper = pyfuncitem.funcargs.get(
"aiomisc_func_wrap", aiomisc.awaitable,
)
aiomisc_test_timeout = pyfuncitem.funcargs.get(
"aiomisc_test_timeout", None,
)
kwargs = {
arg: pyfuncitem.funcargs[arg]
for arg in pyfuncitem._fixtureinfo.argnames
}
@wraps(pyfuncitem.obj)
async def func() -> Any:
return await asyncio.wait_for(
func_wraper(pyfuncitem.obj)(**kwargs),
timeout=aiomisc_test_timeout,
)
event_loop.run_until_complete(func())
return True
@pytest.fixture(scope="session")
def thread_pool_size(request: pytest.FixtureRequest) -> int:
return request.config.getoption("--aiomisc-pool-size")
@pytest.fixture
def services() -> Iterable[aiomisc.Service]:
return []
@pytest.fixture
def default_context() -> Mapping[str, Any]:
return {}
loop_autouse = os.getenv("AIOMISC_LOOP_AUTOUSE", "1") == "1"
@pytest.fixture(name="thread_pool_executor")
def _thread_pool_executor() -> Type[concurrent.futures.ThreadPoolExecutor]:
from aiomisc.thread_pool import ThreadPoolExecutor
return ThreadPoolExecutor
@pytest.fixture(autouse=loop_autouse, name="event_loop_policy")
def _event_loop_policy() -> asyncio.AbstractEventLoopPolicy:
if USE_UVLOOP and uvloop_module is not None:
return uvloop_module.EventLoopPolicy()
return asyncio.DefaultEventLoopPolicy()
@pytest.fixture(name="entrypoint_kwargs")
def _entrypoint_kwargs() -> dict:
return {"log_config": False}
@pytest.fixture(autouse=loop_autouse)
def event_loop(
request: pytest.FixtureRequest,
event_loop_policy: asyncio.AbstractEventLoopPolicy,
caplog: pytest.LogCaptureFixture,
thread_pool_size: int,
loop_debug: bool,
thread_pool_executor: Callable[..., concurrent.futures.ThreadPoolExecutor],
) -> Generator[Any, asyncio.AbstractEventLoop, None]:
basic_config(
log_format="plain",
stream=caplog.handler.stream,
)
get_marker = request.node.get_closest_marker
forbid_loop_getter_marker = get_marker("forbid_get_event_loop")
catch_unhandled_marker = get_marker("catch_loop_exceptions")
exceptions: List[Dict[str, Any]] = []
def catch_exceptions(_: Any, catched: Dict[str, Any]) -> None:
nonlocal exceptions
exceptions.append(catched)
try:
asyncio.set_event_loop_policy(event_loop_policy)
loop = asyncio.new_event_loop()
loop.set_debug(loop_debug)
asyncio.set_event_loop(loop)
set_current_loop(loop)
pool = thread_pool_executor(thread_pool_size)
loop.set_default_executor(pool)
if catch_unhandled_marker:
loop.set_exception_handler(catch_exceptions)
if LOG_LEVEL:
LOG_LEVEL.set(logging.getLogger().getEffectiveLevel())
try:
with mock_get_event_loop() as event_loop_getter_mock:
if forbid_loop_getter_marker:
event_loop_getter_mock.side_effect = partial(
pytest.fail, "get_event_loop is forbidden",
)
yield loop
finally:
if exceptions:
logging.error(
"Unhandled exceptions found:\n\n\t%s",
"\n\t".join(
(
"Message: {m}\n\t"
"Future: {f}\n\t"
"Exception: {e}"
).format(
m=e["message"],
f=repr(e.get("future")),
e=repr(e.get("exception")),
) for e in exceptions
),
)
pytest.fail("Unhandled exceptions found. See logs.")
basic_config(
log_format="plain",
stream=sys.stderr,
)
if loop.is_closed():
return
with suppress(Exception):
loop.run_until_complete(loop.shutdown_asyncgens())
with suppress(Exception):
loop.close()
finally:
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
@pytest.fixture
def loop(event_loop: asyncio.AbstractEventLoop) -> asyncio.AbstractEventLoop:
warnings.warn("fixture `loop` is deprecated, use `event_loop` instead")
return event_loop
@pytest.fixture(autouse=loop_autouse)
async def entrypoint(
services: Iterable[aiomisc.Service],
loop_debug: bool,
default_context: Mapping[str, Any],
entrypoint_kwargs: Mapping[str, Any],
thread_pool_size: int,
thread_pool_executor: Callable[..., concurrent.futures.ThreadPoolExecutor],
event_loop: asyncio.AbstractEventLoop,
) -> AsyncGenerator[aiomisc.entrypoint, None]:
from aiomisc.context import get_context
from aiomisc.entrypoint import entrypoint
async with entrypoint(
*services, loop=event_loop, **entrypoint_kwargs
) as ep:
ctx = get_context()
for key, value in default_context.items():
ctx[key] = value
yield ep
def get_unused_port(*args: Any) -> int:
warnings.warn(
(
"Do not use get_unused_port directly, use fixture "
"'aiomisc_unused_port_factory'"
),
DeprecationWarning, stacklevel=2,
)
return unused_port(*args)
class PortSocket(NamedTuple):
port: int
socket: socket.socket
@pytest.fixture
def aiomisc_socket_factory(
request: pytest.FixtureRequest, localhost: str,
) -> Callable[..., PortSocket]:
""" Returns a """
def factory(*args: Any, **kwargs: Any) -> PortSocket:
sock = bind_socket(*args, address=localhost, port=0, **kwargs)
port = sock.getsockname()[1]
# Close socket after teardown
request.addfinalizer(sock.close)
return PortSocket(port=port, socket=sock)
return factory
class SocketWrapper(abc.ABC):
address: str
port: int
def __init__(self, *args: Any):
self._socket_args: Tuple[Any, ...] = args
self.address: str = ""
self.port: int = 0
@abc.abstractmethod
def prepare(self, address: str) -> None:
raise NotImplementedError
def close(self) -> None:
pass
class SocketWrapperUnix(SocketWrapper):
socket: socket.socket
fd: int
def __init__(self, *args: Any):
super().__init__(*args)
self.socket = socket.socket(*args)
self.fd = -1
def close(self) -> None:
self.socket.close()
if self.fd > 0:
os.close(self.fd)
def prepare(self, address: str) -> None:
self.socket.bind((address, 0))
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock_set_reuseport(self.socket, True)
self.address, self.port = self.socket.getsockname()[:2]
# detach socket object and save file descriptor
self.fd = self.socket.detach()
class SocketWrapperWindows(SocketWrapper):
def prepare(self, address: str) -> None:
self.address = address
self.port = unused_port(*self._socket_args)
socket_wrapper: Type[SocketWrapper]
if platform.system() == "Windows":
socket_wrapper = SocketWrapperWindows
else:
socket_wrapper = SocketWrapperUnix
@pytest.fixture
def aiomisc_unused_port_factory(
request: pytest.FixtureRequest, localhost: str,
) -> Callable[[], int]:
def port_factory(*args: Any) -> int:
wrapper = socket_wrapper(*args)
wrapper.prepare("::" if ":" in localhost else "0.0.0.0")
request.addfinalizer(wrapper.close)
return wrapper.port
return port_factory
@pytest.fixture
def aiomisc_unused_port(aiomisc_unused_port_factory: Callable[..., int]) -> int:
return aiomisc_unused_port_factory()