Skip to content

Commit

Permalink
fixup! Two step shutdown with sigint
Browse files Browse the repository at this point in the history
  • Loading branch information
ocaballeror committed Feb 16, 2018
1 parent e1f0b75 commit 6b977f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
1 change: 0 additions & 1 deletion osbrain/nameserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def shutdown_all(self):
# In case the agent was force killed and did not unregister
pass


def shutdown(self):
"""
Shutdown the name server. All agents will be shutdown as well.
Expand Down
9 changes: 8 additions & 1 deletion osbrain/tests/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import psutil
from psutil import STATUS_DEAD, STATUS_ZOMBIE

from osbrain import run_agent
from osbrain import run_logger
Expand Down Expand Up @@ -37,4 +38,10 @@ def is_pid_alive(pid):
"""
Check if a given PID corresponds to a currently active process.
"""
return psutil.Process(pid) != psutil.STATUS_RUNNING
dead_statuses = [STATUS_ZOMBIE, STATUS_DEAD]
try:
process = psutil.Process(pid)
print('%d is %s', pid, process.status())
return process.status() not in dead_statuses
except psutil.NoSuchProcess:
return False
4 changes: 2 additions & 2 deletions osbrain/tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_agent_shutdown(nsproxy):
assert 'a0' not in nsproxy.list()


def test_sigint_agent_kill(nsproxy):
def test_agent_sigint_kill(nsproxy):
"""
Test SIGINT signal on an agent.
Expand All @@ -153,7 +153,7 @@ def get_pid(self):
agent_pid = agent.get_pid()

os.kill(agent_pid, signal.SIGINT)
time.sleep(2)
os.wait()
assert not is_pid_alive(agent_pid)


Expand Down
21 changes: 9 additions & 12 deletions osbrain/tests/test_nameserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,16 @@ def get_pid(self):

ns = run_nameserver(base=NewNameServer)
ns_pid = ns.get_pid()
agent = run_agent('agent')
agent = run_agent('agent', base=NewAgent)
agent_pid = agent.get_pid()

os.kill(ns_pid, signal.SIGINT)

# Give some time for the agent to be shut down
time.sleep(2)

# Check agent is not alive
with pytest.raises(Exception):
assert agent.ping() == 'pong'

# Check the server process is dead
# Check that both the agent and the nameserver are not alive
assert not is_pid_alive(agent_pid)
assert not is_pid_alive(ns_pid)


Expand All @@ -292,28 +290,27 @@ def get_pid(self):
return os.getpid()

ns = run_nameserver(base=NewNameServer)
ns_addr = ns.addr()
ns_pid = ns.get_pid()

# Create many agents so the nameserver does not have time to shut them all
# down
pids = []
for i in range(10):
AgentProcess('agent'+str(i), nsaddr=ns_addr, base=NewAgent).start()
agent = Proxy('agent'+str(i))
agent.run()
for i in range(15):
agent = run_agent('agent'+str(i), base=NewAgent)
pids.append(agent.get_pid())

# Send a second sigint without any time for the agents to shutdown
os.kill(ns_pid, signal.SIGINT)
time.sleep(.01)
os.kill(ns_pid, signal.SIGINT)

# Check that some of the agents are still alive
assert any(is_pid_alive(pid) for pid in pids)
for pid in pids:
os.kill(pid, signal.SIGTERM)

# Check the server process is really dead
# Wait for the name server process to be dead
time.sleep(2)
assert not is_pid_alive(ns_pid)


Expand Down

0 comments on commit 6b977f3

Please sign in to comment.