1515
1616from 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-
2218def 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
3027def 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
6660def 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
7670def 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