Commit aa353b5e authored by alina's avatar alina

Added unit test to check that protocol.Server kills alive children when shutdown.

parent 97e7d152
......@@ -115,26 +115,26 @@ class Server(object):
# -PID to kill to whole process group
os.kill(-pid, signal.SIGTERM)
now = time.time()
ch = set(self._children)
while time.time() - now < KILL_WAIT:
ch = set(self._children)
for pid in ch:
try:
if nemu.subprocess_.poll(pid):
ch.remove(pid)
self._children.remove(pid)
except OSError, e:
if e.errno == errno.ECHILD:
ch.remove(pid)
self._children.remove(pid)
else:
raise
if not ch:
break
time.sleep(0.1)
for pid in ch:
for pid in self._children:
warning("Killing forcefully process %d." % pid)
# -PID to kill to whole process group
os.kill(-pid, signal.SIGKILL)
for pid in ch:
for pid in self._children:
try:
nemu.subprocess_.poll(pid)
except OSError, e:
......
......@@ -43,6 +43,27 @@ class TestServer(unittest.TestCase):
s2.close()
t.join()
def test_server_clean(self):
(s0, s1) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
def run_server():
nemu.protocol.Server(s0, s0).run()
t = threading.Thread(target = run_server)
t.start()
cli = nemu.protocol.Client(s1, s1)
null = file('/dev/null', 'wb')
argv = [ '/bin/sh', '-c', 'yes' ]
pid = cli.spawn(argv, stdout = null)
self.assertTrue(os.path.exists("/proc/%d" % pid))
# try to exit while there are still processes running
cli.shutdown()
t.join()
# Check that the process was killed.
# We are asumming that the pid is not going to be reused fast enough
# to generate a false possitive.
self.assertFalse(os.path.exists("/proc/%d" % pid))
def test_spawn_recovery(self):
(s0, s1) = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment