def counter_event(event):
    _time = MockedTime()
    counter = RollingNumber(200, 10, _time=_time)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # We start out with 0 sum
    assert counter.rolling_sum(event) == 0

    # Increment
    counter.increment(event)

    # We shoud have 1 bucket
    assert counter.buckets.size == 1

    # The count should be 1
    assert counter.buckets.last().adder(event).sum() == 1
    assert counter.rolling_sum(event) == 1

    # Sleep to get to a new bucket
    _time.increment(counter.buckets_size_in_milliseconds() * 3)

    # Incremenet again in latest bucket
    counter.increment(event)

    # We should have 4 buckets
    assert counter.buckets.size == 4

    # The count of the last bucket
    assert counter.buckets.last().adder(event).sum() == 1

    # The total count
    assert counter.rolling_sum(event) == 2
Esempio n. 2
0
def counter_event(event):
    time = MockedTime()
    counter = RollingNumber(time, 200, 10)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # We start out with 0 sum
    assert counter.rolling_sum(event) == 0

    # Increment
    counter.increment(event)

    # We shoud have 1 bucket
    assert counter.buckets.size == 1

    # The count should be 1
    assert counter.buckets.last().adder(event).sum() == 1
    assert counter.rolling_sum(event) == 1

    # Sleep to get to a new bucket
    time.increment(counter.buckets_size_in_milliseconds() * 3)

    # Incremenet again in latest bucket
    counter.increment(event)

    # We should have 4 buckets
    assert counter.buckets.size == 4

    # The count of the last bucket
    assert counter.buckets.last().adder(event).sum() == 1

    # The total count
    assert counter.rolling_sum(event) == 2
def test_cumulative_counter_after_rolling():
    _time = MockedTime()
    counter = RollingNumber(20, 2, _time=_time)
    event = RollingNumberEvent.SUCCESS

    assert counter.cumulative_sum(event) == 0

    # Iterate over 20 buckets on a queue sized for 2
    for i in range(20):
        counter.increment(event)
        _time.increment(counter.buckets_size_in_milliseconds())

        assert len(counter.values(event)) == 2

        counter.value_of_latest_bucket(event)

    # Cumulative count should be 20 (for the number of loops above) regardless
    # of buckets rolling
    assert counter.cumulative_sum(event) == 20
Esempio n. 4
0
def test_cumulative_counter_after_rolling():
    time = MockedTime()
    counter = RollingNumber(time, 20, 2)
    event = RollingNumberEvent.SUCCESS

    assert counter.cumulative_sum(event) == 0

    # Iterate over 20 buckets on a queue sized for 2
    for i in range(20):
        counter.increment(event)
        time.increment(counter.buckets_size_in_milliseconds())

        assert len(counter.values(event)) == 2

        counter.value_of_latest_bucket(event)

    # Cumulative count should be 20 (for the number of loops above) regardless
    # of buckets rolling
    assert counter.cumulative_sum(event) == 20
Esempio n. 5
0
def test_empty_buckets_fill_in():
    time = MockedTime()
    counter = RollingNumber(time, 200, 10)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Add 1
    counter.increment(RollingNumberEvent.SUCCESS)

    # Confirm we have 1 bucket
    assert counter.buckets.size == 1

    # Wait past 3 bucket time periods (the 1st bucket then 2 empty ones)
    time.increment(counter.buckets_size_in_milliseconds() * 3)

    # Add another
    counter.increment(RollingNumberEvent.SUCCESS)

    # We should have 4 (1 + 2 empty + 1 new one) buckets
    assert counter.buckets.size == 4
def test_empty_buckets_fill_in():
    _time = MockedTime()
    counter = RollingNumber(200, 10, _time=_time)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Add 1
    counter.increment(RollingNumberEvent.SUCCESS)

    # Confirm we have 1 bucket
    assert counter.buckets.size == 1

    # Wait past 3 bucket time periods (the 1st bucket then 2 empty ones)
    _time.increment(counter.buckets_size_in_milliseconds() * 3)

    # Add another
    counter.increment(RollingNumberEvent.SUCCESS)

    # We should have 4 (1 + 2 empty + 1 new one) buckets
    assert counter.buckets.size == 4
