Skip to content

Commit 2a9c537

Browse files
authored
Manage p4d process in unit tests more precisely (#201)
1 parent f29698c commit 2a9c537

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

python/test_perforce.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
from perforce import P4Repo
1717

18-
# Time after which the p4 server will automatically be shut-down.
19-
__P4D_TIMEOUT__ = 30
20-
# __P4D_TIMEOUT__ = None
21-
2218
def find_free_port():
2319
"""Find an open port that we could run a perforce server on"""
2420
# pylint: disable=no-member
@@ -27,6 +23,7 @@ def find_free_port():
2723
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
2824
return sock.getsockname()[1]
2925

26+
@contextmanager
3027
def run_p4d(p4port, from_zip=None):
3128
"""Start a perforce server with the given hostname:port.
3229
Optionally unzip server state from a file
@@ -56,21 +53,18 @@ def run_p4d(p4port, from_zip=None):
5653
os.chmod(os.path.join(p4ssldir, 'certificate.txt'), 0o600)
5754
os.environ['P4SSLDIR'] = p4ssldir
5855
os.environ['P4TRUST'] = p4trust
59-
try:
60-
subprocess.check_output(["p4d", "-r", tmpdir, "-p", p4port],
61-
timeout=__P4D_TIMEOUT__)
62-
except subprocess.TimeoutExpired:
63-
pass
56+
57+
yield subprocess.Popen(['p4d', '-r', tmpdir, '-p', p4port])
6458

6559
@pytest.fixture
6660
def server():
6761
"""Start a p4 server in the background and return the address"""
6862
port = find_free_port()
6963
p4port = 'ssl:localhost:%s' % port
70-
Thread(target=partial(run_p4d, p4port, from_zip='server.zip'), daemon=True).start()
71-
time.sleep(1)
7264
os.environ['P4PORT'] = p4port
73-
return p4port
65+
with run_p4d(p4port, from_zip='server.zip'):
66+
time.sleep(1)
67+
yield p4port
7468

7569
@pytest.fixture
7670
def tmpdir():
@@ -96,7 +90,6 @@ def test_server_fixture(capsys, server):
9690
repo = P4Repo()
9791

9892
# To change the fixture server, uncomment the line below with 'store_server' and put a breakpoint on it
99-
# Change __P4D_TIMEOUT__ to 'None' or an otherwise large amount of time
10093
# Run unit tests in the debugger and hit the breakpoint
10194
# Log in using details printed to stdout (port/user) via p4v or the command line
10295
# Make changes to the p4 server

0 commit comments

Comments
 (0)