Beispiel #1
0
    def testPersistentLoadSaveHook(self):
        def saveHook(value):
            newValue = dict(value)
            newValue['a'] = value['a'] * 2
            newValue['b'] = value['b'] * 2
            return newValue

        def loadHook(value):
            newValue = dict(value)
            newValue['a'] = value['a'] / 2
            newValue['b'] = value['b'] / 2
            return newValue

        timedDict = TimedPersistentDictionary(TWO_HOURS,
                                              filename=self._filename,
                                              saveHook=saveHook)
        timedDict['key'] = {"a": 2, "b": 4, "C": "aap"}

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertEqual({"a": 4, "b": 8, "C": "aap"}, timedDict2['key'])
        self.assertEqual({"a": 2, "b": 4, "C": "aap"}, timedDict['key'])

        timedDict3 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename,
                                               loadHook=loadHook)
        self.assertEqual({"a": 2, "b": 4, "C": "aap"}, timedDict3['key'])
Beispiel #2
0
    def testPersistent(self):
        self.assertFalse(isfile(self._filename))
        self.timedDict['key'] = 'value'
        self.assertTrue(isfile(self._filename))
        self.assertEqual(dict, type(self.timedDict._dictionary))

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertEqual('value', timedDict2['key'])
        self.assertEqual(dict, type(timedDict2._dictionary))

        self.assertEqual(self.timedDict.id(), timedDict2.id())
Beispiel #3
0
    def testPersistentDel(self):
        self.timedDict['key'] = "yet another value"
        self.assertTrue("key" in self.timedDict)

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertTrue("key" in timedDict2)

        del timedDict2['key']
        timedDict3 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertFalse("key" in timedDict3)
Beispiel #4
0
    def testPersistentIfLRU(self):
        self.timedDict = TimedPersistentDictionary(TWO_HOURS,
                                                   filename=self._filename,
                                                   lruMaxSize=10)
        self.assertFalse(isfile(self._filename))
        self.timedDict['key'] = 'value'
        self.assertTrue(isfile(self._filename))
        self.assertEqual(lrucache, type(self.timedDict._dictionary))

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertEqual('value', timedDict2['key'])
        self.assertEqual(lrucache, type(timedDict2._dictionary))
Beispiel #5
0
 def testPersistentPop(self):
     self.timedDict['key'] = "value"
     self.timedDict.pop("key")
     self.assertFalse("key" in self.timedDict)
     timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                            filename=self._filename)
     self.assertFalse("key" in timedDict2)
Beispiel #6
0
 def testPersistentClear(self):
     self.timedDict['key'] = 'another value'
     self.assertTrue("key" in self.timedDict)
     self.timedDict.clear()
     self.assertFalse("key" in self.timedDict)
     timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                            filename=self._filename)
     self.assertFalse("key" in timedDict2)
Beispiel #7
0
    def testPersistentGetExpired(self):
        self.timedDict['key'] = "yet another value"
        self.timedDict._now = lambda: time() + TWO_HOURS + TWO_HOURS
        self.assertEqual(None, self.timedDict.get("key"))
        self.assertFalse("key" in self.timedDict)

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertFalse("key" in timedDict2)
Beispiel #8
0
    def testPersistentPurge(self):
        self.timedDict['key'] = "yet another value"
        self.timedDict._now = lambda: time() + TWO_HOURS + TWO_HOURS
        self.assertEqual("yet another value", self.timedDict.peek("key"))
        self.timedDict.purge()
        self.assertFalse("key" in self.timedDict)

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertFalse("key" in timedDict2)
Beispiel #9
0
 def testExpiredLeastRecentlyUsedGracefullyDealtWith(self):
     timedDict = TimedPersistentDictionary(TWO_HOURS,
                                           filename=join(
                                               self.tempdir, "TheDict"),
                                           lruMaxSize=2)
     timedDict['key1'] = 'five'
     timedDict['key2'] = 'six'
     timedDict['key3'] = 'seven'
     self.assertEqual(set(['key2', 'key3']), set(timedDict.keys()),
                      set(timedDict.keys()))
     self.assertEqual(3, len(timedDict._times))
     self.assertEqual(3, len(timedDict._expirationOrder))
     timedDict.purge()
     self.assertEqual(2, len(timedDict._times))
     self.assertEqual(2, len(timedDict._expirationOrder))
     timedDict._now = lambda: time() + TWO_HOURS
     timedDict.purge()
     self.assertEqual([], list(timedDict.keys()))
     self.assertEqual(0, len(timedDict._times))
     self.assertEqual(0, len(timedDict._expirationOrder))
