Skip to content

Commit 7c0dd29

Browse files
committed
pytest-server-fixtures: Support large pids in xvfb
Since a large number of 64 bit distributions are now using larger PIDs than 65535 by default, and the xvfb fixtures makes the assumption that PIDs are going to be less than that, check what the maximum is, falling back to 65535 if we can't.
1 parent 0018cc5 commit 7c0dd29

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

pytest-server-fixtures/pytest_server_fixtures/xvfb.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ class XvfbServer(object):
4343

4444
def __init__(self):
4545
tmpdir = mkdtemp(prefix='XvfbServer.', dir=Workspace.get_base_tempdir())
46-
for servernum in range(os.getpid(), 65536):
46+
pid_max = 65536
47+
with open('/proc/sys/kernel/pid_max') as pid_max_file:
48+
pid_max = int(pid_max_file.read())
49+
for servernum in range(os.getpid(), pid_max):
4750
if os.path.exists('/tmp/.X{0}-lock'.format(servernum)):
4851
continue
4952
self.display = ':' + str(servernum)

pytest-server-fixtures/tests/integration/test_xvfb_server.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from itertools import chain, repeat
66

77
try:
8-
from unittest.mock import patch
8+
from unittest.mock import mock_open, patch
99
except ImportError:
1010
# python 2
1111
from mock import patch
@@ -69,3 +69,23 @@ def test_handles_unexpected_failure_to_start():
6969
with raises(RuntimeError) as ex:
7070
XvfbServer()
7171
assert 'Failed to start Xvfb' in str(ex)
72+
73+
74+
def test_handles_64_bit_pids():
75+
with patch('os.getpid') as mock_getpid:
76+
# This just has to be larger than 65535
77+
mock_getpid.return_value = 65537
78+
with XvfbServer() as server:
79+
assert server.display
80+
81+
82+
def test_errors_64_bit_pids():
83+
with patch('os.getpid') as mock_getpid:
84+
target = 'pytest_server_fixtures.xvfb.open'
85+
with patch(target, mock_open(read_data='4')) as m:
86+
# This just has to be larger than the returned max PID
87+
mock_getpid.return_value = 7
88+
with raises(RuntimeError) as ex:
89+
XvfbServer()
90+
m.assert_called_once_with('/proc/sys/kernel/pid_max')
91+
assert 'Unable to find a free server number to start Xvfb' in str(ex)

0 commit comments

Comments
 (0)