~storm/storm/trunk

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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
from unittest import defaultTestLoader

from storm.properties import Int
from storm.info import get_obj_info
from storm.cache import Cache, GenerationalCache
from storm.tests.helper import TestHelper


class StubObjectInfo(object):

    def __init__(self, id):
        self.id = id
        self.hashed = False

    def get_obj(self):
        return str(self.id)

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self.id)

    def __hash__(self):
        self.hashed = True
        return self.id

    def __lt__(self, other):
        return self.id < other.id


class StubClass(object):

    __storm_table__ = "stub_class"

    id = Int(primary=True)


class BaseCacheTest(TestHelper):

    Cache = Cache

    def setUp(self):
        super(BaseCacheTest, self).setUp()
        self.obj_infos = [StubObjectInfo(i) for i in range(10)]
        for i in range(len(self.obj_infos)):
            setattr(self, "obj%d" % (i+1), self.obj_infos[i])

    def clear_hashed(self):
        for obj_info in self.obj_infos:
            obj_info.hashed = False

    def test_initially_empty(self):
        cache = self.Cache()
        self.assertEqual(cache.get_cached(), [])

    def test_add(self):
        cache = self.Cache(5)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)
        self.assertEqual(sorted(cache.get_cached()),
                         [self.obj1, self.obj2, self.obj3])

    def test_adding_similar_obj_infos(self):
        """If __eq__ is broken, this fails."""
        obj_info1 = get_obj_info(StubClass())
        obj_info2 = get_obj_info(StubClass())
        cache = self.Cache(5)
        cache.add(obj_info1)
        cache.add(obj_info2)
        cache.add(obj_info2)
        cache.add(obj_info1)
        self.assertEqual(sorted([hash(obj_info)
                                 for obj_info in cache.get_cached()]),
                         sorted([hash(obj_info1), hash(obj_info2)]))

    def test_remove(self):
        cache = self.Cache(5)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)
        cache.remove(self.obj2)
        self.assertEqual(sorted(cache.get_cached()),
                         [self.obj1, self.obj3])

    def test_add_existing(self):
        cache = self.Cache(5)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)
        cache.add(self.obj2)
        self.assertEqual(sorted(cache.get_cached()),
                         [self.obj1, self.obj2, self.obj3])

    def test_add_with_size_zero(self):
        """Cache is disabled entirely on add() if size is 0."""
        cache = self.Cache(0)
        cache.add(self.obj1)
        # Ensure that we don't even check if obj_info is in the
        # cache, by testing if it was hashed.  Hopefully, that means
        # we got a faster path.
        self.assertEqual(self.obj1.hashed, False)

    def test_remove_with_size_zero(self):
        """Cache is disabled entirely on remove() if size is 0."""
        cache = self.Cache(0)
        cache.remove(self.obj1)

    def test_clear(self):
        """The clear method empties the cache."""
        cache = self.Cache(5)
        for obj_info in self.obj_infos:
            cache.add(obj_info)
        cache.clear()
        self.assertEqual(cache.get_cached(), [])

        # Just an additional check ensuring that any additional structures
        # which may be used were cleaned properly as well.
        for obj_info in self.obj_infos:
            self.assertEqual(cache.remove(obj_info), False)

    def test_set_zero_size(self):
        """
        Setting a cache's size to zero clears the cache.
        """
        cache = self.Cache()
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.set_size(0)
        self.assertEqual(cache.get_cached(), [])

    def test_fit_size(self):
        """
        A cache of size n can hold at least n objects.
        """
        size = 10
        cache = self.Cache(size)
        for value in range(size):
            cache.add(StubObjectInfo(value))
        self.assertEqual(len(cache.get_cached()), size)


class CacheTest(BaseCacheTest):

    def test_size_and_fifo_behaviour(self):
        cache = Cache(5)
        for obj_info in self.obj_infos:
            cache.add(obj_info)
        self.assertEqual([obj_info.id for obj_info in cache.get_cached()],
                         [9, 8, 7, 6, 5])

    def test_reduce_max_size_to_zero(self):
        """When setting the size to zero, there's an optimization."""
        cache = Cache(5)
        obj_info = self.obj_infos[0]
        cache.add(obj_info)
        obj_info.hashed = False
        cache.set_size(0)
        self.assertEqual(cache.get_cached(), [])
        # Ensure that we don't even check if obj_info is in the
        # cache, by testing if it was hashed.  Hopefully, that means
        # we got a faster path.
        self.assertEqual(obj_info.hashed, False)

    def test_reduce_max_size(self):
        cache = Cache(5)
        for obj_info in self.obj_infos:
            cache.add(obj_info)
        cache.set_size(3)
        self.assertEqual([obj_info.id for obj_info in cache.get_cached()],
                         [9, 8, 7])

        # Adding items past the new maximum size should drop older ones.
        for obj_info in self.obj_infos[:2]:
            cache.add(obj_info)
        self.assertEqual([obj_info.id for obj_info in cache.get_cached()],
                         [1, 0, 9])

    def test_increase_max_size(self):
        cache = Cache(5)
        for obj_info in self.obj_infos:
            cache.add(obj_info)
        cache.set_size(10)
        self.assertEqual([obj_info.id for obj_info in cache.get_cached()],
                         [9, 8, 7, 6, 5])

        # Adding items past the new maximum size should drop older ones.
        for obj_info in self.obj_infos[:6]:
            cache.add(obj_info)
        self.assertEqual([obj_info.id for obj_info in cache.get_cached()],
                         [5, 4, 3, 2, 1, 0, 9, 8, 7, 6])


