provide-foundation 0.0.0.dev0__py3-none-any.whl → 0.0.0.dev2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. provide/foundation/__init__.py +41 -23
  2. provide/foundation/archive/__init__.py +23 -0
  3. provide/foundation/archive/base.py +70 -0
  4. provide/foundation/archive/bzip2.py +157 -0
  5. provide/foundation/archive/gzip.py +159 -0
  6. provide/foundation/archive/operations.py +334 -0
  7. provide/foundation/archive/tar.py +164 -0
  8. provide/foundation/archive/zip.py +203 -0
  9. provide/foundation/cli/__init__.py +2 -2
  10. provide/foundation/cli/commands/deps.py +13 -7
  11. provide/foundation/cli/commands/logs/__init__.py +1 -1
  12. provide/foundation/cli/commands/logs/query.py +1 -1
  13. provide/foundation/cli/commands/logs/send.py +1 -1
  14. provide/foundation/cli/commands/logs/tail.py +1 -1
  15. provide/foundation/cli/decorators.py +11 -10
  16. provide/foundation/cli/main.py +1 -1
  17. provide/foundation/cli/testing.py +2 -35
  18. provide/foundation/cli/utils.py +21 -17
  19. provide/foundation/config/__init__.py +35 -2
  20. provide/foundation/config/base.py +2 -2
  21. provide/foundation/config/converters.py +479 -0
  22. provide/foundation/config/defaults.py +67 -0
  23. provide/foundation/config/env.py +4 -19
  24. provide/foundation/config/loader.py +9 -3
  25. provide/foundation/config/sync.py +19 -4
  26. provide/foundation/console/input.py +5 -5
  27. provide/foundation/console/output.py +35 -13
  28. provide/foundation/context/__init__.py +8 -4
  29. provide/foundation/context/core.py +85 -109
  30. provide/foundation/core.py +1 -2
  31. provide/foundation/crypto/__init__.py +2 -0
  32. provide/foundation/crypto/certificates/__init__.py +34 -0
  33. provide/foundation/crypto/certificates/base.py +173 -0
  34. provide/foundation/crypto/certificates/certificate.py +290 -0
  35. provide/foundation/crypto/certificates/factory.py +213 -0
  36. provide/foundation/crypto/certificates/generator.py +138 -0
  37. provide/foundation/crypto/certificates/loader.py +130 -0
  38. provide/foundation/crypto/certificates/operations.py +198 -0
  39. provide/foundation/crypto/certificates/trust.py +107 -0
  40. provide/foundation/errors/__init__.py +2 -3
  41. provide/foundation/errors/decorators.py +0 -231
  42. provide/foundation/errors/types.py +0 -97
  43. provide/foundation/eventsets/__init__.py +0 -0
  44. provide/foundation/eventsets/display.py +84 -0
  45. provide/foundation/eventsets/registry.py +160 -0
  46. provide/foundation/eventsets/resolver.py +192 -0
  47. provide/foundation/eventsets/sets/das.py +128 -0
  48. provide/foundation/eventsets/sets/database.py +125 -0
  49. provide/foundation/eventsets/sets/http.py +153 -0
  50. provide/foundation/eventsets/sets/llm.py +139 -0
  51. provide/foundation/eventsets/sets/task_queue.py +107 -0
  52. provide/foundation/eventsets/types.py +70 -0
  53. provide/foundation/file/directory.py +13 -22
  54. provide/foundation/file/lock.py +3 -1
  55. provide/foundation/hub/components.py +77 -515
  56. provide/foundation/hub/config.py +151 -0
  57. provide/foundation/hub/discovery.py +62 -0
  58. provide/foundation/hub/handlers.py +81 -0
  59. provide/foundation/hub/lifecycle.py +194 -0
  60. provide/foundation/hub/manager.py +4 -4
  61. provide/foundation/hub/processors.py +44 -0
  62. provide/foundation/integrations/__init__.py +11 -0
  63. provide/foundation/{observability → integrations}/openobserve/__init__.py +10 -7
  64. provide/foundation/{observability → integrations}/openobserve/auth.py +1 -1
  65. provide/foundation/{observability → integrations}/openobserve/client.py +12 -12
  66. provide/foundation/{observability → integrations}/openobserve/commands.py +3 -3
  67. provide/foundation/integrations/openobserve/config.py +37 -0
  68. provide/foundation/{observability → integrations}/openobserve/formatters.py +1 -1
  69. provide/foundation/{observability → integrations}/openobserve/otlp.py +1 -1
  70. provide/foundation/{observability → integrations}/openobserve/search.py +2 -2
  71. provide/foundation/{observability → integrations}/openobserve/streaming.py +4 -4
  72. provide/foundation/logger/__init__.py +3 -10
  73. provide/foundation/logger/config/logging.py +68 -298
  74. provide/foundation/logger/config/telemetry.py +41 -121
  75. provide/foundation/logger/core.py +0 -2
  76. provide/foundation/logger/custom_processors.py +1 -0
  77. provide/foundation/logger/factories.py +11 -2
  78. provide/foundation/logger/processors/main.py +20 -84
  79. provide/foundation/logger/setup/__init__.py +5 -1
  80. provide/foundation/logger/setup/coordinator.py +76 -24
  81. provide/foundation/logger/setup/processors.py +2 -9
  82. provide/foundation/logger/trace.py +27 -0
  83. provide/foundation/metrics/otel.py +10 -10
  84. provide/foundation/observability/__init__.py +2 -2
  85. provide/foundation/process/__init__.py +9 -0
  86. provide/foundation/process/exit.py +47 -0
  87. provide/foundation/process/lifecycle.py +115 -59
  88. provide/foundation/resilience/__init__.py +35 -0
  89. provide/foundation/resilience/circuit.py +164 -0
  90. provide/foundation/resilience/decorators.py +220 -0
  91. provide/foundation/resilience/fallback.py +193 -0
  92. provide/foundation/resilience/retry.py +325 -0
  93. provide/foundation/streams/config.py +79 -0
  94. provide/foundation/streams/console.py +7 -8
  95. provide/foundation/streams/core.py +6 -3
  96. provide/foundation/streams/file.py +12 -2
  97. provide/foundation/testing/__init__.py +84 -2
  98. provide/foundation/testing/archive/__init__.py +24 -0
  99. provide/foundation/testing/archive/fixtures.py +217 -0
  100. provide/foundation/testing/cli.py +30 -17
  101. provide/foundation/testing/common/__init__.py +32 -0
  102. provide/foundation/testing/common/fixtures.py +236 -0
  103. provide/foundation/testing/file/__init__.py +40 -0
  104. provide/foundation/testing/file/content_fixtures.py +316 -0
  105. provide/foundation/testing/file/directory_fixtures.py +107 -0
  106. provide/foundation/testing/file/fixtures.py +52 -0
  107. provide/foundation/testing/file/special_fixtures.py +153 -0
  108. provide/foundation/testing/logger.py +117 -11
  109. provide/foundation/testing/mocking/__init__.py +46 -0
  110. provide/foundation/testing/mocking/fixtures.py +331 -0
  111. provide/foundation/testing/process/__init__.py +48 -0
  112. provide/foundation/testing/process/async_fixtures.py +405 -0
  113. provide/foundation/testing/process/fixtures.py +56 -0
  114. provide/foundation/testing/process/subprocess_fixtures.py +209 -0
  115. provide/foundation/testing/threading/__init__.py +38 -0
  116. provide/foundation/testing/threading/basic_fixtures.py +101 -0
  117. provide/foundation/testing/threading/data_fixtures.py +99 -0
  118. provide/foundation/testing/threading/execution_fixtures.py +263 -0
  119. provide/foundation/testing/threading/fixtures.py +54 -0
  120. provide/foundation/testing/threading/sync_fixtures.py +97 -0
  121. provide/foundation/testing/time/__init__.py +32 -0
  122. provide/foundation/testing/time/fixtures.py +409 -0
  123. provide/foundation/testing/transport/__init__.py +30 -0
  124. provide/foundation/testing/transport/fixtures.py +280 -0
  125. provide/foundation/tools/__init__.py +58 -0
  126. provide/foundation/tools/base.py +348 -0
  127. provide/foundation/tools/cache.py +268 -0
  128. provide/foundation/tools/downloader.py +224 -0
  129. provide/foundation/tools/installer.py +254 -0
  130. provide/foundation/tools/registry.py +223 -0
  131. provide/foundation/tools/resolver.py +321 -0
  132. provide/foundation/tools/verifier.py +186 -0
  133. provide/foundation/tracer/otel.py +7 -11
  134. provide/foundation/tracer/spans.py +2 -2
  135. provide/foundation/transport/__init__.py +155 -0
  136. provide/foundation/transport/base.py +171 -0
  137. provide/foundation/transport/client.py +266 -0
  138. provide/foundation/transport/config.py +140 -0
  139. provide/foundation/transport/errors.py +79 -0
  140. provide/foundation/transport/http.py +232 -0
  141. provide/foundation/transport/middleware.py +360 -0
  142. provide/foundation/transport/registry.py +167 -0
  143. provide/foundation/transport/types.py +45 -0
  144. provide/foundation/utils/deps.py +14 -12
  145. provide/foundation/utils/parsing.py +49 -4
  146. {provide_foundation-0.0.0.dev0.dist-info → provide_foundation-0.0.0.dev2.dist-info}/METADATA +5 -28
  147. provide_foundation-0.0.0.dev2.dist-info/RECORD +225 -0
  148. provide/foundation/cli/commands/logs/generate_old.py +0 -569
  149. provide/foundation/crypto/certificates.py +0 -896
  150. provide/foundation/logger/emoji/__init__.py +0 -44
  151. provide/foundation/logger/emoji/matrix.py +0 -209
  152. provide/foundation/logger/emoji/sets.py +0 -458
  153. provide/foundation/logger/emoji/types.py +0 -56
  154. provide/foundation/logger/setup/emoji_resolver.py +0 -64
  155. provide_foundation-0.0.0.dev0.dist-info/RECORD +0 -149
  156. /provide/foundation/{observability → integrations}/openobserve/exceptions.py +0 -0
  157. /provide/foundation/{observability → integrations}/openobserve/models.py +0 -0
  158. {provide_foundation-0.0.0.dev0.dist-info → provide_foundation-0.0.0.dev2.dist-info}/WHEEL +0 -0
  159. {provide_foundation-0.0.0.dev0.dist-info → provide_foundation-0.0.0.dev2.dist-info}/entry_points.txt +0 -0
  160. {provide_foundation-0.0.0.dev0.dist-info → provide_foundation-0.0.0.dev2.dist-info}/licenses/LICENSE +0 -0
  161. {provide_foundation-0.0.0.dev0.dist-info → provide_foundation-0.0.0.dev2.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,405 @@
1
+ """
2
+ Async-specific test fixtures for process testing.
3
+
4
+ Provides fixtures for testing async operations, event loops, and
5
+ async context management across the provide-io ecosystem.
6
+ """
7
+
8
+ import asyncio
9
+ from unittest.mock import AsyncMock
10
+ from collections.abc import AsyncGenerator, Callable
11
+
12
+ import pytest
13
+
14
+
15
+ @pytest.fixture
16
+ async def clean_event_loop() -> AsyncGenerator[None, None]:
17
+ """
18
+ Ensure clean event loop for async tests.
19
+
20
+ Cancels all pending tasks after the test to prevent event loop issues.
21
+
22
+ Yields:
23
+ None - fixture for test setup/teardown.
24
+ """
25
+ yield
26
+
27
+ # Clean up any pending tasks
28
+ loop = asyncio.get_event_loop()
29
+ pending = asyncio.all_tasks(loop)
30
+
31
+ for task in pending:
32
+ if not task.done():
33
+ task.cancel()
34
+
35
+ # Wait for all tasks to complete cancellation
36
+ if pending:
37
+ await asyncio.gather(*pending, return_exceptions=True)
38
+
39
+
40
+ @pytest.fixture
41
+ def async_timeout() -> Callable[[float], asyncio.Task]:
42
+ """
43
+ Provide configurable timeout wrapper for async operations.
44
+
45
+ Returns:
46
+ A function that wraps async operations with a timeout.
47
+ """
48
+ def _timeout_wrapper(coro, seconds: float = 5.0):
49
+ """
50
+ Wrap a coroutine with a timeout.
51
+
52
+ Args:
53
+ coro: Coroutine to wrap
54
+ seconds: Timeout in seconds
55
+
56
+ Returns:
57
+ Result of the coroutine or raises asyncio.TimeoutError
58
+ """
59
+ return asyncio.wait_for(coro, timeout=seconds)
60
+
61
+ return _timeout_wrapper
62
+
63
+
64
+ @pytest.fixture
65
+ def event_loop_policy():
66
+ """
67
+ Set event loop policy for tests to avoid conflicts.
68
+
69
+ Returns:
70
+ New event loop policy for isolated testing.
71
+ """
72
+ policy = asyncio.get_event_loop_policy()
73
+ new_policy = asyncio.DefaultEventLoopPolicy()
74
+ asyncio.set_event_loop_policy(new_policy)
75
+
76
+ yield new_policy
77
+
78
+ # Restore original policy
79
+ asyncio.set_event_loop_policy(policy)
80
+
81
+
82
+ @pytest.fixture
83
+ async def async_context_manager():
84
+ """
85
+ Factory for creating mock async context managers.
86
+
87
+ Returns:
88
+ Function that creates configured async context manager mocks.
89
+ """
90
+ def _create_async_cm(enter_value=None, exit_value=None):
91
+ """
92
+ Create a mock async context manager.
93
+
94
+ Args:
95
+ enter_value: Value to return from __aenter__
96
+ exit_value: Value to return from __aexit__
97
+
98
+ Returns:
99
+ AsyncMock configured as context manager
100
+ """
101
+ mock_cm = AsyncMock()
102
+ mock_cm.__aenter__ = AsyncMock(return_value=enter_value)
103
+ mock_cm.__aexit__ = AsyncMock(return_value=exit_value)
104
+ return mock_cm
105
+
106
+ return _create_async_cm
107
+
108
+
109
+ @pytest.fixture
110
+ async def async_iterator():
111
+ """
112
+ Factory for creating mock async iterators.
113
+
114
+ Returns:
115
+ Function that creates async iterator mocks with specified values.
116
+ """
117
+ def _create_async_iter(values):
118
+ """
119
+ Create a mock async iterator.
120
+
121
+ Args:
122
+ values: List of values to yield
123
+
124
+ Returns:
125
+ Async iterator that yields the specified values
126
+ """
127
+ class AsyncIterator:
128
+ def __init__(self, vals):
129
+ self.vals = vals
130
+ self.index = 0
131
+
132
+ def __aiter__(self):
133
+ return self
134
+
135
+ async def __anext__(self):
136
+ if self.index >= len(self.vals):
137
+ raise StopAsyncIteration
138
+ value = self.vals[self.index]
139
+ self.index += 1
140
+ return value
141
+
142
+ return AsyncIterator(values)
143
+
144
+ return _create_async_iter
145
+
146
+
147
+ @pytest.fixture
148
+ def async_queue():
149
+ """
150
+ Create an async queue for testing producer/consumer patterns.
151
+
152
+ Returns:
153
+ asyncio.Queue instance for testing.
154
+ """
155
+ return asyncio.Queue()
156
+
157
+
158
+ @pytest.fixture
159
+ async def async_lock():
160
+ """
161
+ Create an async lock for testing synchronization.
162
+
163
+ Returns:
164
+ asyncio.Lock instance for testing.
165
+ """
166
+ return asyncio.Lock()
167
+
168
+
169
+ @pytest.fixture
170
+ def mock_async_sleep():
171
+ """
172
+ Mock asyncio.sleep to speed up tests.
173
+
174
+ Returns:
175
+ Mock that replaces asyncio.sleep with instant return.
176
+ """
177
+ original_sleep = asyncio.sleep
178
+
179
+ async def instant_sleep(seconds):
180
+ """Sleep replacement that returns immediately."""
181
+ return None
182
+
183
+ asyncio.sleep = instant_sleep
184
+
185
+ yield instant_sleep
186
+
187
+ # Restore original
188
+ asyncio.sleep = original_sleep
189
+
190
+
191
+ @pytest.fixture
192
+ def async_gather_helper():
193
+ """
194
+ Helper for testing asyncio.gather operations.
195
+
196
+ Returns:
197
+ Function to gather async results with error handling.
198
+ """
199
+ async def _gather(*coroutines, return_exceptions: bool = False):
200
+ """
201
+ Gather results from multiple coroutines.
202
+
203
+ Args:
204
+ *coroutines: Coroutines to gather
205
+ return_exceptions: Whether to return exceptions as results
206
+
207
+ Returns:
208
+ List of results from coroutines
209
+ """
210
+ return await asyncio.gather(*coroutines, return_exceptions=return_exceptions)
211
+
212
+ return _gather
213
+
214
+
215
+ @pytest.fixture
216
+ def async_task_group():
217
+ """
218
+ Manage a group of async tasks with cleanup.
219
+
220
+ Returns:
221
+ AsyncTaskGroup instance for managing tasks.
222
+ """
223
+ class AsyncTaskGroup:
224
+ def __init__(self):
225
+ self.tasks = []
226
+
227
+ def create_task(self, coro):
228
+ """Create and track a task."""
229
+ task = asyncio.create_task(coro)
230
+ self.tasks.append(task)
231
+ return task
232
+
233
+ async def wait_all(self, timeout: float = None):
234
+ """Wait for all tasks to complete."""
235
+ if not self.tasks:
236
+ return []
237
+
238
+ done, pending = await asyncio.wait(
239
+ self.tasks,
240
+ timeout=timeout,
241
+ return_when=asyncio.ALL_COMPLETED
242
+ )
243
+
244
+ if pending:
245
+ for task in pending:
246
+ task.cancel()
247
+
248
+ results = []
249
+ for task in done:
250
+ try:
251
+ results.append(task.result())
252
+ except Exception as e:
253
+ results.append(e)
254
+
255
+ return results
256
+
257
+ async def cancel_all(self):
258
+ """Cancel all tasks."""
259
+ for task in self.tasks:
260
+ if not task.done():
261
+ task.cancel()
262
+
263
+ if self.tasks:
264
+ await asyncio.gather(*self.tasks, return_exceptions=True)
265
+
266
+ async def __aenter__(self):
267
+ return self
268
+
269
+ async def __aexit__(self, *args):
270
+ await self.cancel_all()
271
+
272
+ return AsyncTaskGroup()
273
+
274
+
275
+ @pytest.fixture
276
+ def async_condition_waiter():
277
+ """
278
+ Helper for waiting on async conditions in tests.
279
+
280
+ Returns:
281
+ Function to wait for conditions with timeout.
282
+ """
283
+ async def _wait_for(condition: Callable[[], bool], timeout: float = 5.0, interval: float = 0.1):
284
+ """
285
+ Wait for a condition to become true.
286
+
287
+ Args:
288
+ condition: Function that returns True when condition is met
289
+ timeout: Maximum wait time
290
+ interval: Check interval
291
+
292
+ Returns:
293
+ True if condition met, False if timeout
294
+ """
295
+ start = asyncio.get_event_loop().time()
296
+
297
+ while asyncio.get_event_loop().time() - start < timeout:
298
+ if condition():
299
+ return True
300
+ await asyncio.sleep(interval)
301
+
302
+ return False
303
+
304
+ return _wait_for
305
+
306
+
307
+ @pytest.fixture
308
+ def async_pipeline():
309
+ """
310
+ Create an async pipeline for testing data flow.
311
+
312
+ Returns:
313
+ AsyncPipeline instance for chaining async operations.
314
+ """
315
+ class AsyncPipeline:
316
+ def __init__(self):
317
+ self.stages = []
318
+ self.results = []
319
+
320
+ def add_stage(self, func: Callable):
321
+ """Add a processing stage."""
322
+ self.stages.append(func)
323
+ return self
324
+
325
+ async def process(self, data):
326
+ """Process data through all stages."""
327
+ result = data
328
+ for stage in self.stages:
329
+ if asyncio.iscoroutinefunction(stage):
330
+ result = await stage(result)
331
+ else:
332
+ result = stage(result)
333
+ self.results.append(result)
334
+ return result
335
+
336
+ async def process_batch(self, items: list):
337
+ """Process multiple items."""
338
+ tasks = [self.process(item) for item in items]
339
+ return await asyncio.gather(*tasks)
340
+
341
+ def clear(self):
342
+ """Clear stages and results."""
343
+ self.stages.clear()
344
+ self.results.clear()
345
+
346
+ return AsyncPipeline()
347
+
348
+
349
+ @pytest.fixture
350
+ def async_rate_limiter():
351
+ """
352
+ Create an async rate limiter for testing.
353
+
354
+ Returns:
355
+ AsyncRateLimiter instance for controlling request rates.
356
+ """
357
+ class AsyncRateLimiter:
358
+ def __init__(self, rate: int = 10, per: float = 1.0):
359
+ self.rate = rate
360
+ self.per = per
361
+ self.allowance = rate
362
+ self.last_check = asyncio.get_event_loop().time()
363
+
364
+ async def acquire(self):
365
+ """Acquire permission to proceed."""
366
+ current = asyncio.get_event_loop().time()
367
+ time_passed = current - self.last_check
368
+ self.last_check = current
369
+
370
+ self.allowance += time_passed * (self.rate / self.per)
371
+ if self.allowance > self.rate:
372
+ self.allowance = self.rate
373
+
374
+ if self.allowance < 1.0:
375
+ sleep_time = (1.0 - self.allowance) * (self.per / self.rate)
376
+ await asyncio.sleep(sleep_time)
377
+ self.allowance = 0.0
378
+ else:
379
+ self.allowance -= 1.0
380
+
381
+ async def __aenter__(self):
382
+ await self.acquire()
383
+ return self
384
+
385
+ async def __aexit__(self, *args):
386
+ pass
387
+
388
+ return AsyncRateLimiter()
389
+
390
+
391
+ __all__ = [
392
+ "clean_event_loop",
393
+ "async_timeout",
394
+ "event_loop_policy",
395
+ "async_context_manager",
396
+ "async_iterator",
397
+ "async_queue",
398
+ "async_lock",
399
+ "mock_async_sleep",
400
+ "async_gather_helper",
401
+ "async_task_group",
402
+ "async_condition_waiter",
403
+ "async_pipeline",
404
+ "async_rate_limiter",
405
+ ]
@@ -0,0 +1,56 @@
1
+ """
2
+ Process Test Fixtures.
3
+
4
+ Core process testing fixtures with re-exports from specialized modules.
5
+ Utilities for testing async code, managing event loops, and handling
6
+ async subprocess mocking across the provide-io ecosystem.
7
+ """
8
+
9
+ # Re-export all fixtures from specialized modules
10
+ from provide.foundation.testing.process.async_fixtures import (
11
+ clean_event_loop,
12
+ async_timeout,
13
+ event_loop_policy,
14
+ async_context_manager,
15
+ async_iterator,
16
+ async_queue,
17
+ async_lock,
18
+ mock_async_sleep,
19
+ async_gather_helper,
20
+ async_task_group,
21
+ async_condition_waiter,
22
+ async_pipeline,
23
+ async_rate_limiter,
24
+ )
25
+
26
+ from provide.foundation.testing.process.subprocess_fixtures import (
27
+ mock_async_process,
28
+ async_stream_reader,
29
+ async_subprocess,
30
+ async_mock_server,
31
+ async_test_client,
32
+ )
33
+
34
+
35
+ __all__ = [
36
+ # Async fixtures
37
+ "clean_event_loop",
38
+ "async_timeout",
39
+ "event_loop_policy",
40
+ "async_context_manager",
41
+ "async_iterator",
42
+ "async_queue",
43
+ "async_lock",
44
+ "mock_async_sleep",
45
+ "async_gather_helper",
46
+ "async_task_group",
47
+ "async_condition_waiter",
48
+ "async_pipeline",
49
+ "async_rate_limiter",
50
+ # Subprocess fixtures
51
+ "mock_async_process",
52
+ "async_stream_reader",
53
+ "async_subprocess",
54
+ "async_mock_server",
55
+ "async_test_client",
56
+ ]
@@ -0,0 +1,209 @@
1
+ """
2
+ Subprocess-specific test fixtures for process testing.
3
+
4
+ Provides fixtures for mocking and testing subprocess operations,
5
+ stream handling, and process communication.
6
+ """
7
+
8
+ import asyncio
9
+ from unittest.mock import AsyncMock, Mock
10
+
11
+ import pytest
12
+
13
+
14
+ @pytest.fixture
15
+ def mock_async_process() -> AsyncMock:
16
+ """
17
+ Mock async subprocess for testing.
18
+
19
+ Returns:
20
+ AsyncMock configured as a subprocess with common attributes.
21
+ """
22
+ mock_process = AsyncMock()
23
+ mock_process.communicate = AsyncMock(return_value=(b"output", b""))
24
+ mock_process.returncode = 0
25
+ mock_process.pid = 12345
26
+ mock_process.stdin = AsyncMock()
27
+ mock_process.stdout = AsyncMock()
28
+ mock_process.stderr = AsyncMock()
29
+ mock_process.wait = AsyncMock(return_value=0)
30
+ mock_process.kill = Mock()
31
+ mock_process.terminate = Mock()
32
+
33
+ return mock_process
34
+
35
+
36
+ @pytest.fixture
37
+ async def async_stream_reader() -> AsyncMock:
38
+ """
39
+ Mock async stream reader for subprocess stdout/stderr.
40
+
41
+ Returns:
42
+ AsyncMock configured as a stream reader.
43
+ """
44
+ reader = AsyncMock()
45
+
46
+ # Simulate reading lines
47
+ async def readline_side_effect():
48
+ for line in [b"line1\n", b"line2\n", b""]:
49
+ yield line
50
+
51
+ reader.readline = AsyncMock(side_effect=readline_side_effect().__anext__)
52
+ reader.read = AsyncMock(return_value=b"full content")
53
+ reader.at_eof = Mock(side_effect=[False, False, True])
54
+
55
+ return reader
56
+
57
+
58
+ @pytest.fixture
59
+ def async_subprocess():
60
+ """
61
+ Create mock async subprocess for testing.
62
+
63
+ Returns:
64
+ Function that creates mock subprocess with configurable behavior.
65
+ """
66
+ def _create_subprocess(
67
+ returncode: int = 0,
68
+ stdout: bytes = b"",
69
+ stderr: bytes = b"",
70
+ pid: int = 12345
71
+ ) -> AsyncMock:
72
+ """
73
+ Create a mock async subprocess.
74
+
75
+ Args:
76
+ returncode: Process return code
77
+ stdout: Process stdout output
78
+ stderr: Process stderr output
79
+ pid: Process ID
80
+
81
+ Returns:
82
+ AsyncMock configured as subprocess
83
+ """
84
+ process = AsyncMock()
85
+ process.returncode = returncode
86
+ process.pid = pid
87
+ process.communicate = AsyncMock(return_value=(stdout, stderr))
88
+ process.wait = AsyncMock(return_value=returncode)
89
+ process.kill = Mock()
90
+ process.terminate = Mock()
91
+ process.send_signal = Mock()
92
+
93
+ # Add stdout/stderr as async stream readers
94
+ process.stdout = AsyncMock()
95
+ process.stdout.read = AsyncMock(return_value=stdout)
96
+ process.stdout.readline = AsyncMock(side_effect=[stdout, b""])
97
+ process.stdout.at_eof = Mock(side_effect=[False, True])
98
+
99
+ process.stderr = AsyncMock()
100
+ process.stderr.read = AsyncMock(return_value=stderr)
101
+ process.stderr.readline = AsyncMock(side_effect=[stderr, b""])
102
+ process.stderr.at_eof = Mock(side_effect=[False, True])
103
+
104
+ process.stdin = AsyncMock()
105
+ process.stdin.write = AsyncMock()
106
+ process.stdin.drain = AsyncMock()
107
+ process.stdin.close = Mock()
108
+
109
+ return process
110
+
111
+ return _create_subprocess
112
+
113
+
114
+ @pytest.fixture
115
+ def async_mock_server():
116
+ """
117
+ Create a mock async server for testing.
118
+
119
+ Returns:
120
+ Mock server with async methods.
121
+ """
122
+ class AsyncMockServer:
123
+ def __init__(self):
124
+ self.started = False
125
+ self.connections = []
126
+ self.requests = []
127
+
128
+ async def start(self, host: str = "localhost", port: int = 8080):
129
+ """Start the mock server."""
130
+ self.started = True
131
+ self.host = host
132
+ self.port = port
133
+
134
+ async def stop(self):
135
+ """Stop the mock server."""
136
+ self.started = False
137
+ for conn in self.connections:
138
+ await conn.close()
139
+
140
+ async def handle_connection(self, reader, writer):
141
+ """Mock connection handler."""
142
+ conn = {"reader": reader, "writer": writer}
143
+ self.connections.append(conn)
144
+
145
+ # Mock reading request
146
+ data = await reader.read(1024)
147
+ self.requests.append(data)
148
+
149
+ # Mock sending response
150
+ writer.write(b"HTTP/1.1 200 OK\r\n\r\nOK")
151
+ await writer.drain()
152
+
153
+ writer.close()
154
+ await writer.wait_closed()
155
+
156
+ def get_url(self) -> str:
157
+ """Get server URL."""
158
+ return f"http://{self.host}:{self.port}"
159
+
160
+ return AsyncMockServer()
161
+
162
+
163
+ @pytest.fixture
164
+ def async_test_client():
165
+ """
166
+ Create an async HTTP test client.
167
+
168
+ Returns:
169
+ Mock async HTTP client for testing.
170
+ """
171
+ class AsyncTestClient:
172
+ def __init__(self):
173
+ self.responses = {}
174
+ self.requests = []
175
+
176
+ def set_response(self, url: str, response: dict):
177
+ """Set a mock response for a URL."""
178
+ self.responses[url] = response
179
+
180
+ async def get(self, url: str, **kwargs) -> dict:
181
+ """Mock GET request."""
182
+ self.requests.append({"method": "GET", "url": url, "kwargs": kwargs})
183
+ return self.responses.get(url, {"status": 404, "body": "Not Found"})
184
+
185
+ async def post(self, url: str, data=None, **kwargs) -> dict:
186
+ """Mock POST request."""
187
+ self.requests.append({"method": "POST", "url": url, "data": data, "kwargs": kwargs})
188
+ return self.responses.get(url, {"status": 200, "body": "OK"})
189
+
190
+ async def close(self):
191
+ """Close the client."""
192
+ pass
193
+
194
+ async def __aenter__(self):
195
+ return self
196
+
197
+ async def __aexit__(self, *args):
198
+ await self.close()
199
+
200
+ return AsyncTestClient()
201
+
202
+
203
+ __all__ = [
204
+ "mock_async_process",
205
+ "async_stream_reader",
206
+ "async_subprocess",
207
+ "async_mock_server",
208
+ "async_test_client",
209
+ ]
@@ -0,0 +1,38 @@
1
+ """
2
+ Threading testing utilities for the provide-io ecosystem.
3
+
4
+ Fixtures and utilities for testing multi-threaded code, thread synchronization,
5
+ and concurrent operations across any project that depends on provide.foundation.
6
+ """
7
+
8
+ from provide.foundation.testing.threading.fixtures import (
9
+ test_thread,
10
+ thread_pool,
11
+ thread_barrier,
12
+ thread_safe_list,
13
+ thread_safe_counter,
14
+ thread_event,
15
+ thread_condition,
16
+ mock_thread,
17
+ thread_local_storage,
18
+ concurrent_executor,
19
+ thread_synchronizer,
20
+ deadlock_detector,
21
+ thread_exception_handler,
22
+ )
23
+
24
+ __all__ = [
25
+ "test_thread",
26
+ "thread_pool",
27
+ "thread_barrier",
28
+ "thread_safe_list",
29
+ "thread_safe_counter",
30
+ "thread_event",
31
+ "thread_condition",
32
+ "mock_thread",
33
+ "thread_local_storage",
34
+ "concurrent_executor",
35
+ "thread_synchronizer",
36
+ "deadlock_detector",
37
+ "thread_exception_handler",
38
+ ]