|
3 | 3 | import logging |
4 | 4 | import random |
5 | 5 | import time |
| 6 | +from concurrent.futures import _base |
6 | 7 | from threading import local |
7 | 8 |
|
8 | 9 | import pytest |
@@ -38,6 +39,15 @@ def fail(): |
38 | 39 | print(hello) |
39 | 40 |
|
40 | 41 |
|
| 42 | +class CustomThreadPoolExecutor(concurrent.futures.ThreadPoolExecutor): |
| 43 | + class _Future(_base.Future): |
| 44 | + def __getattr__(self, item): |
| 45 | + return lambda: True |
| 46 | + |
| 47 | + def submit(self, fn, /, *args, **kwargs): |
| 48 | + return self._Future() |
| 49 | + |
| 50 | + |
41 | 51 | def test_init(app): |
42 | 52 | executor = Executor(app) |
43 | 53 | assert 'executor' in app.extensions |
@@ -67,6 +77,20 @@ def test_process_executor_init(default_app): |
67 | 77 | assert isinstance(executor, concurrent.futures.ProcessPoolExecutor) |
68 | 78 |
|
69 | 79 |
|
| 80 | +def test_custom_executor_init(default_app): |
| 81 | + default_app.config['EXECUTOR_TYPE'] = 'custom' |
| 82 | + default_app.config['EXECUTOR_POOL_CLASS'] = CustomThreadPoolExecutor |
| 83 | + executor = Executor(default_app) |
| 84 | + assert isinstance(executor._self, CustomThreadPoolExecutor) |
| 85 | + assert isinstance(executor, CustomThreadPoolExecutor) |
| 86 | + |
| 87 | + |
| 88 | +def test_invalid_process_custom_init(default_app): |
| 89 | + default_app.config['EXECUTOR_TYPE'] = 'custom' |
| 90 | + with pytest.raises(ValueError): |
| 91 | + _ = Executor(default_app) |
| 92 | + |
| 93 | + |
70 | 94 | def test_default_executor_init(default_app): |
71 | 95 | executor = Executor(default_app) |
72 | 96 | assert isinstance(executor._self, concurrent.futures.ThreadPoolExecutor) |
@@ -319,6 +343,15 @@ def decorated(n): |
319 | 343 | assert future.result() == fib(5) |
320 | 344 |
|
321 | 345 |
|
| 346 | +def test_custom_executor_getarrt(default_app): |
| 347 | + default_app.config['EXECUTOR_TYPE'] = 'custom' |
| 348 | + default_app.config['EXECUTOR_POOL_CLASS'] = CustomThreadPoolExecutor |
| 349 | + executor = Executor(default_app) |
| 350 | + with default_app.test_request_context(''): |
| 351 | + executor.submit_stored('fibonacci', fib, 35) |
| 352 | + assert executor.futures.custom_func('fibonacci') |
| 353 | + |
| 354 | + |
322 | 355 | thread_local = local() |
323 | 356 |
|
324 | 357 |
|
|
0 commit comments