~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testtransactions.py

  • Committer: Robert Collins
  • Date: 2005-10-11 01:05:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1438.
  • Revision ID: robertc@robertcollins.net-20051011010524-e258bc8d051cc9d2
add a cache bound to Transactions, and a precious facility, so that we keep inventory.weave in memory, but can discard weaves for other such files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import bzrlib.transactions as transactions
28
28
 
29
29
 
 
30
class DummyWeave(object):
 
31
    """A class that can be instantiated and compared."""
 
32
 
 
33
    def __init__(self, message):
 
34
        self._message = message
 
35
 
 
36
    def __eq__(self, other):
 
37
        if other is None:
 
38
            return False
 
39
        return self._message == other._message
 
40
 
 
41
 
 
42
class TestSymbols(TestCase):
 
43
 
 
44
    def test_symbols(self):
 
45
        from bzrlib.transactions import ReadOnlyTransaction
 
46
        from bzrlib.transactions import PassThroughTransaction
 
47
 
 
48
 
30
49
class TestReadOnlyTransaction(TestCase):
31
50
 
32
 
    def test_symbols(self):
33
 
        from bzrlib.transactions import ReadOnlyTransaction
34
 
 
35
 
    def test_construct(self):
36
 
        transactions.ReadOnlyTransaction()
 
51
    def setUp(self):
 
52
        self.transaction = transactions.ReadOnlyTransaction()
 
53
        super(TestReadOnlyTransaction, self).setUp()
37
54
 
38
55
    def test_register_clean(self):
39
 
        transaction = transactions.ReadOnlyTransaction()
40
 
        transaction.register_clean("anobject")
 
56
        self.transaction.register_clean("anobject")
41
57
 
42
58
    def test_register_dirty_raises(self):
43
 
        transaction = transactions.ReadOnlyTransaction()
44
59
        self.assertRaises(errors.ReadOnlyError, 
45
 
                          transaction.register_dirty,"anobject")
 
60
                          self.transaction.register_dirty,"anobject")
46
61
    
47
62
    def test_commit_raises(self):
48
 
        transaction = transactions.ReadOnlyTransaction()
49
 
        self.assertRaises(errors.CommitNotPossible, transaction.commit)
 
63
        self.assertRaises(errors.CommitNotPossible, self.transaction.commit)
50
64
 
51
65
    def test_map(self):
52
 
        transaction = transactions.ReadOnlyTransaction()
53
 
        self.assertNotEqual(None, getattr(transaction, "map", None))
 
66
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
54
67
    
55
68
    def test_add_and_get(self):
56
 
        transaction = transactions.ReadOnlyTransaction()
57
69
        weave = "a weave"
58
 
        transaction.map.add_weave("id", weave)
59
 
        self.assertEqual(weave, transaction.map.find_weave("id"))
 
70
        self.transaction.map.add_weave("id", weave)
 
71
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
60
72
 
61
73
    def test_rollback_returns(self):
62
 
        transaction = transactions.ReadOnlyTransaction()
63
 
        transaction.rollback()
 
74
        self.transaction.rollback()
64
75
 
65
76
    def test_finish_returns(self):
66
 
        transaction = transactions.ReadOnlyTransaction()
67
 
        transaction.finish()
 
77
        self.transaction.finish()
68
78
 
 
79
    def test_zero_size_cache(self):
 
80
        self.transaction.set_cache_size(0)
 
81
        weave = DummyWeave('a weave')
 
82
        self.transaction.map.add_weave("id", weave)
 
83
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
84
        weave = None
 
85
        # add an object, should fall right out if there are no references
 
86
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
87
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
88
        # but if we have a reference it should stick around
 
89
        weave = DummyWeave("another weave")
 
90
        self.transaction.map.add_weave("id", weave)
 
91
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
92
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
93
        del weave
 
94
        # its not a weakref system
 
95
        self.assertEqual(DummyWeave("another weave"),
 
96
                         self.transaction.map.find_weave("id"))
69
97
        
 
98
    def test_small_cache(self):
 
99
        self.transaction.set_cache_size(1)
 
100
        # add an object, should not fall right out if there are no references
 
101
        #sys.getrefcounts(foo)
 
102
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
 
103
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
 
104
        self.assertEqual(DummyWeave("a weave"),
 
105
                         self.transaction.map.find_weave("id"))
 
106
        self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
 
107
        self.transaction.register_clean(self.transaction.map.find_weave("id2"))
 
108
        # currently a fifo
 
109
        self.assertEqual(None, self.transaction.map.find_weave("id"))
 
110
        self.assertEqual(DummyWeave("a weave also"),
 
111
                         self.transaction.map.find_weave("id2"))
 
112
 
 
113
    def test_small_cache_with_references(self):
 
114
        # if we have a reference it should stick around
 
115
        weave = "a weave"
 
116
        weave2 = "another weave"
 
117
        self.transaction.map.add_weave("id", weave)
 
118
        self.transaction.map.add_weave("id2", weave2)
 
119
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
120
        self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
 
121
        weave = None
 
122
        # its not a weakref system
 
123
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))
 
124
 
 
125
    def test_precious_with_zero_size_cache(self):
 
126
        self.transaction.set_cache_size(0)
 
127
        weave = DummyWeave('a weave')
 
128
        self.transaction.map.add_weave("id", weave)
 
129
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
 
130
        weave = None
 
131
        # add an object, should not fall out even with no references.
 
132
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
 
133
                                        precious=True)
 
134
        self.assertEqual(DummyWeave('a weave'),
 
135
                         self.transaction.map.find_weave("id"))
 
136
 
 
137
 
70
138
class TestPassThroughTransaction(TestCase):
71
139
 
72
 
    def test_symbols(self):
73
 
        from bzrlib.transactions import PassThroughTransaction
74
 
 
75
140
    def test_construct(self):
76
141
        transactions.PassThroughTransaction()
77
142
 
104
169
    def test_finish_returns(self):
105
170
        transaction = transactions.PassThroughTransaction()
106
171
        transaction.finish()
 
172
 
 
173
    def test_cache_is_ignored(self):
 
174
        transaction = transactions.PassThroughTransaction()
 
175
        transaction.set_cache_size(100)
 
176
        weave = "a weave"
 
177
        transaction.map.add_weave("id", weave)
 
178
        self.assertEqual(None, transaction.map.find_weave("id"))