~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_strace.py

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
 
1
# Copyright (C) 2007-2011 Canonical Ltd
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
17
16
 
18
17
"""Tests for the strace-invoking support."""
19
18
 
20
 
import errno
21
 
import subprocess
22
19
import threading
23
20
 
24
21
from bzrlib import (
 
22
    strace,
25
23
    tests,
26
24
    )
27
 
from bzrlib.strace import StraceFeature, strace_detailed, StraceResult
28
 
 
29
 
 
30
 
class TestStraceFeature(tests.TestCaseWithTransport):
31
 
 
32
 
    def test_strace_detection(self):
33
 
        """Strace is available if its runnable."""
34
 
        try:
35
 
            proc = subprocess.Popen(['strace'],
36
 
                stderr=subprocess.PIPE,
37
 
                stdout=subprocess.PIPE)
38
 
            proc.communicate()
39
 
            found_strace = True
40
 
        except OSError, e:
41
 
            if e.errno == errno.ENOENT:
42
 
                # strace is not installed
43
 
                found_strace = False
44
 
            else:
45
 
                raise
46
 
        self.assertEqual(found_strace, StraceFeature.available())
 
25
from bzrlib.strace import strace_detailed, StraceResult
 
26
from bzrlib.tests.features import (
 
27
    strace_feature,
 
28
    )
47
29
 
48
30
 
49
31
class TestStrace(tests.TestCaseWithTransport):
50
32
 
51
 
    _test_needs_features = [StraceFeature]
 
33
    _test_needs_features = [strace_feature]
 
34
 
 
35
    def setUp(self):
 
36
        # NB: see http://pad.lv/626679 and
 
37
        # <https://code.launchpad.net/~mbp/bzr/626679-strace/+merge/34157>:
 
38
        # testing strace by connecting to ourselves has repeatedly caused
 
39
        # hangs in running the test suite; these are fixable given enough
 
40
        # determination but given that strace is not used by any other tests
 
41
        # at the moment and that it's only test-support code, we just leave it
 
42
        # untested -- mbp 20100901
 
43
        raise tests.TestSkipped("strace selftests are broken and disabled")
52
44
 
53
45
    def _check_threads(self):
54
46
        # For bug #226769, it was decided that the strace tests should not be
58
50
        # restriction.
59
51
        active = threading.activeCount()
60
52
        if active > 1: # There is always the main thread at least
61
 
            raise tests.KnownFailure(
 
53
            self.knownFailure(
62
54
                '%d active threads, bug #103133 needs to be fixed.' % active)
63
55
 
 
56
    def strace_detailed_or_skip(self, *args, **kwargs):
 
57
        """Run strace, but cope if it's not allowed"""
 
58
        try:
 
59
            return strace_detailed(*args, **kwargs)
 
60
        except strace.StraceError, e:
 
61
            if e.err_messages.startswith(
 
62
                    "attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted"):
 
63
                raise tests.TestSkipped("ptrace not permitted")
 
64
            else:
 
65
                raise
 
66
 
64
67
    def test_strace_callable_is_called(self):
65
68
        self._check_threads()
66
69
 
67
70
        output = []
68
71
        def function(positional, *args, **kwargs):
69
72
            output.append((positional, args, kwargs))
70
 
        strace_detailed(function, ["a", "b"], {"c": "c"},
71
 
                        follow_children=False)
 
73
        self.strace_detailed_or_skip(
 
74
            function, ["a", "b"], {"c": "c"},
 
75
            follow_children=False)
72
76
        self.assertEqual([("a", ("b",), {"c":"c"})], output)
73
77
 
74
78
    def test_strace_callable_result(self):
76
80
 
77
81
        def function():
78
82
            return "foo"
79
 
        result, strace_result = strace_detailed(function,[], {},
 
83
        result, strace_result = self.strace_detailed_or_skip(function,[], {},
80
84
                                                follow_children=False)
81
85
        self.assertEqual("foo", result)
82
86
        self.assertIsInstance(strace_result, StraceResult)
87
91
 
88
92
        def function():
89
93
            self.build_tree(['myfile'])
90
 
        unused, result = strace_detailed(function, [], {},
 
94
        unused, result = self.strace_detailed_or_skip(function, [], {},
91
95
                                         follow_children=False)
92
96
        self.assertContainsRe(result.raw_log, 'myfile')