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 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
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
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_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
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
def test_increment_in_single_bucket(): _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) # 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.rolling_sum(RollingNumberEvent.FAILURE) == 2 assert counter.buckets.last().adder(RollingNumberEvent.TIMEOUT).sum() == 1
def test_empty_sum(): _time = MockedTime() counter = RollingNumber(200, 10, _time=_time) event = RollingNumberEvent.COLLAPSED assert counter.rolling_sum(event) == 0
def test_empty_sum(): time = MockedTime() counter = RollingNumber(time, 200, 10) event = RollingNumberEvent.COLLAPSED assert counter.rolling_sum(event) == 0