~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
"""Blackbox tests for debugger breakin"""
18
 
 
19
 
import os
20
 
import signal
21
 
import subprocess
22
 
import sys
23
 
import time
24
 
 
25
 
from bzrlib import tests
26
 
 
27
 
 
28
 
class TestBreakin(tests.TestCase):
29
 
    # FIXME: If something is broken, these tests may just hang indefinitely in
30
 
    # wait() waiting for the child to exit when it's not going to.
31
 
 
32
 
    def setUp(self):
33
 
        if sys.platform == 'win32':
34
 
            raise tests.TestSkipped('breakin signal not tested on win32')
35
 
        super(TestBreakin, self).setUp()
36
 
 
37
 
    def _dont_SIGQUIT_on_darwin(self):
38
 
        if sys.platform == 'darwin':
39
 
            # At least on Leopard and with python 2.6, this test will raise a
40
 
            # popup window asking if the python failure should be reported to
41
 
            # Apple... That's not the point of the test :) Marking the test as
42
 
            # not applicable Until we find a way to disable that intrusive
43
 
            # behavior... --vila20080611
44
 
            raise tests.TestNotApplicable(
45
 
                '%s raises a popup on OSX' % self.id())
46
 
 
47
 
    # port 0 means to allocate any port
48
 
    _test_process_args = ['serve', '--port', 'localhost:0']
49
 
 
50
 
    def test_breakin(self):
51
 
        # Break in to a debugger while bzr is running
52
 
        # we need to test against a command that will wait for 
53
 
        # a while -- bzr serve should do
54
 
        proc = self.start_bzr_subprocess(self._test_process_args,
55
 
                env_changes=dict(BZR_SIGQUIT_PDB=None))
56
 
        # wait for it to get started, and print the 'listening' line
57
 
        proc.stderr.readline()
58
 
        # first sigquit pops into debugger
59
 
        os.kill(proc.pid, signal.SIGQUIT)
60
 
        proc.stdin.write("q\n")
61
 
        time.sleep(.5)
62
 
        err = proc.stderr.readline()
63
 
        self.assertContainsRe(err, r'entering debugger')
64
 
 
65
 
    def test_breakin_harder(self):
66
 
        self._dont_SIGQUIT_on_darwin()
67
 
        proc = self.start_bzr_subprocess(self._test_process_args,
68
 
                env_changes=dict(BZR_SIGQUIT_PDB=None))
69
 
        # wait for it to get started, and print the 'listening' line
70
 
        proc.stderr.readline()
71
 
        # break into the debugger
72
 
        os.kill(proc.pid, signal.SIGQUIT)
73
 
        # now send a second sigquit, which should cause it to exit.  That
74
 
        # won't happen until the original signal has been noticed by the
75
 
        # child and it's run its signal handler.  We don't know quite how long
76
 
        # this will take, but if it's more than 10s then it's probably not
77
 
        # going to work.
78
 
        for i in range(100):
79
 
            time.sleep(0.1)
80
 
            os.kill(proc.pid, signal.SIGQUIT)
81
 
            # note: waitpid is different on win32, but this test only runs on
82
 
            # unix
83
 
            r = os.waitpid(proc.pid, os.WNOHANG)
84
 
            if r != (0, 0):
85
 
                # high bit says if core was dumped; we don't care
86
 
                self.assertEquals(r[1] & 0x7f, signal.SIGQUIT)
87
 
                break
88
 
        else:
89
 
            self.fail("subprocess wasn't terminated by repeated SIGQUIT")
90
 
 
91
 
    def test_breakin_disabled(self):
92
 
        self._dont_SIGQUIT_on_darwin()
93
 
        proc = self.start_bzr_subprocess(self._test_process_args,
94
 
                env_changes=dict(BZR_SIGQUIT_PDB='0'))
95
 
        # wait for it to get started, and print the 'listening' line
96
 
        proc.stderr.readline()
97
 
        # first hit should just kill it
98
 
        os.kill(proc.pid, signal.SIGQUIT)
99
 
        proc.wait()