Exemple #1
0
    def test_should_return_ttl_cache_if_flush_interval_is_positive(self):
        delta = datetime.timedelta(seconds=1)
        should_be_ttl = [
            lambda timer: caches.create(
                caches.CheckOptions(num_entries=1, flush_interval=delta),
                timer=timer
            ),
            lambda timer: caches.create(
                caches.ReportOptions(num_entries=1, flush_interval=delta),
                timer=timer
            ),
        ]
        for testf in should_be_ttl:
            timer = _DateTimeTimer()
            sync_cache = testf(timer)
            expect(sync_cache).to(be_a(caches.LockedObject))
            with sync_cache as cache:
                expect(cache).to(be_a(caches.DequeOutTTLCache))
                expect(cache.timer()).to(equal(0))
                cache[1] = 1
                expect(set(cache)).to(equal({1}))
                expect(cache.get(1)).to(equal(1))
                timer.tick()
                expect(cache.get(1)).to(equal(1))
                timer.tick()
                expect(cache.get(1)).to(be_none)

            # Is still TTL without the custom timer
            sync_cache = testf(None)
            expect(sync_cache).to(be_a(caches.LockedObject))
            with sync_cache as cache:
                expect(cache).to(be_a(caches.DequeOutTTLCache))
    def test_should_return_ttl_cache_if_flush_interval_is_positive(self):
        delta = datetime.timedelta(seconds=1)
        should_be_ttl = [
            lambda timer: caches.create(
                caches.CheckOptions(num_entries=1, flush_interval=delta),
                timer=timer
            ),
            lambda timer: caches.create(
                caches.ReportOptions(num_entries=1, flush_interval=delta),
                timer=timer
            ),
        ]
        for testf in should_be_ttl:
            timer = _DateTimeTimer()
            sync_cache = testf(timer)
            expect(sync_cache).to(be_a(caches.LockedObject))
            with sync_cache as cache:
                expect(cache).to(be_a(caches.DequeOutTTLCache))
                expect(cache.timer()).to(equal(0))
                cache[1] = 1
                expect(set(cache)).to(equal({1}))
                expect(cache.get(1)).to(equal(1))
                timer.tick()
                expect(cache.get(1)).to(equal(1))
                timer.tick()
                expect(cache.get(1)).to(be_none)

            # Is still TTL without the custom timer
            sync_cache = testf(None)
            expect(sync_cache).to(be_a(caches.LockedObject))
            with sync_cache as cache:
                expect(cache).to(be_a(caches.DequeOutTTLCache))
Exemple #3
0
 def test_should_return_none_if_cache_size_not_positive(self):
     should_be_none = [
         lambda: caches.create(caches.CheckOptions(num_entries=0)),
         lambda: caches.create(caches.CheckOptions(num_entries=-1)),
         lambda: caches.create(caches.ReportOptions(num_entries=0)),
         lambda: caches.create(caches.ReportOptions(num_entries=-1)),
     ]
     for testf in should_be_none:
         expect(testf()).to(be_none)
 def test_should_return_none_if_cache_size_not_positive(self):
     should_be_none = [
         lambda: caches.create(caches.CheckOptions(num_entries=0)),
         lambda: caches.create(caches.CheckOptions(num_entries=-1)),
         lambda: caches.create(caches.ReportOptions(num_entries=0)),
         lambda: caches.create(caches.ReportOptions(num_entries=-1)),
     ]
     for testf in should_be_none:
         expect(testf()).to(be_none)
Exemple #5
0
 def test_should_return_a_lru_cache_if_flush_interval_is_negative(self):
     delta = datetime.timedelta(seconds=-1)
     should_be_ttl = [
         lambda: caches.create(
             caches.CheckOptions(num_entries=1, flush_interval=delta), ),
         lambda: caches.create(
             caches.ReportOptions(num_entries=1, flush_interval=delta)),
     ]
     for testf in should_be_ttl:
         sync_cache = testf()
         expect(sync_cache).to(be_a(caches.LockedObject))
         with sync_cache as cache:
             expect(cache).to(be_a(caches.DequeOutLRUCache))
 def test_should_return_a_lru_cache_if_flush_interval_is_negative(self):
     delta = datetime.timedelta(seconds=-1)
     should_be_ttl = [
         lambda: caches.create(
             caches.CheckOptions(num_entries=1, flush_interval=delta),
         ),
         lambda: caches.create(
             caches.ReportOptions(num_entries=1, flush_interval=delta)),
     ]
     for testf in should_be_ttl:
         sync_cache = testf()
         expect(sync_cache).to(be_a(caches.LockedObject))
         with sync_cache as cache:
             expect(cache).to(be_a(caches.DequeOutLRUCache))
Exemple #7
0
 def test_should_return_none_if_options_is_none(self):
     expect(caches.create(None)).to(be_none)
Exemple #8
0
 def test_should_fail_if_bad_options_are_used(self):
     should_fail = [
         lambda: caches.create(object()),
     ]
     for testf in should_fail:
         expect(testf).to(raise_error(ValueError))
 def test_should_return_none_if_options_is_none(self):
     expect(caches.create(None)).to(be_none)
 def test_should_fail_if_bad_options_are_used(self):
     should_fail = [
         lambda: caches.create(object()),
     ]
     for testf in should_fail:
         expect(testf).to(raise_error(ValueError))