~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_signals.py

  • Committer: John Arbash Meinel
  • Date: 2011-09-22 14:45:18 UTC
  • mto: This revision was merged to the branch mainline in revision 6170.
  • Revision ID: john@arbash-meinel.com-20110922144518-0d11ytg0v7clv5xk
Change the code a bit.

Now, by default, there is no global state. It is created as part of
bzrlib.smart.signals.install_sighup_handler().
That way, 99% of bzr commands won't care, only 'bzr serve' is affected.
Further, the *test suite* won't have a by-product of tons of
register/unregister calls that temporarily cache dicts and create
garbage at runtime.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
import gc
19
18
import signal
20
19
import weakref
21
20
 
33
32
        #       with a tearDown that asserts that all the entries have been
34
33
        #       removed properly. Global state is always a bit messy. A shame
35
34
        #       that we need it for signal handling.
36
 
        self.orig__on_sighup = self.overrideAttr(signals, '_on_sighup',
37
 
                                                 weakref.WeakValueDictionary())
 
35
        orig = signals._setup_on_hangup_dict()
 
36
        self.assertIs(None, orig)
 
37
        def cleanup():
 
38
            signals._on_sighup = None
 
39
        self.addCleanup(cleanup)
38
40
 
39
41
    def test_registered_callback_gets_called(self):
40
42
        calls = []
98
100
        # We overrideAttr during the test suite, so that we don't pollute the
99
101
        # original dict. However, we can test that what we override matches
100
102
        # what we are putting there.
101
 
        self.assertIsInstance(self.orig__on_sighup,
 
103
        self.assertIsInstance(signals._on_sighup,
102
104
                              weakref.WeakValueDictionary)
103
105
        calls = []
104
106
        def call_me():
105
107
            calls.append('called')
106
108
        signals.register_on_hangup('myid', call_me)
107
109
        del call_me
108
 
        gc.collect()
 
110
        # Non-CPython might want to do a gc.collect() here
109
111
        signals._sighup_handler(signal.SIGHUP, None)
110
112
        self.assertEqual([], calls)
 
113
 
 
114
    def test_not_installed(self):
 
115
        # If you haven't called bzrlib.smart.signals.install_sighup_handler,
 
116
        # then _on_sighup should be None, and all the calls become no-ops.
 
117
        signals._on_sighup = None
 
118
        calls = []
 
119
        def call_me():
 
120
            calls.append('called')
 
121
        signals.register_on_hangup('myid', calls)
 
122
        signals._sighup_handler(signal.SIGHUP, None)
 
123
        signals.unregister_on_hangup('myid')
 
124
        log = self.get_log()
 
125
        self.assertEqual('', log)