~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# Copyright (C) 2005 by Canonical Ltd
#   Authors: Robert Collins <robert.collins@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Tests for the behaviour of the Transaction concept in bzr."""

# import system imports here
import os
import sys

#import bzrlib specific imports here
import bzrlib.errors as errors
from bzrlib.tests import TestCase, TestCaseInTempDir
import bzrlib.transactions as transactions


class DummyWeave(object):
    """A class that can be instantiated and compared."""

    def __init__(self, message):
        self._message = message
        self.finished = False

    def __eq__(self, other):
        if other is None:
            return False
        return self._message == other._message

    def transaction_finished(self):
        self.finished = True


class TestSymbols(TestCase):

    def test_public_symbols(self):
        from bzrlib.transactions import ReadOnlyTransaction
        from bzrlib.transactions import PassThroughTransaction


class TestReadOnlyTransaction(TestCase):

    def setUp(self):
        self.transaction = transactions.ReadOnlyTransaction()
        super(TestReadOnlyTransaction, self).setUp()

    def test_register_clean(self):
        self.transaction.register_clean("anobject")

    def test_register_dirty_raises(self):
        self.assertRaises(errors.ReadOnlyError, 
                          self.transaction.register_dirty,"anobject")
    
    def test_map(self):
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
    
    def test_add_and_get(self):
        weave = "a weave"
        self.transaction.map.add_weave("id", weave)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))

    def test_finish_returns(self):
        self.transaction.finish()

    def test_finish_does_not_tell_versioned_file_finished(self):
        # read only transactions never write, so theres no
        # need to inform versioned files about finishing
        weave = DummyWeave('a weave')
        self.transaction.finish()
        self.assertFalse(weave.finished)

    def test_zero_size_cache(self):
        self.transaction.set_cache_size(0)
        weave = DummyWeave('a weave')
        self.transaction.map.add_weave("id", weave)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        weave = None
        # add an object, should fall right out if there are no references
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
        self.assertEqual(None, self.transaction.map.find_weave("id"))
        # but if we have a reference it should stick around
        weave = DummyWeave("another weave")
        self.transaction.map.add_weave("id", weave)
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        del weave
        # its not a weakref system
        self.assertEqual(DummyWeave("another weave"),
                         self.transaction.map.find_weave("id"))
        
    def test_small_cache(self):
        self.transaction.set_cache_size(1)
        # add an object, should not fall right out if there are no references
        #sys.getrefcounts(foo)
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
        self.assertEqual(DummyWeave("a weave"),
                         self.transaction.map.find_weave("id"))
        self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
        self.transaction.register_clean(self.transaction.map.find_weave("id2"))
        # currently a fifo
        self.assertEqual(None, self.transaction.map.find_weave("id"))
        self.assertEqual(DummyWeave("a weave also"),
                         self.transaction.map.find_weave("id2"))

    def test_small_cache_with_references(self):
        # if we have a reference it should stick around
        weave = "a weave"
        weave2 = "another weave"
        self.transaction.map.add_weave("id", weave)
        self.transaction.map.add_weave("id2", weave2)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
        weave = None
        # its not a weakref system
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))

    def test_precious_with_zero_size_cache(self):
        self.transaction.set_cache_size(0)
        weave = DummyWeave('a weave')
        self.transaction.map.add_weave("id", weave)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        weave = None
        # add an object, should not fall out even with no references.
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
                                        precious=True)
        self.assertEqual(DummyWeave('a weave'),
                         self.transaction.map.find_weave("id"))

    def test_precious_revision_history(self):
        """Disabled test until revision-history is a real object."""
        print "Disabled: test_precious_revision_history"
        return
        self.transaction.set_cache_size(0)
        history = []
        self.transaction.map.add_revision_history(history)
        self.assertEqual(history, self.transaction.map.find_revision_history())
        history = None
        # add an object, should not fall out even with no references.
        self.transaction.register_clean(
            self.transaction.map.find_revision_history(), precious=True)
        self.assertEqual([], self.transaction.map.find_revision_history())

    def test_writable(self):
        self.assertFalse(self.transaction.writeable())


