summaryrefslogtreecommitdiffstats
path: root/khashmir/test_kstore.py
blob: d2f2b154a6239715a19c96fb74fe3169836904fc (plain)
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
# The contents of this file are subject to the BitTorrent Open Source License
# Version 1.1 (the License).  You may not copy or use this file, in either
# source code or executable form, except in compliance with the License.  You
# may obtain a copy of the License at http://www.bittorrent.com/license/.
#
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
# for the specific language governing rights and limitations under the
# License.

import unittest
from BitTorrent.platform import bttime
from time import sleep

from kstore import KStore
if __name__ =="__main__":
    tests = unittest.defaultTestLoader.loadTestsFromNames(['test_kstore'])
    result = unittest.TextTestRunner().run(tests)


class BasicTests(unittest.TestCase):
    def setUp(self):
        self.k = KStore()
        
    def testNoKeys(self):
        self.assertEqual(self.k.keys(), [])

    def testKey(self):
        self.k['foo'] = 'bar'
        self.assertEqual(self.k.keys(), ['foo'])

    def testKeys(self):
        self.k['foo'] = 'bar'
        self.k['wing'] = 'wang'
        l = self.k.keys()
        l.sort()
        self.assertEqual(l, ['foo', 'wing'])
        
    def testInsert(self):
        self.k['foo'] = 'bar'
        self.assertEqual(self.k['foo'], ['bar'])

    def testInsertTwo(self):
        self.k['foo'] = 'bar'
        self.k['foo'] = 'bing'
        l = self.k['foo']
        l.sort()
        self.assertEqual(l, ['bar', 'bing'])
        
    def testExpire(self):
        self.k['foo'] = 'bar'
        self.k.expire(bttime() - 1)
        l = self.k['foo']
        l.sort()
        self.assertEqual(l, ['bar'])
        self.k['foo'] = 'bing'
        t = bttime()
        self.k.expire(bttime() - 1)
        l = self.k['foo']
        l.sort()
        self.assertEqual(l, ['bar', 'bing'])        
        self.k['foo'] = 'ding'
        self.k['foo'] = 'dang'
        l = self.k['foo']
        l.sort()
        self.assertEqual(l, ['bar', 'bing', 'dang', 'ding'])
        self.k.expire(t)
        l = self.k['foo']
        l.sort()
        self.assertEqual(l, ['dang', 'ding'])
        
    def testDup(self):
        self.k['foo'] = 'bar'
        self.k['foo'] = 'bar'
        self.assertEqual(self.k['foo'], ['bar'])

    def testSample(self):
        for i in xrange(2):
            self.k['foo'] = i
        l = self.k.sample('foo', 5)
        l.sort()
        self.assertEqual(l, [0, 1])

        for i in xrange(10):
            for i in xrange(10):
                self.k['bar'] = i
            l = self.k.sample('bar', 5)
            self.assertEqual(len(l), 5)
            for i in xrange(len(l)):
                self.assert_(l[i] not in l[i+1:])