Beispiel #10
0
 def testLruMaxSize(self):
     timedDict = TimedPersistentDictionary(TWO_HOURS,
                                           filename=join(
                                               self.tempdir, "TheDict"),
                                           lruMaxSize=2)
     self.assertEqual(0, timedDict.size())
     timedDict['key'] = 'five'
     self.assertEqual(1, timedDict.size())
     timedDict['key1'] = 'six'
     self.assertEqual(2, timedDict.size())
     self.assertEqual('five', timedDict['key'])
     timedDict['key2'] = 'seven'
     self.assertEqual(2, timedDict.size())
     self.assertEqual(set(['key', 'key2']), set(timedDict.keys()))
Beispiel #11
0
 def setUp(self):
     SeecrTestCase.setUp(self)
     self._filename = join(self.tempdir, "TheDict")
     self.timedDict = TimedPersistentDictionary(TWO_HOURS,
                                                filename=self._filename)
Beispiel #12
0
class TimedPersistentDictionaryTest(SeecrTestCase):
    def setUp(self):
        SeecrTestCase.setUp(self)
        self._filename = join(self.tempdir, "TheDict")
        self.timedDict = TimedPersistentDictionary(TWO_HOURS,
                                                   filename=self._filename)

    def testPersistent(self):
        self.assertFalse(isfile(self._filename))
        self.timedDict['key'] = 'value'
        self.assertTrue(isfile(self._filename))
        self.assertEqual(dict, type(self.timedDict._dictionary))

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertEqual('value', timedDict2['key'])
        self.assertEqual(dict, type(timedDict2._dictionary))

        self.assertEqual(self.timedDict.id(), timedDict2.id())

    def testPersistentPop(self):
        self.timedDict['key'] = "value"
        self.timedDict.pop("key")
        self.assertFalse("key" in self.timedDict)
        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertFalse("key" in timedDict2)

    def testPersistentClear(self):
        self.timedDict['key'] = 'another value'
        self.assertTrue("key" in self.timedDict)
        self.timedDict.clear()
        self.assertFalse("key" in self.timedDict)
        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertFalse("key" in timedDict2)

    def testPersistentPurge(self):
        self.timedDict['key'] = "yet another value"
        self.timedDict._now = lambda: time() + TWO_HOURS + TWO_HOURS
        self.assertEqual("yet another value", self.timedDict.peek("key"))
        self.timedDict.purge()
        self.assertFalse("key" in self.timedDict)

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertFalse("key" in timedDict2)

    def testPersistentGetExpired(self):
        self.timedDict['key'] = "yet another value"
        self.timedDict._now = lambda: time() + TWO_HOURS + TWO_HOURS
        self.assertEqual(None, self.timedDict.get("key"))
        self.assertFalse("key" in self.timedDict)

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertFalse("key" in timedDict2)

    def testPersistentDel(self):
        self.timedDict['key'] = "yet another value"
        self.assertTrue("key" in self.timedDict)

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertTrue("key" in timedDict2)

        del timedDict2['key']
        timedDict3 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertFalse("key" in timedDict3)

    def testPersistentIfLRU(self):
        self.timedDict = TimedPersistentDictionary(TWO_HOURS,
                                                   filename=self._filename,
                                                   lruMaxSize=10)
        self.assertFalse(isfile(self._filename))
        self.timedDict['key'] = 'value'
        self.assertTrue(isfile(self._filename))
        self.assertEqual(lrucache, type(self.timedDict._dictionary))

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertEqual('value', timedDict2['key'])
        self.assertEqual(lrucache, type(timedDict2._dictionary))

    def testPersistentLoadSaveHook(self):
        def saveHook(value):
            newValue = dict(value)
            newValue['a'] = value['a'] * 2
            newValue['b'] = value['b'] * 2
            return newValue

        def loadHook(value):
            newValue = dict(value)
            newValue['a'] = value['a'] / 2
            newValue['b'] = value['b'] / 2
            return newValue

        timedDict = TimedPersistentDictionary(TWO_HOURS,
                                              filename=self._filename,
                                              saveHook=saveHook)
        timedDict['key'] = {"a": 2, "b": 4, "C": "aap"}

        timedDict2 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename)
        self.assertEqual({"a": 4, "b": 8, "C": "aap"}, timedDict2['key'])
        self.assertEqual({"a": 2, "b": 4, "C": "aap"}, timedDict['key'])

        timedDict3 = TimedPersistentDictionary(TWO_HOURS,
                                               filename=self._filename,
                                               loadHook=loadHook)
        self.assertEqual({"a": 2, "b": 4, "C": "aap"}, timedDict3['key'])

    def testBasicGetAndPut(self):
        self.timedDict['key'] = 'value'
        self.assertEqual('value', self.timedDict['key'])

        self.timedDict['key'] = 'otherValue'
        self.assertEqual('otherValue', self.timedDict['key'])
        self.timedDict['key'] = 5
        self.assertEqual(5, self.timedDict['key'])
        self.timedDict['key'] = 5.0
        self.assertEqual(5.0, self.timedDict['key'])
        self.timedDict['key'] = ['someMutable', 5]
        self.assertEqual(['someMutable', 5], self.timedDict['key'])

        self.assertEqual(['someMutable', 5], self.timedDict.get('key'))

        self.assertRaises(TypeError, self.timedDict.__setitem__, [], 'value')
        self.assertRaises(KeyError, self.timedDict.__getitem__, 'iamnothere')
        self.assertEqual(None, self.timedDict.get('iamnothere'))
        self.assertEqual('MARK', self.timedDict.get('iamnothere', 'MARK'))

    def testBasicContains(self):
        self.timedDict[5] = 'five'
        self.timedDict['six'] = 6
        self.assertTrue(5 in self.timedDict)
        self.assertTrue('six' in self.timedDict)
        self.assertFalse(42 in self.timedDict)
        self.assertFalse(None in self.timedDict)

        self.assertTrue(5 in self.timedDict)
        self.assertTrue('six' in self.timedDict)
        self.assertFalse(42 in self.timedDict)
        self.assertFalse(None in self.timedDict)

    def testBasicDeletion(self):
        self.timedDict['key'] = 'five'

        try:
            del self.timedDict['key']
        except:
            self.fail("This shouldn't happen.")
        self.assertRaises(KeyError, self.timedDict.__delitem__, 'idontexist')

    def testExpirationOrderIsKeptInternally(self):
        self.timedDict[3] = SomeObject(1)
        self.timedDict[1] = SomeObject(10)
        self.timedDict[2] = SomeObject(20)

        self.assertEqual([3, 1, 2], self.timedDict._expirationOrder)

        self.timedDict[1] = SomeObject(23)
        self.assertEqual([3, 2, 1], self.timedDict._expirationOrder)

        self.timedDict[0] = SomeObject(23.01)
        self.assertEqual([3, 2, 1, 0], self.timedDict._expirationOrder)

        del self.timedDict[2]
        self.assertEqual([3, 1, 0], self.timedDict._expirationOrder)

    def testGetTime(self):
        self.timedDict[1] = SomeObject('id1')
        with stderr_replaced():
            self.assertTrue(time() - self.timedDict.getTime(1) < 2.0)

    def testTouch(self):
        self.timedDict[1] = SomeObject('id1')
        self.timedDict[2] = SomeObject('id2')

        self.assertEqual([1, 2], self.timedDict._expirationOrder)
        self.timedDict.touch(1)
        self.assertEqual([2, 1], self.timedDict._expirationOrder)

    def testPurge(self):
        self.timedDict[1] = SomeObject('id1')
        self.timedDict[2] = SomeObject('id2')
        self.timedDict._now = lambda: time() + ONE_HOUR
        self.assertEqual([1, 2], self.timedDict._expirationOrder)

        self.timedDict[3] = SomeObject('id3')
        self.timedDict.touch(2)
        self.timedDict._now = lambda: time() + TWO_HOURS
        self.timedDict.purge()
        self.assertEqual([3, 2], self.timedDict._expirationOrder)
        self.timedDict._now = lambda: time() + TWO_HOURS + TWO_HOURS
        self.timedDict.purge()
        self.assertEqual([], self.timedDict._expirationOrder)

    def testPurgeOnSetItem(self):
        self.timedDict[1] = SomeObject('id1')
        self.timedDict._now = lambda: time() + TWO_HOURS
        self.timedDict[2] = SomeObject('id2')

        self.assertEqual([2], self.timedDict._expirationOrder)

    def testDeleteExpiredOnGetItem(self):
        self.timedDict[1] = SomeObject('id1')
        self.timedDict._now = lambda: time() + TWO_HOURS

        self.assertRaises(KeyError, self.timedDict.__getitem__, 1)
        self.assertEqual([], self.timedDict._expirationOrder)

    def testExpiredOnInShouldReturnDefaultOnGetWithoutAnException(self):
        setTime = time()
        self.timedDict._now = lambda: setTime
        self.timedDict[1] = SomeObject('id1')

        self.timedDict._now = lambda: setTime + TWO_HOURS
        try:
            1 in self.timedDict
        except KeyError:
            self.fail("Key shouldn't have expired yet.")
        except:
            self.fail("This should not happen.")

        self.timedDict._now = lambda: setTime + TWO_HOURS + 0.000001

        self.assertEqual(None, self.timedDict.get(1))
        self.assertEqual('a default', self.timedDict.get(1, 'a default'))
        self.assertEqual([], self.timedDict._expirationOrder)

    def testPop(self):
        setTime = time()
        self.timedDict[1] = "Now you see me, now you don't."
        self.assertEqual("Now you see me, now you don't.",
                         self.timedDict.pop(1))
        self.assertRaises(KeyError, lambda: self.timedDict.pop(1))
        self.assertEqual("default", self.timedDict.pop(1, 'default'))
        self.assertEqual(None, self.timedDict.pop(1, None))
        self.assertEqual("default", self.timedDict.pop(1, default='default'))
        self.assertEqual(None, self.timedDict.pop(1, default=None))
        self.timedDict[1] = "Now you see me, now you don't."
        setTime = time()
        self.timedDict._now = lambda: setTime + TWO_HOURS + 0.000001
        self.assertRaises(KeyError, lambda: self.timedDict.pop(1))

    def testPeek(self):
        self.timedDict[1] = "Now you see me, now you don't."
        self.timedDict._now = lambda: time() + TWO_HOURS

        self.assertEqual("Now you see me, now you don't.",
                         self.timedDict.peek(1))
        self.assertRaises(KeyError, self.timedDict.__getitem__, 1)

    def testClear(self):
        self.timedDict['key'] = 'five'
        self.timedDict.clear()
        self.assertRaises(KeyError, lambda: self.timedDict["key"])

    def testUpdateTimeout(self):
        self.timedDict['key'] = 'five'
        self.timedDict._now = lambda: time() + TWO_HOURS
        self.timedDict.setTimeout(TWO_HOURS + 1)
        self.assertEqual("five", self.timedDict["key"])

    def testSize(self):
        self.assertEqual(0, self.timedDict.size())
        self.timedDict['key'] = 'five'
        self.assertEqual(1, self.timedDict.size())
        self.timedDict['key'] = 'six'
        self.assertEqual(1, self.timedDict.size())
        self.timedDict['key2'] = 'six'
        self.assertEqual(2, self.timedDict.size())
        self.timedDict._now = lambda: time() + TWO_HOURS
        self.assertEqual(0, self.timedDict.size())

    def testLruMaxSize(self):
        timedDict = TimedPersistentDictionary(TWO_HOURS,
                                              filename=join(
                                                  self.tempdir, "TheDict"),
                                              lruMaxSize=2)
        self.assertEqual(0, timedDict.size())
        timedDict['key'] = 'five'
        self.assertEqual(1, timedDict.size())
        timedDict['key1'] = 'six'
        self.assertEqual(2, timedDict.size())
        self.assertEqual('five', timedDict['key'])
        timedDict['key2'] = 'seven'
        self.assertEqual(2, timedDict.size())
        self.assertEqual(set(['key', 'key2']), set(timedDict.keys()))

    def testExpiredLeastRecentlyUsedGracefullyDealtWith(self):
        timedDict = TimedPersistentDictionary(TWO_HOURS,
                                              filename=join(
                                                  self.tempdir, "TheDict"),
                                              lruMaxSize=2)
        timedDict['key1'] = 'five'
        timedDict['key2'] = 'six'
        timedDict['key3'] = 'seven'
        self.assertEqual(set(['key2', 'key3']), set(timedDict.keys()),
                         set(timedDict.keys()))
        self.assertEqual(3, len(timedDict._times))
        self.assertEqual(3, len(timedDict._expirationOrder))
        timedDict.purge()
        self.assertEqual(2, len(timedDict._times))
        self.assertEqual(2, len(timedDict._expirationOrder))
        timedDict._now = lambda: time() + TWO_HOURS
        timedDict.purge()
        self.assertEqual([], list(timedDict.keys()))
        self.assertEqual(0, len(timedDict._times))
        self.assertEqual(0, len(timedDict._expirationOrder))
Beispiel #13
0
def CookiePersistentStore(timeout, filename, saveHook=None, loadHook=None, *args, **kwargs):
    return CookieStore(
        TimedPersistentDictionary(timeout, filename=filename, saveHook=saveHook, loadHook=loadHook),
        timeout,
        *args, **kwargs)