18
18
"""Tests of the bzr serve command."""
28
26
from bzrlib import (
33
30
revision as _mod_revision,
37
35
from bzrlib.branch import Branch
38
36
from bzrlib.bzrdir import BzrDir
39
37
from bzrlib.smart import client, medium
40
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
38
from bzrlib.smart.server import (
41
42
from bzrlib.tests import (
42
43
TestCaseWithMemoryTransport,
43
44
TestCaseWithTransport,
46
from bzrlib.trace import mutter
47
46
from bzrlib.transport import remote
53
52
*func_args, **func_kwargs):
54
53
"""Run 'bzr serve', and run the given func in a thread once the server
57
56
When 'func' terminates, the server will be terminated too.
59
58
Returns stdout and stderr.
62
def on_server_start(backing_urls, tcp_server):
64
target=on_server_start_thread, args=(tcp_server,))
66
60
def on_server_start_thread(tcp_server):
61
"""This runs concurrently with the server thread.
63
The server is interrupted as soon as ``func`` finishes, even if an
64
exception is encountered.
69
68
self.tcp_server = tcp_server
73
72
except Exception, e:
74
73
# Log errors to make some test failures a little less
76
mutter('func broke: %r', e)
75
trace.mutter('func broke: %r', e)
78
77
# Then stop the server
79
mutter('interrupting...')
78
trace.mutter('interrupting...')
80
79
thread.interrupt_main()
80
# When the hook is fired, it just starts ``on_server_start_thread`` and
82
def on_server_start(backing_urls, tcp_server):
84
target=on_server_start_thread, args=(tcp_server,))
81
87
SmartTCPServer.hooks.install_named_hook(
82
88
'server_started_ex', on_server_start,
83
89
'run_bzr_serve_then_func hook')
84
90
# start a TCP server
86
out, err = self.run_bzr(['serve'] + list(serve_args))
92
out, err = self.run_bzr(['serve'] + list(serve_args),
87
94
except KeyboardInterrupt, e:
95
102
super(TestBzrServe, self).setUp()
96
103
self.disable_missing_extensions_warning()
105
def test_server_exception_with_hook(self):
106
"""Catch exception from the server in the server_exception hook.
108
We use ``run_bzr_serve_then_func`` without a ``func`` so the server
109
will receive a KeyboardInterrupt exception we want to catch.
112
if exception[0] is KeyboardInterrupt:
113
sys.stderr.write('catching KeyboardInterrupt\n')
117
SmartTCPServer.hooks.install_named_hook(
118
'server_exception', hook,
119
'test_server_except_hook hook')
120
args = ['--port', 'localhost:0', '--quiet']
121
out, err = self.run_bzr_serve_then_func(args, retcode=0)
122
self.assertEqual('catching KeyboardInterrupt\n', err)
124
def test_server_exception_no_hook(self):
125
"""test exception without hook returns error"""
127
out, err = self.run_bzr_serve_then_func(args, retcode=3)
98
129
def assertInetServerShutsdownCleanly(self, process):
99
130
"""Shutdown the server process looking for errors."""
100
131
# Shutdown the server: the server should shut down when it cannot read
277
308
class TestUserdirExpansion(TestCaseWithMemoryTransport):
279
def fake_expanduser(self, path):
311
def fake_expanduser(path):
280
312
"""A simple, environment-independent, function for the duration of this