/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_serve.py

  • Committer: Vincent Ladeuil
  • Date: 2011-07-06 09:22:00 UTC
  • mfrom: (6008 +trunk)
  • mto: (6012.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6013.
  • Revision ID: v.ladeuil+lp@free.fr-20110706092200-7iai2mwzc0sqdsvf
MergingĀ inĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Tests of the bzr serve command."""
19
19
 
20
20
import os
21
 
import os.path
22
21
import signal
23
 
import subprocess
24
22
import sys
25
23
import thread
26
24
import threading
27
25
 
28
26
from bzrlib import (
29
27
    builtins,
30
 
    debug,
31
28
    errors,
32
29
    osutils,
33
30
    revision as _mod_revision,
 
31
    trace,
34
32
    transport,
35
33
    urlutils,
36
34
    )
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 (
 
39
    BzrServerFactory,
 
40
    SmartTCPServer,
 
41
    )
41
42
from bzrlib.tests import (
42
43
    TestCaseWithMemoryTransport,
43
44
    TestCaseWithTransport,
44
 
    TestSkipped,
45
45
    )
46
 
from bzrlib.trace import mutter
47
46
from bzrlib.transport import remote
48
47
 
49
48
 
53
52
                                *func_args, **func_kwargs):
54
53
        """Run 'bzr serve', and run the given func in a thread once the server
55
54
        has started.
56
 
        
 
55
 
57
56
        When 'func' terminates, the server will be terminated too.
58
 
        
 
57
 
59
58
        Returns stdout and stderr.
60
59
        """
61
 
        # install hook
62
 
        def on_server_start(backing_urls, tcp_server):
63
 
            t = threading.Thread(
64
 
                target=on_server_start_thread, args=(tcp_server,))
65
 
            t.start()
66
60
        def on_server_start_thread(tcp_server):
 
61
            """This runs concurrently with the server thread.
 
62
 
 
63
            The server is interrupted as soon as ``func`` finishes, even if an
 
64
            exception is encountered.
 
65
            """
67
66
            try:
68
67
                # Run func if set
69
68
                self.tcp_server = tcp_server
73
72
                    except Exception, e:
74
73
                        # Log errors to make some test failures a little less
75
74
                        # mysterious.
76
 
                        mutter('func broke: %r', e)
 
75
                        trace.mutter('func broke: %r', e)
77
76
            finally:
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
 
81
        # return
 
82
        def on_server_start(backing_urls, tcp_server):
 
83
            t = threading.Thread(
 
84
                target=on_server_start_thread, args=(tcp_server,))
 
85
            t.start()
 
86
        # install hook
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
85
91
        try:
86
 
            out, err = self.run_bzr(['serve'] + list(serve_args))
 
92
            out, err = self.run_bzr(['serve'] + list(serve_args),
 
93
                                    retcode=retcode)
87
94
        except KeyboardInterrupt, e:
88
95
            out, err = e.args
89
96
        return out, err
95
102
        super(TestBzrServe, self).setUp()
96
103
        self.disable_missing_extensions_warning()
97
104
 
 
105
    def test_server_exception_with_hook(self):
 
106
        """Catch exception from the server in the server_exception hook.
 
107
 
 
108
        We use ``run_bzr_serve_then_func`` without a ``func`` so the server
 
109
        will receive a KeyboardInterrupt exception we want to catch.
 
110
        """
 
111
        def hook(exception):
 
112
            if exception[0] is KeyboardInterrupt:
 
113
                sys.stderr.write('catching KeyboardInterrupt\n')
 
114
                return True
 
115
            else:
 
116
                return False
 
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)
 
123
 
 
124
    def test_server_exception_no_hook(self):
 
125
        """test exception without hook returns error"""
 
126
        args = []
 
127
        out, err = self.run_bzr_serve_then_func(args, retcode=3)
 
128
 
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
164
195
        url = 'bzr://localhost:%d/' % port
165
196
        self.permit_url(url)
166
197
        return process, url
167
 
    
 
198
 
168
199
    def test_bzr_serve_quiet(self):
169
200
        self.make_branch('.')
170
201
        args = ['--port', 'localhost:0', '--quiet']
276
307
 
277
308
class TestUserdirExpansion(TestCaseWithMemoryTransport):
278
309
 
279
 
    def fake_expanduser(self, path):
 
310
    @staticmethod
 
311
    def fake_expanduser(path):
280
312
        """A simple, environment-independent, function for the duration of this
281
313
        test.
282
314
 
334
366
        self.assertEqual(base_url, self.bzr_serve_transport.base)
335
367
        self.assertEqual(base_dir,
336
368
            server_maker.get_base_path(self.bzr_serve_transport))
337