Improve reliability of fork test

This commit is contained in:
David Cramer 2015-03-29 11:43:31 -07:00
parent 1deda056ce
commit 5fe0fccced
1 changed files with 12 additions and 3 deletions

View File

@ -75,6 +75,7 @@ class ThreadedTransportTest(TestCase):
try:
os.close(fd)
transport = LoggingThreadedScheme(filename, url)
# Log from the parent process - starts the worker thread
transport.async_send(event1, None, None, None)
childpid = os.fork()
@ -83,13 +84,21 @@ class ThreadedTransportTest(TestCase):
transport.async_send(event2, None, None, None)
time.sleep(0.1)
os._exit(0)
# Wait for the child process to finish
os.waitpid(childpid, 0)
self.assertTrue(os.path.isfile(filename))
assert os.path.isfile(filename)
# Ensure threaded worker has finished
transport.get_worker().stop()
with open(filename, 'r') as logfile:
events = dict(x.strip().split() for x in logfile.readlines())
# Check parent and child both logged successfully
self.assertEqual(events, {str(os.getpid()): 'parent',
str(childpid): 'child'})
assert events == {
str(os.getpid()): 'parent',
str(childpid): 'child',
}
finally:
os.remove(filename)