public void TestCollapsedAndResponseFromCacheAgeOutOfCumulativeWindow()
        {
            var key = HystrixCollapserKeyDefault.AsKey("CumulativeCollapser-D");

            stream = CumulativeCollapserEventCounterStream.GetInstance(key, 10, 100);
            stream.StartCachingStreamValuesIfUnstarted();

            var latch = new CountdownEvent(1);

            latchSubscription = stream.Observe().Take(20 + LatchedObserver.STABLE_TICK_COUNT).Subscribe(new LatchedObserver(output, latch));

            for (var i = 0; i < 3; i++)
            {
                Collapser.From(output, key, i).Observe();
                Collapser.From(output, key, i).Observe(); // same arg - should get a response from cache
                Collapser.From(output, key, i).Observe(); // same arg - should get a response from cache
            }

            Assert.True(latch.Wait(10000), "CountdownEvent was not set!");

            Assert.Equal(CollapserEventTypeHelper.Values.Count, stream.Latest.Length);
            var expected = new long[CollapserEventTypeHelper.Values.Count];

            expected[(int)CollapserEventType.BATCH_EXECUTED]      = 1;
            expected[(int)CollapserEventType.ADDED_TO_BATCH]      = 3;
            expected[(int)CollapserEventType.RESPONSE_FROM_CACHE] = 6;
            Assert.Equal(expected, stream.Latest);
        }
        public void TestCollapsedAndResponseFromCacheAgeOutOfCumulativeWindow()
        {
            IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("CumulativeCollapser-D");

            stream = CumulativeCollapserEventCounterStream.GetInstance(key, 10, 100);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(30).Subscribe(new LatchedObserver(latch));

            for (int i = 0; i < 3; i++)
            {
                Collapser.From(output, key, i).Observe();
                Collapser.From(output, key, i).Observe(); // same arg - should get a response from cache
                Collapser.From(output, key, i).Observe(); // same arg - should get a response from cache
            }

            try
            {
                Assert.True(latch.Wait(10000), "CountdownEvent was not set!");
            }
            catch (Exception)
            {
                Assert.True(false, "Interrupted ex");
            }

            Assert.Equal(CollapserEventTypeHelper.Values.Count, stream.Latest.Length);
            long[] expected = new long[CollapserEventTypeHelper.Values.Count];
            expected[(int)CollapserEventType.BATCH_EXECUTED]      = 1;
            expected[(int)CollapserEventType.ADDED_TO_BATCH]      = 3;
            expected[(int)CollapserEventType.RESPONSE_FROM_CACHE] = 6;
            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            Assert.Equal(expected, stream.Latest);
        }
        public void TestEmptyStreamProducesZeros()
        {
            IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("CumulativeCollapser-A");

            stream = CumulativeCollapserEventCounterStream.GetInstance(key, 10, 100);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(10).Subscribe(new LatchedObserver(latch));

            // no writes
            try
            {
                Assert.True(latch.Wait(10000), "CountdownEvent was not set!");
            }
            catch (Exception)
            {
                Assert.False(true, "Interrupted ex");
            }

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            Assert.Equal(CollapserEventTypeHelper.Values.Count, stream.Latest.Length);
            Assert.Equal(0, stream.GetLatest(CollapserEventType.ADDED_TO_BATCH));
            Assert.Equal(0, stream.GetLatest(CollapserEventType.BATCH_EXECUTED));
            Assert.Equal(0, stream.GetLatest(CollapserEventType.RESPONSE_FROM_CACHE));
        }
        public void TestCollapsed()
        {
            IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("CumulativeCollapser-B");

            stream = CumulativeCollapserEventCounterStream.GetInstance(key, 10, 100);
            stream.StartCachingStreamValuesIfUnstarted();

            CountdownEvent latch = new CountdownEvent(1);

            stream.Observe().Take(10).Subscribe(new LatchedObserver(latch));

            for (int i = 0; i < 3; i++)
            {
                CommandStreamTest.Collapser.From(output, key, i).Observe();
            }

            try
            {
                Assert.True(latch.Wait(10000));
            }
            catch (Exception)
            {
                Assert.True(false, "Interrupted ex");
            }

            Assert.Equal(CollapserEventTypeHelper.Values.Count, stream.Latest.Length);
            long[] expected = new long[CollapserEventTypeHelper.Values.Count];
            expected[(int)CollapserEventType.BATCH_EXECUTED] = 1;
            expected[(int)CollapserEventType.ADDED_TO_BATCH] = 3;
            string log = HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString();

            output.WriteLine("ReqLog : " + log);
            Assert.Equal <long[]>(expected, stream.Latest);
        }