예제 #1
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)
예제 #2
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))
예제 #3
0
 def test_should_ignores_lower_expiration(self):
     wanted_expiration = (
         self.AN_INTERVAL + datetime.timedelta(milliseconds=1))
     options = caches.CheckOptions(flush_interval=self.AN_INTERVAL,
                                   expiration=self.A_LOWER_INTERVAL)
     expect(options.flush_interval).to(equal(self.AN_INTERVAL))
     expect(options.expiration).to(equal(wanted_expiration))
     expect(options.expiration).not_to(equal(self.A_LOWER_INTERVAL))
예제 #4
0
 def test_should_create_with_defaults(self):
     options = caches.CheckOptions()
     expect(options.num_entries).to(equal(
         caches.CheckOptions.DEFAULT_NUM_ENTRIES))
     expect(options.flush_interval).to(equal(
         caches.CheckOptions.DEFAULT_FLUSH_INTERVAL))
     expect(options.expiration).to(equal(
         caches.CheckOptions.DEFAULT_EXPIRATION))
예제 #5
0
 def setUp(self):
     self.timer = _DateTimeTimer()
     self.expiration = datetime.timedelta(seconds=2)
     options = caches.CheckOptions(
         flush_interval=datetime.timedelta(seconds=1),
         expiration=self.expiration)
     self.agg = check_request.Aggregator(
         self.SERVICE_NAME, options, timer=self.timer)
예제 #6
0
    def test_should_create_client_ok(self, check_opts, report_opts):
        # the mocks return fake instances else code using them fails
        check_opts.return_value = caches.CheckOptions()
        report_opts.return_value = caches.ReportOptions()

        # ensure the client is constructed using no args instances of the opts
        expect(client.Loaders.DEFAULT.load(self.SERVICE_NAME)).not_to(be_none)
        check_opts.assert_called_once_with()
        report_opts.assert_called_once_with()
예제 #7
0
    def _assert_called_with_no_args_options(self, check_opts, quota_opts, report_opts):
        # the mocks return fake instances else code using them fails
        check_opts.return_value = caches.CheckOptions()
        report_opts.return_value = caches.ReportOptions()
        quota_opts.return_value = caches.QuotaOptions()

        # ensure the client is constructed using no args instances of the opts
        expect(client.Loaders.ENVIRONMENT.load(self.SERVICE_NAME)).not_to(be_none)
        check_opts.assert_called_once_with()
        quota_opts.assert_called_once_with()
        report_opts.assert_called_once_with()
예제 #8
0
    def test_should_create_client_from_environment_ok(self, check_opts, quota_opts, report_opts):
        check_opts.return_value = caches.CheckOptions()
        report_opts.return_value = caches.ReportOptions()
        quota_opts.return_value = caches.QuotaOptions()

        # ensure the client is constructed using options values from the test JSON
        expect(client.Loaders.ENVIRONMENT.load(self.SERVICE_NAME)).not_to(be_none)
        check_opts.assert_called_once_with(expiration=datetime.timedelta(0, 1),
                                           flush_interval=datetime.timedelta(0, 2),
                                           num_entries=10)
        report_opts.assert_called_once_with(flush_interval=datetime.timedelta(0, 1),
                                            num_entries=10)
예제 #9
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))
예제 #10
0
 def setUp(self):
     # -ve num_entries means no cache is present
     self.agg = check_request.Aggregator(
         self.SERVICE_NAME, caches.CheckOptions(num_entries=-1))
예제 #11
0
 def setUp(self):
     self.timer = _DateTimeTimer()
     self.agg = check_request.Aggregator(self.SERVICE_NAME,
                                         caches.CheckOptions())