Merge lp:~alecu/ubuntuone-client/stop-proxy-tunnel into lp:ubuntuone-client

Proposed by Alejandro J. Cura
Status: Merged
Approved by: Alejandro J. Cura
Approved revision: 1216
Merged at revision: 1216
Proposed branch: lp:~alecu/ubuntuone-client/stop-proxy-tunnel
Merge into: lp:ubuntuone-client
Diff against target: 102 lines (+57/-3)
2 files modified
tests/syncdaemon/test_tunnel_runner.py (+47/-2)
ubuntuone/syncdaemon/tunnel_runner.py (+10/-1)
To merge this branch: bzr merge lp:~alecu/ubuntuone-client/stop-proxy-tunnel
Reviewer Review Type Date Requested Status
Diego Sarmentero (community) Approve
Natalia Bidart Approve
Review via email: mp+99134@code.launchpad.net

Commit message

- Stop the proxy tunnel when stopping the reactor (LP: #963485).

Description of the change

- Stop the proxy tunnel when stopping the reactor (LP: #963485).

To post a comment you must log in.
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Looks good.

review: Approve
Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/syncdaemon/test_tunnel_runner.py'
2--- tests/syncdaemon/test_tunnel_runner.py 2012-03-19 03:31:10 +0000
3+++ tests/syncdaemon/test_tunnel_runner.py 2012-03-23 22:49:18 +0000
4@@ -48,6 +48,20 @@
5 self.assertEqual(client, reactor)
6
7
8+class FakeProcessTransport(object):
9+ """A fake ProcessTransport."""
10+
11+ pid = 0
12+
13+ def __init__(self):
14+ """Initialize this fake."""
15+ self._signals_sent = []
16+
17+ def signalProcess(self, signalID):
18+ """Send a signal to the process."""
19+ self._signals_sent.append(signalID)
20+
21+
22 class TunnelRunnerTestCase(TestCase):
23 """Tests for the TunnelRunner."""
24
25@@ -58,8 +72,22 @@
26 """Initialize this testcase."""
27 yield super(TunnelRunnerTestCase, self).setUp()
28 self.spawned = []
29- self.patch(tunnel_client.reactor, "spawnProcess",
30- lambda *args, **kwargs: self.spawned.append((args, kwargs)))
31+ self.triggers = []
32+ self.fake_process_transport = FakeProcessTransport()
33+
34+ def fake_spawn_process(*args, **kwargs):
35+ """A fake spawnProcess."""
36+ self.spawned.append((args, kwargs))
37+ return self.fake_process_transport
38+
39+ self.patch(tunnel_client.reactor, "spawnProcess", fake_spawn_process)
40+
41+ def fake_add_system_event_trigger(*args, **kwargs):
42+ """A fake addSystemEventTrigger."""
43+ self.triggers.append((args, kwargs))
44+
45+ self.patch(tunnel_client.reactor, "addSystemEventTrigger",
46+ fake_add_system_event_trigger)
47 self.process_protocol = None
48 self.process_protocol_class = tunnel_client.TunnelProcessProtocol
49 self.patch(tunnel_client, "TunnelProcessProtocol",
50@@ -76,6 +104,23 @@
51 self.assertEqual(len(self.spawned), 1,
52 "The tunnel process is started.")
53
54+ def test_system_event_finished(self):
55+ """An event is added to stop the process with the reactor."""
56+ expected = [(("before", "shutdown", self.tr.stop), {})]
57+ self.assertEqual(self.triggers, expected)
58+
59+ def test_stop_process(self):
60+ """The process is stopped if still running."""
61+ self.tr.process_transport.pid = 1234
62+ self.tr.stop()
63+ self.assertEqual(self.fake_process_transport._signals_sent, ["KILL"])
64+
65+ def test_not_stopped_if_already_finished(self):
66+ """Do not stop the tunnel process if it's already finished."""
67+ self.tr.process_transport.pid = None
68+ self.tr.stop()
69+ self.assertEqual(self.fake_process_transport._signals_sent, [])
70+
71 @defer.inlineCallbacks
72 def test_tunnel_process_get_client_yielded_twice(self):
73 """The get_client method can be yielded twice."""
74
75=== modified file 'ubuntuone/syncdaemon/tunnel_runner.py'
76--- ubuntuone/syncdaemon/tunnel_runner.py 2012-03-19 13:20:36 +0000
77+++ ubuntuone/syncdaemon/tunnel_runner.py 2012-03-23 22:49:18 +0000
78@@ -33,6 +33,7 @@
79 def __init__(self, host, port):
80 """Start this runner instance."""
81 self.client_d = defer.Deferred()
82+ self.process_transport = None
83 try:
84 self.start_process(host, port)
85 except ImportError:
86@@ -45,7 +46,15 @@
87 protocol = TunnelProcessProtocol(self.client_d)
88 process_path = self.get_process_path()
89 args = [TUNNEL_EXECUTABLE, host, str(port)]
90- reactor.spawnProcess(protocol, process_path, env=None, args=args)
91+ self.process_transport = reactor.spawnProcess(protocol, process_path,
92+ env=None, args=args)
93+ reactor.addSystemEventTrigger("before", "shutdown", self.stop)
94+
95+ def stop(self):
96+ """Stop the tunnel process if still running."""
97+ logger.info("Stopping process %r", self.process_transport.pid)
98+ if self.process_transport.pid is not None:
99+ self.process_transport.signalProcess("KILL")
100
101 def get_process_path(self):
102 """Get the path to the tunnel process."""

Subscribers

People subscribed via source and target branches