Esempio n. 7
0
def test_cumulative_counter_after_rolling_and_reset2():
    time = MockedTime()
    counter = RollingNumber(time, 20, 2)
    event = RollingNumberEvent.SUCCESS

    assert counter.cumulative_sum(event) == 0

    counter.increment(event)
    counter.increment(event)
    counter.increment(event)

    # Iterate over 20 buckets on a queue sized for 2
    for i in range(20):
        time.increment(counter.buckets_size_in_milliseconds())

        # simulate a reset occurring every once in a while
        # so we ensure the absolute sum is handling it okay
        if i == 5 or i == 15:
            counter.reset()

    # No increments during the loop, just some before and after
    counter.increment(event)
    counter.increment(event)

    # Cumulative count should be 5 regardless of buckets rolling
    assert counter.cumulative_sum(event) == 5
def test_cumulative_counter_after_rolling_and_reset2():
    _time = MockedTime()
    counter = RollingNumber(20, 2, _time=_time)
    event = RollingNumberEvent.SUCCESS

    assert counter.cumulative_sum(event) == 0

    counter.increment(event)
    counter.increment(event)
    counter.increment(event)

    # Iterate over 20 buckets on a queue sized for 2
    for i in range(20):
        _time.increment(counter.buckets_size_in_milliseconds())

        # simulate a reset occurring every once in a while
        # so we ensure the absolute sum is handling it okay
        if i == 5 or i == 15:
            counter.reset()

    # No increments during the loop, just some before and after
    counter.increment(event)
    counter.increment(event)

    # Cumulative count should be 5 regardless of buckets rolling
    assert counter.cumulative_sum(event) == 5
def test_reset_buckets():
    _time = MockedTime()
    counter = RollingNumber(200, 10, _time=_time)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Add 1
    counter.increment(RollingNumberEvent.SUCCESS)

    # Confirm we have 1 bucket
    assert counter.buckets.size == 1

    # Confirm we still have 1 bucket
    assert counter.buckets.size == 1

    # Add 1
    counter.increment(RollingNumberEvent.SUCCESS)

    # We should now have a single bucket with no values in it instead of 2 or
    # more buckets
    assert counter.buckets.size == 1
Esempio n. 10
0
def test_reset_buckets():
    time = MockedTime()
    counter = RollingNumber(time, 200, 10)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Add 1
    counter.increment(RollingNumberEvent.SUCCESS)

    # Confirm we have 1 bucket
    assert counter.buckets.size == 1

    # Confirm we still have 1 bucket
    assert counter.buckets.size == 1

    # Add 1
    counter.increment(RollingNumberEvent.SUCCESS)

    # We should now have a single bucket with no values in it instead of 2 or
    # more buckets
    assert counter.buckets.size == 1
Esempio n. 11
0
def test_cumulative_counter_after_rolling_and_reset():
    _time = MockedTime()
    counter = RollingNumber(20, 2, _time=_time)
    event = RollingNumberEvent.SUCCESS

    assert counter.cumulative_sum(event) == 0

    # Iterate over 20 buckets on a queue sized for 2
    for i in range(20):
        counter.increment(event)
        _time.increment(counter.buckets_size_in_milliseconds())

        assert len(counter.values(event)) == 2

        counter.value_of_latest_bucket(event)

        # simulate a reset occurring every once in a while
        # so we ensure the absolute sum is handling it okay
        if i == 5 or i == 15:
            counter.reset()

    # Cumulative count should be 20 (for the number of loops above) regardless
    # of buckets rolling
    assert counter.cumulative_sum(event) == 20
Esempio n. 12
0
def test_create_buckets():
    time = MockedTime()
    counter = RollingNumber(time, 200, 10)

    # confirm the initial settings
    assert counter.milliseconds == 200
    assert counter.bucket_numbers == 10
    assert counter.buckets_size_in_milliseconds() == 20

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # add a success in each interval which should result in all 10 buckets
    # being created with 1 success in each
    for r in range(counter.bucket_numbers):
        counter.increment(RollingNumberEvent.SUCCESS)
        time.increment(counter.buckets_size_in_milliseconds())

    # confirm we have all 10 buckets
    assert counter.buckets.size == 10

    # add 1 more and we should still only have 10 buckets since that's the max
    counter.increment(RollingNumberEvent.SUCCESS)
    assert counter.buckets.size == 10
