Exemplo n.º 1
0
 def test_empty_counter(self, redis):
     counter = RunningCounter(redis, 1, 1, name='test_empty')
     count = counter.count()
     assert count == 0
Exemplo n.º 2
0
 def test_delete_counter(self, redis):
     counter = RunningCounter(redis, 1, 1, name='name1')
     counter.inc()
     counter.delete()
     assert counter.count() == 0
Exemplo n.º 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