Example #1
0
    def test_multi_counters_not_allowed(self, redis):
        counter = RunningCounter(redis, 10, 10, name='test1')

        with pytest.raises(ValueError):
            counter.inc(1, name='test2')

        with pytest.raises(ValueError):
            counter.buckets_counts(name='test2')
Example #2
0
    def test_redis_expirations(self, redis):
        # Test TTL when specifying name in constructor
        name = 'test'
        counter = RunningCounter(redis, 9, 8, name)
        counter.inc(2.3)
        buckets_counts = counter.buckets_counts()
        ttl = redis.ttl(counter._key(name, buckets_counts[0].bucket))
        assert ttl > counter.window

        # Test TTL when specifying name in inc
        name = 'test2'
        counter = RunningCounter(redis, 9, 8, name)
        counter.inc(2.3)
        buckets_counts = counter.buckets_counts(name=name)
        ttl = redis.ttl(counter._key(name, buckets_counts[0].bucket))
        assert ttl > counter.window
Example #3
0
    def test_main(self, redis):
        name = 'test'
        num_buckets = 5
        interval = 5

        # Start counter now
        now = start = datetime.datetime.utcnow().replace(second=0, minute=0)
        with FreezeTime(now):
            counter = RunningCounter(
                redis,
                interval,
                num_buckets,
                name,
            )
            # Add two values to current bucket
            counter.inc(1)
            counter.inc(1.2)

            buckets_counts = counter.buckets_counts()
            bucket = int(time.time()) // interval
            assert buckets_counts == [
                BucketCount(bucket, 2.2),
                BucketCount(bucket - 1, 0),
                BucketCount(bucket - 2, 0),
                BucketCount(bucket - 3, 0),
                BucketCount(bucket - 4, 0),
            ]
            assert counter.count() == 2.2

        # Move half way into window and add value to bucket
        now = start + datetime.timedelta(seconds=int(num_buckets * interval /
                                                     2))
        with FreezeTime(now):
            counter.inc(2.3)
            buckets_counts = counter.buckets_counts()
            new_bucket = int(time.time()) // interval
            assert buckets_counts == [
                BucketCount(new_bucket, 2.3),
                BucketCount(new_bucket - 1, 0),
                BucketCount(new_bucket - 2, 2.2),
                BucketCount(new_bucket - 3, 0),
                BucketCount(new_bucket - 4, 0),
            ]
            assert counter.count() == 4.5

        # Move forward enough to drop first bucket
        now = start + datetime.timedelta(seconds=num_buckets * interval + 1)
        with FreezeTime(now):
            buckets_counts = counter.buckets_counts()
            assert buckets_counts == [
                BucketCount(new_bucket + 3, 0),
                BucketCount(new_bucket + 2, 0),
                BucketCount(new_bucket + 1, 0),
                BucketCount(new_bucket, 2.3),
                BucketCount(new_bucket - 1, 0),
            ]
            assert counter.count() == 2.3

        # Move forward enough to drop all buckets
        now = start + datetime.timedelta(seconds=num_buckets * interval +
                                         int(num_buckets * interval / 2))
        with FreezeTime(now):
            buckets_counts = counter.buckets_counts()
            current_bucket = int(time.time()) // interval
            assert buckets_counts == [
                BucketCount(current_bucket, 0),
                BucketCount(current_bucket - 1, 0),
                BucketCount(current_bucket - 2, 0),
                BucketCount(current_bucket - 3, 0),
                BucketCount(current_bucket - 4, 0),
            ]
            assert counter.count() == 0