class TestGenerationalCache(BaseCacheTest):

    Cache = GenerationalCache

    def setUp(self):
        super(TestGenerationalCache, self).setUp()
        self.obj1 = StubObjectInfo(1)
        self.obj2 = StubObjectInfo(2)
        self.obj3 = StubObjectInfo(3)
        self.obj4 = StubObjectInfo(4)

    def test_initially_empty(self):
        cache = GenerationalCache()
        self.assertEqual(cache.get_cached(), [])

    def test_cache_one_object(self):
        cache = GenerationalCache()
        cache.add(self.obj1)
        self.assertEqual(cache.get_cached(), [self.obj1])

    def test_cache_multiple_objects(self):
        cache = GenerationalCache()
        cache.add(self.obj1)
        cache.add(self.obj2)
        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj2])

    def test_clear_cache(self):
        cache = GenerationalCache()
        cache.add(self.obj1)
        cache.clear()
        self.assertEqual(cache.get_cached(), [])

    def test_clear_cache_clears_the_second_generation(self):
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.clear()
        self.assertEqual(cache.get_cached(), [])

    def test_remove_object(self):
        cache = GenerationalCache()
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        present = cache.remove(self.obj2)
        self.assertTrue(present)
        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj3])

    def test_remove_nothing(self):
        cache = GenerationalCache()
        cache.add(self.obj1)

        present = cache.remove(self.obj2)
        self.assertFalse(present)
        self.assertEqual(cache.get_cached(), [self.obj1])

    def test_size_limit(self):
        """
        A cache will never hold more than twice its size in objects.  The
        generational system is what prevents it from holding exactly the
        requested number of objects.
        """
        size = 10
        cache = GenerationalCache(size)
        for value in range(5 * size):
            cache.add(StubObjectInfo(value))
        self.assertEqual(len(cache.get_cached()), size * 2)

    def test_set_size_smaller_than_current_size(self):
        """
        Setting the size to a smaller size than the number of objects
        currently cached will drop some of the extra content.  Note that
        because of the generation system, it can actually hold two times
        the size requested in edge cases.
        """
        cache = GenerationalCache(150)
        for i in range(250):
            cache.add(StubObjectInfo(i))
        cache.set_size(100)
        cached = cache.get_cached()
        self.assertEqual(len(cached), 100)
        for obj_info in cache.get_cached():
            self.assertTrue(obj_info.id >= 100)

    def test_set_size_larger_than_current_size(self):
        """
        Setting the cache size to something more than the number of
        objects in the cache does not affect its current contents,
        and will merge any elements from the second generation into
        the first one.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1) # new=[1]    old=[]
        cache.add(self.obj2) # new=[2]    old=[1]
        cache.set_size(2)    # new=[1, 2] old=[]
        cache.add(self.obj3) # new=[3]    old=[1, 2]
        self.assertEqual(sorted(cache.get_cached()),
                         [self.obj1, self.obj2, self.obj3])

    def test_set_size_limit(self):
        """
        Setting the size limits the cache's size just like passing an
        initial size would.
        """
        size = 10
        cache = GenerationalCache(size * 100)
        cache.set_size(size)
        for value in range(size * 10):
            cache.add(StubObjectInfo(value))
        self.assertEqual(len(cache.get_cached()), size * 2)

    def test_two_generations(self):
        """
        Inserting more objects than the cache's size causes the cache
        to contain two generations, each holding up to <size> objects.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)

        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj2])

    def test_three_generations(self):
        """
        If more than 2*<size> objects come along, only 2*<size>
        objects are retained.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])

    def test_generational_overlap(self):
        """
        An object that is both in the primary and the secondary
        generation is listed only once in the cache's contents.
        """
        cache = GenerationalCache(2)
        cache.add(self.obj1) # new=[1]    old=[]
        cache.add(self.obj2) # new=[1, 2] old=[]
        cache.add(self.obj3) # new=[3]    old=[1, 2]
        cache.add(self.obj1) # new=[3, 1] old=[1, 2]

        self.assertEqual(sorted(cache.get_cached()),
                         [self.obj1, self.obj2, self.obj3])

    def test_remove_from_overlap(self):
        """
        Removing an object from the cache removes it from both its
        primary and secondary generations.
        """
        cache = GenerationalCache(2)
        cache.add(self.obj1) # new=[1]    old=[]
        cache.add(self.obj2) # new=[1, 2] old=[]
        cache.add(self.obj3) # new=[3]    old=[1, 2]
        cache.add(self.obj1) # new=[3, 1] old=[1, 2]

        present = cache.remove(self.obj1)
        self.assertTrue(present)
        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])

    def test_evict_oldest(self):
        """The "oldest" object is the first to be evicted."""
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)
        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj2, self.obj3])

    def test_evict_LRU(self):
        """
        Actually, it's not the oldest but the LRU object that is first
        to be evicted.  Re-adding the oldest object makes it not be
        the LRU.
        """
        cache = GenerationalCache(1)
        cache.add(self.obj1)
        cache.add(self.obj2)

        # This "refreshes" the oldest object in the cache.
        cache.add(self.obj1)

        cache.add(self.obj3)

        self.assertEqual(sorted(cache.get_cached()), [self.obj1, self.obj3])


def test_suite():
    return defaultTestLoader.loadTestsFromName(__name__)