sanic/tests/test_multiprocessing.py

30 lines
871 B
Python
Raw Normal View History

import multiprocessing
import random
import signal
2018-09-29 18:54:47 +01:00
import pytest
2018-03-16 04:28:52 +00:00
from sanic.testing import HOST, PORT
2016-10-18 09:22:49 +01:00
2018-09-29 18:54:47 +01:00
@pytest.mark.skipif(
not hasattr(signal, 'SIGALRM'),
reason='SIGALRM is not implemented for this platform, we have to come '
'up with another timeout strategy to test these'
)
2018-08-26 15:43:14 +01:00
def test_multiprocessing(app):
"""Tests that the number of children we produce is correct"""
# Selects a number at random so we can spot check
2018-09-29 18:54:47 +01:00
num_workers = random.choice(range(2, multiprocessing.cpu_count() * 2 + 1))
process_list = set()
def stop_on_alarm(*args):
for process in multiprocessing.active_children():
process_list.add(process.pid)
process.terminate()
signal.signal(signal.SIGALRM, stop_on_alarm)
2018-03-16 05:06:58 +00:00
signal.alarm(3)
2018-03-16 04:28:52 +00:00
app.run(HOST, PORT, workers=num_workers)
assert len(process_list) == num_workers