GetBuckets() private méthode

Returns all bucket in reverse order (most recent first)
private GetBuckets ( long startWindowTime ) : IEnumerable
startWindowTime long
Résultat IEnumerable
        public void testCreatesBuckets()
        {
            var time = new MockedClock();

            var counter = new RollingNumber(time, 200, 10);
            // confirm the initial settings
            Assert.Equal(200, counter.TimeInMs);
            Assert.Equal(10, counter.NumberOfBuckets);
            Assert.Equal(20, counter.BucketSizeInMs);


            // add a Success in each interval which should result in all 10 buckets being created with 1 Success in each
            for (int i = 0; i < counter.NumberOfBuckets; i++)
            {
                counter.Increment(RollingNumberEvent.SUCCESS);
                time.Increment(counter.BucketSizeInMs);
            }

            // confirm we have all 10 buckets
            var buckets = counter.GetBuckets().ToArray();
            Assert.Equal(10, buckets.Length);

            // add 1 more and we should still only have 10 buckets since that's the max
            counter.Increment(RollingNumberEvent.SUCCESS);
            buckets = counter.GetBuckets().ToArray();
            Assert.Equal(10, buckets.Length);
        }
        public void testResetBuckets()
        {
            MockedClock time = new MockedClock();
            RollingNumber counter = new RollingNumber(time, 200, 10);

  
            // add 1
            counter.Increment(RollingNumberEvent.SUCCESS);

            // confirm we have 1 bucket
            var buckets = counter.GetBuckets().ToArray();
            Assert.Equal(1, buckets.Length);

            // confirm we still have 1 bucket
            Assert.Equal(1, buckets.Length);

            // add 1
            counter.Increment(RollingNumberEvent.SUCCESS);

            // we should now have a single bucket with no values in it instead of 2 or more buckets
            buckets = counter.GetBuckets().ToArray();
            Assert.Equal(1, buckets.Length);
        }
        public void testIncrementInSingleBucket()
        {
            MockedClock time = new MockedClock();

            RollingNumber counter = new RollingNumber(time, 200, 10);

            // 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);

            // we should have 1 bucket
            var buckets = counter.GetBuckets().ToArray();
            Assert.Equal(1, buckets.Length);

            // the count should be 4
            Assert.Equal(4, counter.GetCurrentBucket().GetAdder(RollingNumberEvent.SUCCESS));
            Assert.Equal(2, counter.GetCurrentBucket().GetAdder(RollingNumberEvent.FAILURE));
            Assert.Equal(1, counter.GetCurrentBucket().GetAdder(RollingNumberEvent.TIMEOUT));
        }
        public void testTimeout()
        {
            MockedClock time = new MockedClock();

            RollingNumber counter = new RollingNumber(time, 200, 10);

            // increment
            counter.Increment(RollingNumberEvent.TIMEOUT);

            var buckets = counter.GetBuckets().ToArray();
            // we should have 1 bucket
            Assert.Equal(1, buckets.Count());

            // the count should be 1
            Assert.Equal(1, buckets.First().GetAdder(RollingNumberEvent.TIMEOUT));
            Assert.Equal(1, counter.GetRollingSum(RollingNumberEvent.TIMEOUT));

            // sleep to get to a new bucket
            time.Increment(counter.BucketSizeInMs * 3);

            // incremenet again in latest bucket
            counter.Increment(RollingNumberEvent.TIMEOUT);

            // we should have 2 buckets
            buckets = counter.GetBuckets().ToArray();
            Assert.Equal(2, buckets.Length);

            // the counts of the last bucket
            Assert.Equal(1, buckets.First().GetAdder(RollingNumberEvent.TIMEOUT));

            // the total counts
            Assert.Equal(2, counter.GetRollingSum(RollingNumberEvent.TIMEOUT));
        }
        public void testUpdateMax2()
        {
            MockedClock time = new MockedClock();

            RollingNumber counter = new RollingNumber(time, 200, 10);

            // increment
            counter.UpdateRollingMax(RollingNumberEvent.THREAD_MAX_ACTIVE, 10);
            counter.UpdateRollingMax(RollingNumberEvent.THREAD_MAX_ACTIVE, 30);
            counter.UpdateRollingMax(RollingNumberEvent.THREAD_MAX_ACTIVE, 20);

            // we should have 1 bucket
            var buckets = counter.GetBuckets().ToArray();
            Assert.Equal(1, buckets.Length);

            // the count should be 30
            Assert.Equal(30, counter.buckets.First().GetMaxUpdater(RollingNumberEvent.THREAD_MAX_ACTIVE));
            Assert.Equal(30, counter.GetRollingMaxValue(RollingNumberEvent.THREAD_MAX_ACTIVE));

            // sleep to get to a new bucket
            time.Increment(counter.BucketSizeInMs * 3);

            counter.UpdateRollingMax(RollingNumberEvent.THREAD_MAX_ACTIVE, 30);
            counter.UpdateRollingMax(RollingNumberEvent.THREAD_MAX_ACTIVE, 30);
            counter.UpdateRollingMax(RollingNumberEvent.THREAD_MAX_ACTIVE, 50);

            // we should have 2 buckets
            buckets = counter.GetBuckets().ToArray();
            Assert.Equal(2, buckets.Length);
        
            // the count
            Assert.Equal(50, buckets.First().GetMaxUpdater(RollingNumberEvent.THREAD_MAX_ACTIVE));
            Assert.Equal(50, counter.GetValueOfLatestBucket(RollingNumberEvent.THREAD_MAX_ACTIVE));

            // values per bucket
            var values = counter.GetValues(RollingNumberEvent.THREAD_MAX_ACTIVE).ToArray();
            Assert.Equal(30, values[1]); // oldest bucket
            Assert.Equal(50, values[0]); // latest bucket
        }
        public void testCounterRetrievalRefreshesBuckets()
        {
            MockedClock time = new MockedClock();

            RollingNumber counter = new RollingNumber(time, 200, 10);

            // 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 bucket
            time.Increment(counter.BucketSizeInMs * 3);

            var buckets = counter.GetBuckets().ToArray();
            // we should have 1 bucket since nothing has triggered the update of buckets in the elapsed time
            Assert.Equal(1, buckets.Length);

            // the total counts
            Assert.Equal(4, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(2, counter.GetRollingSum(RollingNumberEvent.FAILURE));

            // we should have 2 buckets as the counter 'gets' should have triggered the buckets being created to fill in time
            buckets = counter.GetBuckets().ToArray();
            Assert.Equal(2, buckets.Length);

            // wait until window passes
            time.Increment(counter.TimeInMs);

            // the total counts should all be 0 (and the buckets cleared by the get, not only increment)
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.FAILURE));

            // increment
            counter.Increment(RollingNumberEvent.SUCCESS);

            // the total counts should now include only the last bucket after a reset since the window passed
            Assert.Equal(1, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.FAILURE));
        }
        public void testIncrementInMultipleBuckets()
        {
            MockedClock time = new MockedClock();

            RollingNumber counter = new RollingNumber(time, 200, 10);

            // 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.BucketSizeInMs * 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);

            // we should have 2 buckets
            var buckets = counter.GetBuckets().ToArray();
            Assert.Equal(2, buckets.Length);

            // the counts of the last bucket
            Assert.Equal(2, buckets.First().GetAdder(RollingNumberEvent.SUCCESS));
            Assert.Equal(3, buckets.First().GetAdder(RollingNumberEvent.FAILURE));
            Assert.Equal(1, buckets.First().GetAdder(RollingNumberEvent.TIMEOUT));
            Assert.Equal(1, buckets.First().GetAdder(RollingNumberEvent.SHORT_CIRCUITED));

            // the total counts
            Assert.Equal(6, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(5, counter.GetRollingSum(RollingNumberEvent.FAILURE));
            Assert.Equal(3, counter.GetRollingSum(RollingNumberEvent.TIMEOUT));
            Assert.Equal(2, counter.GetRollingSum(RollingNumberEvent.SHORT_CIRCUITED));

            // wait until window passes
            time.Increment(counter.TimeInMs*10);

            // increment
            counter.Increment(RollingNumberEvent.SUCCESS);

            // the total counts should now include only the last bucket after a reset since the window passed
            Assert.Equal(1, counter.GetRollingSum(RollingNumberEvent.SUCCESS));
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.FAILURE));
            Assert.Equal(0, counter.GetRollingSum(RollingNumberEvent.TIMEOUT));
        }