sapling/tests/test-fb-hgext-patchpython.py
Kostia Balytskyi b27a46c987 fb-hgext: fix copied fb-hgext tests
Summary:
This is a big bulk of generally almost-obvious fixes to the moved tests. Mostly
these fixes have to do with correct importing of the actual extensions.

Depends on D6675329

Test Plan:
- ./run-tests.py fails less after this commit
- see further commits for more test fixes

Reviewers: #sourcecontrol

Differential Revision: https://phabricator.intern.facebook.com/D6675344
2018-01-09 03:06:09 -08:00

64 lines
1.7 KiB
Python

import errno
import os
import signal
import socket
import sys
import time
try:
import SocketServer
except ImportError:
# Python 3
import socketserver as SocketServer
# Make sure we use patchpython.py in this repo, unaffected by PYTHONPATH
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../hgext'))
import patchpython
assert patchpython # pass pyflakes "import but unused" check
def testnozombies():
class reportpidhandler(SocketServer.StreamRequestHandler):
def handle(self):
self.wfile.write('%s' % (os.getpid(),))
class server(SocketServer.ForkingMixIn, SocketServer.UnixStreamServer):
pass
socketpath = 'testsocket'
pid = os.fork()
if pid > 0:
# client
waittime = 0
while not os.path.exists(socketpath):
time.sleep(0.1)
waittime += 0.1
if waittime > 5:
assert False, 'server timed out'
try:
pids = []
for i in xrange(5):
s = socket.socket(socket.AF_UNIX)
s.connect(socketpath)
buf = s.recv(1024)
s.close()
pids.append(int(buf))
# give the server some time to do cleanup
time.sleep(0.5)
for p in pids:
try:
os.kill(p, 0)
assert False, 'zombie process detected'
except OSError as ex:
if ex.errno != errno.ESRCH:
raise
finally:
os.kill(pid, signal.SIGTERM) # stop server
os.unlink(socketpath)
else:
# server
s = server(socketpath, reportpidhandler)
s.serve_forever(poll_interval=0.1)
testnozombies()