Esempio n. 13
0
def test_cumulative_counter_after_rolling_and_reset():
    time = MockedTime()
    counter = RollingNumber(time, 20, 2)
    event = RollingNumberEvent.SUCCESS

    assert counter.cumulative_sum(event) == 0

    # Iterate over 20 buckets on a queue sized for 2
    for i in range(20):
        counter.increment(event)
        time.increment(counter.buckets_size_in_milliseconds())

        assert len(counter.values(event)) == 2

        counter.value_of_latest_bucket(event)

        # simulate a reset occurring every once in a while
        # so we ensure the absolute sum is handling it okay
        if i == 5 or i == 15:
            counter.reset()

    # Cumulative count should be 20 (for the number of loops above) regardless
    # of buckets rolling
    assert counter.cumulative_sum(event) == 20
Esempio n. 14
0
def test_create_buckets():
    _time = MockedTime()
    counter = RollingNumber(200, 10, _time=_time)

    # confirm the initial settings
    assert counter.milliseconds == 200
    assert counter.bucket_numbers == 10
    assert counter.buckets_size_in_milliseconds() == 20

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # add a success in each interval which should result in all 10 buckets
    # being created with 1 success in each
    for r in range(counter.bucket_numbers):
        counter.increment(RollingNumberEvent.SUCCESS)
        _time.increment(counter.buckets_size_in_milliseconds())

    # confirm we have all 10 buckets
    assert counter.buckets.size == 10

    # add 1 more and we should still only have 10 buckets since that's the max
    counter.increment(RollingNumberEvent.SUCCESS)
    assert counter.buckets.size == 10
Esempio n. 15
0
def test_counter_retrieval_refreshes_buckets():
    _time = MockedTime()
    counter = RollingNumber(200, 10, _time=_time)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)

    # Sleep to get to a new bucketV
    _time.increment(counter.buckets_size_in_milliseconds() * 3)

    # We should have 1 bucket since nothing has triggered the update of
    # buckets in the elapsed time
    assert counter.buckets.size == 1

    # The total counts
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 4
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 2

    # We should have 4 buckets as the counter should have triggered
    # the buckets being created to fill in time
    assert counter.buckets.size == 4

    # Wait until window passes
    _time.increment(counter.milliseconds)

    # The total counts should all be 0 (and the buckets cleared by the get,
    #not only increment)
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 0
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 0

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)

    # The total count should now include only the last bucket after a reset
    # since the window passed
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 1
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 0
Esempio n. 16
0
def test_counter_retrieval_refreshes_buckets():
    time = MockedTime()
    counter = RollingNumber(time, 200, 10)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)

    # Sleep to get to a new bucketV
    time.increment(counter.buckets_size_in_milliseconds() * 3)

    # We should have 1 bucket since nothing has triggered the update of
    # buckets in the elapsed time
    assert counter.buckets.size == 1

    # The total counts
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 4
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 2

    # We should have 4 buckets as the counter should have triggered
    # the buckets being created to fill in time
    assert counter.buckets.size == 4

    # Wait until window passes
    time.increment(counter.milliseconds)

    # The total counts should all be 0 (and the buckets cleared by the get,
    #not only increment)
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 0
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 0

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)

    # The total count should now include only the last bucket after a reset
    # since the window passed
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 1
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 0
def test_increment_in_single_bucket():
    time = MockedTime()
    counter = RollingNumber(time, 200, 10)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.TIMEOUT)

    # Confirm we have 1 bucket
    assert counter.buckets.size == 1

    # The count should match
    assert counter.buckets.last().adder(RollingNumberEvent.SUCCESS).sum() == 4
    assert counter.buckets.last().adder(RollingNumberEvent.FAILURE).sum() == 2
    assert counter.buckets.last().adder(RollingNumberEvent.TIMEOUT).sum() == 1
Esempio n. 18
0
def test_cumulative_counter_after_rolling_and_reset3():
    time = MockedTime()
    counter = RollingNumber(time, 20, 2)
    event = RollingNumberEvent.SUCCESS

    assert counter.cumulative_sum(event) == 0

    counter.increment(event)
    counter.increment(event)
    counter.increment(event)

    # Iterate over 20 buckets on a queue sized for 2
    for i in range(20):
        time.increment(counter.buckets_size_in_milliseconds())

    # Since we are rolling over the buckets it should reset naturally

    # No increments during the loop, just some before and after
    counter.increment(event)
    counter.increment(event)

    # Cumulative count should be 5 regardless of buckets rolling
    assert counter.cumulative_sum(event) == 5
