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 (community) 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
=== modified file 'tests/syncdaemon/test_tunnel_runner.py'
--- tests/syncdaemon/test_tunnel_runner.py 2012-03-19 03:31:10 +0000
+++ tests/syncdaemon/test_tunnel_runner.py 2012-03-23 22:49:18 +0000
@@ -48,6 +48,20 @@
48 self.assertEqual(client, reactor)48 self.assertEqual(client, reactor)
4949
5050
51class FakeProcessTransport(object):
52 """A fake ProcessTransport."""
53
54 pid = 0
55
56 def __init__(self):
57 """Initialize this fake."""
58 self._signals_sent = []
59
60 def signalProcess(self, signalID):
61 """Send a signal to the process."""
62 self._signals_sent.append(signalID)
63
64
51class TunnelRunnerTestCase(TestCase):65class TunnelRunnerTestCase(TestCase):
52 """Tests for the TunnelRunner."""66 """Tests for the TunnelRunner."""
5367
@@ -58,8 +72,22 @@
58 """Initialize this testcase."""72 """Initialize this testcase."""
59 yield super(TunnelRunnerTestCase, self).setUp()73 yield super(TunnelRunnerTestCase, self).setUp()
60 self.spawned = []74 self.spawned = []
61 self.patch(tunnel_client.reactor, "spawnProcess",75 self.triggers = []
62 lambda *args, **kwargs: self.spawned.append((args, kwargs)))76 self.fake_process_transport = FakeProcessTransport()
77
78 def fake_spawn_process(*args, **kwargs):
79 """A fake spawnProcess."""
80 self.spawned.append((args, kwargs))
81 return self.fake_process_transport
82
83 self.patch(tunnel_client.reactor, "spawnProcess", fake_spawn_process)
84
85 def fake_add_system_event_trigger(*args, **kwargs):
86 """A fake addSystemEventTrigger."""
87 self.triggers.append((args, kwargs))
88
89 self.patch(tunnel_client.reactor, "addSystemEventTrigger",
90 fake_add_system_event_trigger)
63 self.process_protocol = None91 self.process_protocol = None
64 self.process_protocol_class = tunnel_client.TunnelProcessProtocol92 self.process_protocol_class = tunnel_client.TunnelProcessProtocol
65 self.patch(tunnel_client, "TunnelProcessProtocol",93 self.patch(tunnel_client, "TunnelProcessProtocol",
@@ -76,6 +104,23 @@
76 self.assertEqual(len(self.spawned), 1,104 self.assertEqual(len(self.spawned), 1,
77 "The tunnel process is started.")105 "The tunnel process is started.")
78106
107 def test_system_event_finished(self):
108 """An event is added to stop the process with the reactor."""
109 expected = [(("before", "shutdown", self.tr.stop), {})]
110 self.assertEqual(self.triggers, expected)
111
112 def test_stop_process(self):
113 """The process is stopped if still running."""
114 self.tr.process_transport.pid = 1234
115 self.tr.stop()
116 self.assertEqual(self.fake_process_transport._signals_sent, ["KILL"])
117
118 def test_not_stopped_if_already_finished(self):
119 """Do not stop the tunnel process if it's already finished."""
120 self.tr.process_transport.pid = None
121 self.tr.stop()
122 self.assertEqual(self.fake_process_transport._signals_sent, [])
123
79 @defer.inlineCallbacks124 @defer.inlineCallbacks
80 def test_tunnel_process_get_client_yielded_twice(self):125 def test_tunnel_process_get_client_yielded_twice(self):
81 """The get_client method can be yielded twice."""126 """The get_client method can be yielded twice."""
82127
=== modified file 'ubuntuone/syncdaemon/tunnel_runner.py'
--- ubuntuone/syncdaemon/tunnel_runner.py 2012-03-19 13:20:36 +0000
+++ ubuntuone/syncdaemon/tunnel_runner.py 2012-03-23 22:49:18 +0000
@@ -33,6 +33,7 @@
33 def __init__(self, host, port):33 def __init__(self, host, port):
34 """Start this runner instance."""34 """Start this runner instance."""
35 self.client_d = defer.Deferred()35 self.client_d = defer.Deferred()
36 self.process_transport = None
36 try:37 try:
37 self.start_process(host, port)38 self.start_process(host, port)
38 except ImportError:39 except ImportError:
@@ -45,7 +46,15 @@
45 protocol = TunnelProcessProtocol(self.client_d)46 protocol = TunnelProcessProtocol(self.client_d)
46 process_path = self.get_process_path()47 process_path = self.get_process_path()
47 args = [TUNNEL_EXECUTABLE, host, str(port)]48 args = [TUNNEL_EXECUTABLE, host, str(port)]
48 reactor.spawnProcess(protocol, process_path, env=None, args=args)49 self.process_transport = reactor.spawnProcess(protocol, process_path,
50 env=None, args=args)
51 reactor.addSystemEventTrigger("before", "shutdown", self.stop)
52
53 def stop(self):
54 """Stop the tunnel process if still running."""
55 logger.info("Stopping process %r", self.process_transport.pid)
56 if self.process_transport.pid is not None:
57 self.process_transport.signalProcess("KILL")
4958
50 def get_process_path(self):59 def get_process_path(self):
51 """Get the path to the tunnel process."""60 """Get the path to the tunnel process."""

Subscribers

People subscribed via source and target branches