diff --git a/pytest-server-fixtures/pytest_server_fixtures/xvfb.py b/pytest-server-fixtures/pytest_server_fixtures/xvfb.py index 01d858b..58554c6 100644 --- a/pytest-server-fixtures/pytest_server_fixtures/xvfb.py +++ b/pytest-server-fixtures/pytest_server_fixtures/xvfb.py @@ -43,7 +43,10 @@ class XvfbServer(object): def __init__(self): tmpdir = mkdtemp(prefix='XvfbServer.', dir=Workspace.get_base_tempdir()) - for servernum in range(os.getpid(), 65536): + pid_max = 65536 + with open('/proc/sys/kernel/pid_max') as pid_max_file: + pid_max = int(pid_max_file.read()) + for servernum in range(os.getpid(), pid_max): if os.path.exists('/tmp/.X{0}-lock'.format(servernum)): continue self.display = ':' + str(servernum) diff --git a/pytest-server-fixtures/tests/integration/test_xvfb_server.py b/pytest-server-fixtures/tests/integration/test_xvfb_server.py index 96d6315..8ca6424 100644 --- a/pytest-server-fixtures/tests/integration/test_xvfb_server.py +++ b/pytest-server-fixtures/tests/integration/test_xvfb_server.py @@ -5,7 +5,7 @@ from itertools import chain, repeat try: - from unittest.mock import patch + from unittest.mock import mock_open, patch except ImportError: # python 2 from mock import patch @@ -69,3 +69,23 @@ def test_handles_unexpected_failure_to_start(): with raises(RuntimeError) as ex: XvfbServer() assert 'Failed to start Xvfb' in str(ex) + + +def test_handles_64_bit_pids(): + with patch('os.getpid') as mock_getpid: + # This just has to be larger than 65535 + mock_getpid.return_value = 65537 + with XvfbServer() as server: + assert server.display + + +def test_errors_64_bit_pids(): + with patch('os.getpid') as mock_getpid: + target = 'pytest_server_fixtures.xvfb.open' + with patch(target, mock_open(read_data='4')) as m: + # This just has to be larger than the returned max PID + mock_getpid.return_value = 7 + with raises(RuntimeError) as ex: + XvfbServer() + m.assert_called_once_with('/proc/sys/kernel/pid_max') + assert 'Unable to find a free server number to start Xvfb' in str(ex)