Esempio n. 19
0
def test_cumulative_counter_after_rolling_and_reset3():
    _time = MockedTime()
    counter = RollingNumber(20, 2, _time=_time)
    event = RollingNumberEvent.SUCCESS

    assert counter.cumulative_sum(event) == 0

    counter.increment(event)
    counter.increment(event)
    counter.increment(event)

    # Iterate over 20 buckets on a queue sized for 2
    for i in range(20):
        _time.increment(counter.buckets_size_in_milliseconds())

    # Since we are rolling over the buckets it should reset naturally

    # No increments during the loop, just some before and after
    counter.increment(event)
    counter.increment(event)

    # Cumulative count should be 5 regardless of buckets rolling
    assert counter.cumulative_sum(event) == 5
Esempio n. 20
0
def test_increment_in_multiple_buckets():
    time = MockedTime()
    counter = RollingNumber(time, 200, 10)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.TIMEOUT)
    counter.increment(RollingNumberEvent.TIMEOUT)
    counter.increment(RollingNumberEvent.SHORT_CIRCUITED)

    # Sleep to get to a new bucket
    time.increment(counter.buckets_size_in_milliseconds() * 3)

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.TIMEOUT)
    counter.increment(RollingNumberEvent.SHORT_CIRCUITED)

    # Confirm we have 4 bucket
    assert counter.buckets.size == 4

    # The count of the last buckets
    assert counter.buckets.last().adder(RollingNumberEvent.SUCCESS).sum() == 2
    assert counter.buckets.last().adder(RollingNumberEvent.FAILURE).sum() == 3
    assert counter.buckets.last().adder(RollingNumberEvent.TIMEOUT).sum() == 1
    assert counter.buckets.last().adder(RollingNumberEvent.SHORT_CIRCUITED).sum() == 1

    # The total count
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 6
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 5
    assert counter.rolling_sum(RollingNumberEvent.TIMEOUT) == 3
    assert counter.rolling_sum(RollingNumberEvent.SHORT_CIRCUITED) == 2

    # Wait until window passes
    time.increment(counter.milliseconds)

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)

    # The total count should now include only the last bucket after a reset
    # since the window passed
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 1
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 0
    assert counter.rolling_sum(RollingNumberEvent.TIMEOUT) == 0
    assert counter.rolling_sum(RollingNumberEvent.SHORT_CIRCUITED) == 0
Esempio n. 21
0
def test_increment_in_multiple_buckets():
    _time = MockedTime()
    counter = RollingNumber(200, 10, _time=_time)

    # We start out with 0 buckets in the queue
    assert counter.buckets.size == 0

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.TIMEOUT)
    counter.increment(RollingNumberEvent.TIMEOUT)
    counter.increment(RollingNumberEvent.SHORT_CIRCUITED)

    # Sleep to get to a new bucket
    _time.increment(counter.buckets_size_in_milliseconds() * 3)

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.SUCCESS)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.FAILURE)
    counter.increment(RollingNumberEvent.TIMEOUT)
    counter.increment(RollingNumberEvent.SHORT_CIRCUITED)

    # Confirm we have 4 bucket
    assert counter.buckets.size == 4

    # The count of the last buckets
    assert counter.buckets.last().adder(RollingNumberEvent.SUCCESS).sum() == 2
    assert counter.buckets.last().adder(RollingNumberEvent.FAILURE).sum() == 3
    assert counter.buckets.last().adder(RollingNumberEvent.TIMEOUT).sum() == 1
    assert counter.buckets.last().adder(
        RollingNumberEvent.SHORT_CIRCUITED).sum() == 1

    # The total count
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 6
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 5
    assert counter.rolling_sum(RollingNumberEvent.TIMEOUT) == 3
    assert counter.rolling_sum(RollingNumberEvent.SHORT_CIRCUITED) == 2

    # Wait until window passes
    _time.increment(counter.milliseconds)

    # Increment
    counter.increment(RollingNumberEvent.SUCCESS)

    # The total count should now include only the last bucket after a reset
    # since the window passed
    assert counter.rolling_sum(RollingNumberEvent.SUCCESS) == 1
    assert counter.rolling_sum(RollingNumberEvent.FAILURE) == 0
    assert counter.rolling_sum(RollingNumberEvent.TIMEOUT) == 0
    assert counter.rolling_sum(RollingNumberEvent.SHORT_CIRCUITED) == 0