class TestPassThroughTransaction(TestCase):

    def test_construct(self):
        transactions.PassThroughTransaction()

    def test_register_clean(self):
        transaction = transactions.PassThroughTransaction()
        transaction.register_clean("anobject")
    
    def test_register_dirty(self):
        transaction = transactions.PassThroughTransaction()
        transaction.register_dirty("anobject")
    
    def test_map(self):
        transaction = transactions.PassThroughTransaction()
        self.assertNotEqual(None, getattr(transaction, "map", None))
    
    def test_add_and_get(self):
        transaction = transactions.PassThroughTransaction()
        weave = "a weave"
        transaction.map.add_weave("id", weave)
        self.assertEqual(None, transaction.map.find_weave("id"))
        
    def test_finish_returns(self):
        transaction = transactions.PassThroughTransaction()
        transaction.finish()

    def test_finish_tells_versioned_file_finished(self):
        # pass through transactions allow writes so they
        # need to inform versioned files about finishing
        weave = DummyWeave('a weave')
        transaction = transactions.PassThroughTransaction()
        transaction.register_dirty(weave)
        transaction.finish()
        self.assertTrue(weave.finished)

    def test_cache_is_ignored(self):
        transaction = transactions.PassThroughTransaction()
        transaction.set_cache_size(100)
        weave = "a weave"
        transaction.map.add_weave("id", weave)
        self.assertEqual(None, transaction.map.find_weave("id"))

    def test_writable(self):
        transaction = transactions.PassThroughTransaction()
        self.assertTrue(transaction.writeable())
        

class TestWriteTransaction(TestCase):

    def setUp(self):
        self.transaction = transactions.WriteTransaction()
        super(TestWriteTransaction, self).setUp()

    def test_register_clean(self):
        self.transaction.register_clean("anobject")
    
    def test_register_dirty(self):
        self.transaction.register_dirty("anobject")
    
    def test_map(self):
        self.assertNotEqual(None, getattr(self.transaction, "map", None))
    
    def test_add_and_get(self):
        weave = "a weave"
        self.transaction.map.add_weave("id", weave)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        
    def test_finish_returns(self):
        self.transaction.finish()

    def test_finish_tells_versioned_file_finished(self):
        # write transactions allow writes so they
        # need to inform versioned files about finishing
        weave = DummyWeave('a weave')
        self.transaction.register_dirty(weave)
        self.transaction.finish()
        self.assertTrue(weave.finished)

    def test_zero_size_cache(self):
        self.transaction.set_cache_size(0)
        # add an object, should fall right out if there are no references
        weave = DummyWeave('a weave')
        self.transaction.map.add_weave("id", weave)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        weave = None
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
        self.assertEqual(None, self.transaction.map.find_weave("id"))
        # but if we have a reference to a clean object it should stick around
        weave = DummyWeave("another weave")
        self.transaction.map.add_weave("id", weave)
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        del weave
        # its not a weakref system
        self.assertEqual(DummyWeave("another weave"),
                         self.transaction.map.find_weave("id"))

    def test_zero_size_cache_dirty_objects(self):
        self.transaction.set_cache_size(0)
        # add a dirty object, which should not fall right out.
        weave = DummyWeave('a weave')
        self.transaction.map.add_weave("id", weave)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        weave = None
        self.transaction.register_dirty(self.transaction.map.find_weave("id"))
        self.assertNotEqual(None, self.transaction.map.find_weave("id"))
    
    def test_clean_to_dirty(self):
        # a clean object may become dirty.
        weave = DummyWeave('A weave')
        self.transaction.map.add_weave("id", weave)
        self.transaction.register_clean(weave)
        self.transaction.register_dirty(weave)
        self.assertTrue(self.transaction.is_dirty(weave))
        self.assertFalse(self.transaction.is_clean(weave))

    def test_small_cache(self):
        self.transaction.set_cache_size(1)
        # add an object, should not fall right out if there are no references
        #sys.getrefcounts(foo)
        self.transaction.map.add_weave("id", DummyWeave("a weave"))
        self.transaction.register_clean(self.transaction.map.find_weave("id"))
        self.assertEqual(DummyWeave("a weave"),
                         self.transaction.map.find_weave("id"))
        self.transaction.map.add_weave("id2", DummyWeave("a weave also"))
        self.transaction.register_clean(self.transaction.map.find_weave("id2"))
        # currently a fifo
        self.assertEqual(None, self.transaction.map.find_weave("id"))
        self.assertEqual(DummyWeave("a weave also"),
                         self.transaction.map.find_weave("id2"))

    def test_small_cache_with_references(self):
        # if we have a reference it should stick around
        weave = "a weave"
        weave2 = "another weave"
        self.transaction.map.add_weave("id", weave)
        self.transaction.map.add_weave("id2", weave2)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        self.assertEqual(weave2, self.transaction.map.find_weave("id2"))
        weave = None
        # its not a weakref system
        self.assertEqual("a weave", self.transaction.map.find_weave("id"))

    def test_precious_with_zero_size_cache(self):
        self.transaction.set_cache_size(0)
        weave = DummyWeave('a weave')
        self.transaction.map.add_weave("id", weave)
        self.assertEqual(weave, self.transaction.map.find_weave("id"))
        weave = None
        # add an object, should not fall out even with no references.
        self.transaction.register_clean(self.transaction.map.find_weave("id"),
                                        precious=True)
        self.assertEqual(DummyWeave('a weave'),
                         self.transaction.map.find_weave("id"))

    def test_writable(self):
        self.assertTrue(self.transaction.writeable())