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)); } 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 TestCollapsedAndResponseFromCache() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("RollingCollapser-C"); CountdownEvent latch = new CountdownEvent(1); var observer = new LatchedObserver(output, latch); stream = RollingCollapserEventCounterStream.GetInstance(key, 10, 100); latchSubscription = stream.Observe().Subscribe(observer); Assert.True(Time.WaitUntil(() => observer.StreamRunning, 1000), "Stream failed to start"); List <Task> cTasks = new List <Task>(); for (int i = 0; i < 3; i++) { cTasks.Add(Collapser.From(output, key, i).ExecuteAsync()); cTasks.Add(Collapser.From(output, key, i).ExecuteAsync()); // same arg - should get a response from cache cTasks.Add(Collapser.From(output, key, i).ExecuteAsync()); // same arg - should get a response from cache } Task.WaitAll(cTasks.ToArray()); Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 500, output), "Latch took to long to update"); 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; 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)); } 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 <long[]>(expected, stream.Latest); }
public void TestCollapsedAndResponseFromCacheAgeOutOfRollingWindow() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("RollingCollapser-D"); CountdownEvent latch = new CountdownEvent(1); var observer = new LatchedObserver(output, latch); stream = RollingCollapserEventCounterStream.GetInstance(key, 10, 100); latchSubscription = stream.Observe().Take(20 + LatchedObserver.STABLE_TICK_COUNT).Subscribe(observer); Assert.True(Time.WaitUntil(() => observer.StreamRunning, 1000), "Stream failed to start"); List <Task> cTasks = new List <Task>(); for (int i = 0; i < 3; i++) { cTasks.Add(Collapser.From(output, key, i).ExecuteAsync()); cTasks.Add(Collapser.From(output, key, i).ExecuteAsync()); // same arg - should get a response from cache cTasks.Add(Collapser.From(output, key, i).ExecuteAsync()); // same arg - should get a response from cache } Task.WaitAll(cTasks.ToArray()); Assert.True(latch.Wait(10000), "CountdownEvent was not set!"); output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString()); Assert.Equal(CollapserEventTypeHelper.Values.Count, stream.Latest.Length); long[] expected = new long[CollapserEventTypeHelper.Values.Count]; expected[(int)CollapserEventType.BATCH_EXECUTED] = 0; expected[(int)CollapserEventType.ADDED_TO_BATCH] = 0; expected[(int)CollapserEventType.RESPONSE_FROM_CACHE] = 0; Assert.Equal(expected, stream.Latest); }
public void TestCollapsed() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("CumulativeCollapser-B"); CountdownEvent latch = new CountdownEvent(1); var observer = new LatchedObserver(output, latch); stream = CumulativeCollapserEventCounterStream.GetInstance(key, 10, 100); latchSubscription = stream.Observe().Subscribe(observer); List <Task> tasks = new List <Task>(); for (int i = 0; i < 3; i++) { tasks.Add(Collapser.From(output, key, i).ExecuteAsync()); } Task.WaitAll(tasks.ToArray()); Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 500, output), "Latch took to long to update"); 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; 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); latchSubscription = stream.Observe().Take(20 + LatchedObserver.STABLE_TICK_COUNT).Subscribe(new LatchedObserver(output, 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 } Assert.True(latch.Wait(10000), "CountdownEvent was not set!"); 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; Assert.Equal(expected, stream.Latest); }
public void TestEmptyStreamProducesEmptyDistributions() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("Collapser-Batch-Size-A"); CountdownEvent latch = new CountdownEvent(1); var observer = new LatchedObserver(output, latch); stream = RollingCollapserBatchSizeDistributionStream.GetInstance(key, 10, 100); latchSubscription = stream.Observe().Subscribe(observer); Assert.True(Time.WaitUntil(() => observer.StreamRunning, 1000), "Stream failed to start"); Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 500, output), "Latch took to long to update"); Assert.Equal(0, stream.Latest.GetTotalCount()); }
public User GetById(int id) { var collapserOptions = new HystrixCollapserOptions(HystrixCollapserKeyDefault.AsKey("UserCollapser"), RequestCollapserScope.GLOBAL) { TimerDelayInMilliseconds = 5000, MaxRequestsInBatch = 10, RequestCacheEnabled = true }; var collapser = new UserServiceCollapser(collapserOptions, _loggerFactory.CreateLogger <UserServiceCollapser>()); collapser.UserId = id; return(collapser.Execute()); }
public void TestEmptyStreamProducesZeros() { var key = HystrixCollapserKeyDefault.AsKey("RollingCollapser-A"); var latch = new CountdownEvent(1); var observer = new LatchedObserver(output, latch); stream = RollingCollapserEventCounterStream.GetInstance(key, 10, 100); latchSubscription = stream.Observe().Subscribe(observer); Assert.True(Time.WaitUntil(() => observer.StreamRunning, 1000), "Stream failed to start"); Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 500, output), "Latch took to long to update"); 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 TestEmptyStreamProducesEmptyDistributions() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("Collapser-Batch-Size-A"); stream = RollingCollapserBatchSizeDistributionStream.GetInstance(key, 10, 100); stream.StartCachingStreamValuesIfUnstarted(); CountdownEvent latch = new CountdownEvent(1); stream.Observe().Skip(10).Take(10).Subscribe( (distribution) => { output.WriteLine("OnNext @ " + DateTime.Now.Ticks / 10000 + " : " + distribution.GetMean() + "/" + distribution.GetTotalCount() + " " + Thread.CurrentThread.ManagedThreadId); Assert.Equal(0, distribution.GetTotalCount()); }, (e) => { Assert.True(false, e.Message); }, () => { latch.SignalEx(); }); //no writes try { Assert.True(latch.Wait(10000)); } catch (Exception) { Assert.True(false, "Interrupted ex"); } Assert.Equal(0, stream.Latest.GetTotalCount()); }
public void TestCollapsed() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("RollingCollapser-B"); stream = RollingCollapserEventCounterStream.GetInstance(key, 10, 100); stream.StartCachingStreamValuesIfUnstarted(); CountdownEvent latch = new CountdownEvent(1); stream.Observe().Take(10).Subscribe(new LatchedObserver(output, latch)); for (int i = 0; i < 3; i++) { Collapser.From(output, key, i).Observe(); } Assert.True(latch.Wait(10000), "CountdownEvent was not set!"); output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString()); 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; Assert.Equal(expected, stream.Latest); }
public static Collapser From(ITestOutputHelper output, int arg) { return(new Collapser(output, HystrixCollapserKeyDefault.AsKey("Collapser"), arg)); }
public void TestBatches() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("Collapser-Batch-Size-B"); stream = RollingCollapserBatchSizeDistributionStream.GetInstance(key, 10, 100); stream.StartCachingStreamValuesIfUnstarted(); CountdownEvent latch = new CountdownEvent(1); stream.Observe().Take(10).Subscribe( (distribution) => { output.WriteLine("OnNext @ " + (DateTime.Now.Ticks / 10000) + " : " + distribution.GetMean() + "/" + distribution.GetTotalCount() + " " + Thread.CurrentThread.ManagedThreadId); }, (e) => { Assert.True(false, e.Message); }, () => { latch.SignalEx(); }); Collapser.From(output, key, 1).Observe(); Collapser.From(output, key, 2).Observe(); Collapser.From(output, key, 3).Observe(); try { Time.Wait(250); } catch (Exception) { Assert.False(true, "Interrupted ex"); } Collapser.From(output, key, 4).Observe(); try { Time.Wait(250); } catch (Exception) { Assert.False(true, "Interrupted ex"); } Collapser.From(output, key, 5).Observe(); Collapser.From(output, key, 6).Observe(); Collapser.From(output, key, 7).Observe(); Collapser.From(output, key, 8).Observe(); Collapser.From(output, key, 9).Observe(); try { Time.Wait(250); } catch (Exception) { Assert.False(true, "Interrupted ex"); } Collapser.From(output, key, 10).Observe(); Collapser.From(output, key, 11).Observe(); Collapser.From(output, key, 12).Observe(); Assert.True(latch.Wait(10000), "CountdownEvent was not set!"); // should have 4 batches: 3, 1, 5, 3 Assert.Equal(4, stream.Latest.GetTotalCount()); Assert.Equal(3, stream.LatestMean); Assert.Equal(1, stream.GetLatestPercentile(0)); Assert.Equal(5, stream.GetLatestPercentile(100)); }
public void TestBatches() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("Collapser-Batch-Size-B"); CountdownEvent latch = new CountdownEvent(1); var observer = new LatchedObserver(output, latch); stream = RollingCollapserBatchSizeDistributionStream.GetInstance(key, 10, 100); latchSubscription = stream.Observe().Subscribe(observer); Assert.True(Time.WaitUntil(() => observer.StreamRunning, 1000), "Stream failed to start"); // First collapser created with key will be used for all command creations List <Task> tasks = new List <Task>(); var c1 = Collapser.From(output, key, 1); tasks.Add(c1.ExecuteAsync()); var c2 = Collapser.From(output, key, 2); tasks.Add(c2.ExecuteAsync()); var c3 = Collapser.From(output, key, 3); tasks.Add(c3.ExecuteAsync()); Assert.True(Time.WaitUntil(() => c1.CommandCreated, 500), "Batch 1 too long to start"); c1.CommandCreated = false; var c4 = Collapser.From(output, key, 4); tasks.Add(c4.ExecuteAsync()); Assert.True(Time.WaitUntil(() => c1.CommandCreated, 500), "Batch 2 too long to start"); c1.CommandCreated = false; var c5 = Collapser.From(output, key, 5); tasks.Add(c5.ExecuteAsync()); var c6 = Collapser.From(output, key, 6); tasks.Add(c6.ExecuteAsync()); var c7 = Collapser.From(output, key, 7); tasks.Add(c7.ExecuteAsync()); var c8 = Collapser.From(output, key, 8); tasks.Add(c8.ExecuteAsync()); var c9 = Collapser.From(output, key, 9); tasks.Add(c9.ExecuteAsync()); Assert.True(Time.WaitUntil(() => c1.CommandCreated, 500), "Batch 3 too long to start"); c1.CommandCreated = false; var c10 = Collapser.From(output, key, 10); tasks.Add(c10.ExecuteAsync()); var c11 = Collapser.From(output, key, 11); tasks.Add(c11.ExecuteAsync()); var c12 = Collapser.From(output, key, 12); tasks.Add(c12.ExecuteAsync()); Assert.True(Time.WaitUntil(() => c1.CommandCreated, 500), "Batch 4 too long to start"); Task.WaitAll(tasks.ToArray()); Assert.True(WaitForLatchedObserverToUpdate(observer, 1, 500, output), "Latch took to long to update"); // should have 4 batches: 3, 1, 5, 3 Assert.Equal(4, stream.Latest.GetTotalCount()); Assert.Equal(3, stream.LatestMean); Assert.Equal(1, stream.GetLatestPercentile(0)); Assert.Equal(5, stream.GetLatestPercentile(100)); }
public void TestBatchesAgeOut() { IHystrixCollapserKey key = HystrixCollapserKeyDefault.AsKey("Collapser-Batch-Size-B"); CountdownEvent latch = new CountdownEvent(1); var observer = new LatchedObserver(output, latch); stream = RollingCollapserBatchSizeDistributionStream.GetInstance(key, 10, 100); latchSubscription = stream.Observe().Take(20 + LatchedObserver.STABLE_TICK_COUNT).Subscribe(observer); Assert.True(Time.WaitUntil(() => observer.StreamRunning, 1000), "Stream failed to start"); // First collapser created with key will be used for all command creations List <Task> tasks = new List <Task>(); var c1 = Collapser.From(output, key, 1); tasks.Add(c1.ExecuteAsync()); var c2 = Collapser.From(output, key, 2); tasks.Add(c2.ExecuteAsync()); var c3 = Collapser.From(output, key, 3); tasks.Add(c3.ExecuteAsync()); Assert.True(Time.WaitUntil(() => c1.CommandCreated, 500), "Batch 1 too long to start"); c1.CommandCreated = false; var c4 = Collapser.From(output, key, 4); tasks.Add(c4.ExecuteAsync()); Assert.True(Time.WaitUntil(() => c1.CommandCreated, 500), "Batch 2 too long to start"); c1.CommandCreated = false; var c5 = Collapser.From(output, key, 5); tasks.Add(c5.ExecuteAsync()); var c6 = Collapser.From(output, key, 6); tasks.Add(c6.ExecuteAsync()); var c7 = Collapser.From(output, key, 7); tasks.Add(c7.ExecuteAsync()); var c8 = Collapser.From(output, key, 8); tasks.Add(c8.ExecuteAsync()); var c9 = Collapser.From(output, key, 9); tasks.Add(c9.ExecuteAsync()); Assert.True(Time.WaitUntil(() => c1.CommandCreated, 500), "Batch 3 too long to start"); c1.CommandCreated = false; var c10 = Collapser.From(output, key, 10); tasks.Add(c10.ExecuteAsync()); var c11 = Collapser.From(output, key, 11); tasks.Add(c11.ExecuteAsync()); var c12 = Collapser.From(output, key, 12); tasks.Add(c12.ExecuteAsync()); Assert.True(Time.WaitUntil(() => c1.CommandCreated, 500), "Batch 4 too long to start"); Task.WaitAll(tasks.ToArray()); Assert.True(latch.Wait(10000), "CountdownEvent was not set!"); Assert.Equal(0, stream.Latest.GetTotalCount()); Assert.Equal(0, stream.LatestMean); }