Beispiel #1
0
        public async Task CanCompleteSuccessfully()
        {
            CommandBase command = new SuccessCommand();

            CommandResult result = await command.ExecuteAsync(CancellationToken.None);

            Assert.Equal(0, result.ExitCode);
        }
Beispiel #2
0
        public async Task TestTripCircuitOnFailuresAboveThreshold()
        {
            var key = "cmd-B";

            HystrixCommand <bool> cmd1 = new SuccessCommand(key, 0);
            var cb     = cmd1._circuitBreaker;
            var stream = HealthCountsStream.GetInstance(HystrixCommandKeyDefault.AsKey(key), cmd1.CommandOptions);

            Assert.True(WaitForHealthCountToUpdate(key, 1000, output), "Health count stream failed to start");

            // this should start as allowing requests
            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");

            // success with high latency
            _ = await cmd1.ExecuteAsync();

            HystrixCommand <bool> cmd2 = new SuccessCommand(key, 0);

            _ = await cmd2.ExecuteAsync();

            HystrixCommand <bool> cmd3 = new FailureCommand(key, 0);

            _ = await cmd3.ExecuteAsync();

            HystrixCommand <bool> cmd4 = new SuccessCommand(key, 0);

            _ = await cmd4.ExecuteAsync();

            HystrixCommand <bool> cmd5 = new FailureCommand(key, 0);

            _ = await cmd5.ExecuteAsync();

            HystrixCommand <bool> cmd6 = new SuccessCommand(key, 0);

            _ = await cmd6.ExecuteAsync();

            HystrixCommand <bool> cmd7 = new FailureCommand(key, 0);

            _ = await cmd7.ExecuteAsync();

            HystrixCommand <bool> cmd8 = new FailureCommand(key, 0);

            _ = await cmd8.ExecuteAsync();

            // Let window pass, this should trip the circuit as the error percentage is above the threshold
            // Time.Wait(125);
            Assert.True(WaitForHealthCountToUpdate(key, 250, output), "Health count stream failed to update");

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            output.WriteLine("Current CircuitBreaker Status : " + cmd1.Metrics.Healthcounts);
            Assert.False(cb.AllowRequest, "Request allowed when NOT expected!");
            Assert.True(cb.IsOpen, "Circuit is closed when it should be open!");
        }
Beispiel #3
0
        public async Task TestTripCircuitAsync()
        {
            var key = "cmd-A";

            HystrixCommand <bool> cmd1 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd2 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd3 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd4 = new SuccessCommand(key, 0);

            var cb = cmd1._circuitBreaker;

            Assert.True(WaitForHealthCountToUpdate(key, 1000, output), "Health count stream failed to start");

            _ = await cmd1.ExecuteAsync();

            _ = await cmd2.ExecuteAsync();

            _ = await cmd3.ExecuteAsync();

            _ = await cmd4.ExecuteAsync();

            // this should still allow requests as everything has been successful

            // Time.Wait(125);
            Assert.True(WaitForHealthCountToUpdate(key, 250, output), "Health count stream failed to update");

            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");

            // fail
            HystrixCommand <bool> cmd5 = new FailureCommand(key, 0);
            HystrixCommand <bool> cmd6 = new FailureCommand(key, 0);
            HystrixCommand <bool> cmd7 = new FailureCommand(key, 0);
            HystrixCommand <bool> cmd8 = new FailureCommand(key, 0);

            Assert.False(await cmd5.ExecuteAsync());
            Assert.False(await cmd6.ExecuteAsync());
            Assert.False(await cmd7.ExecuteAsync());
            Assert.False(await cmd8.ExecuteAsync());

            // make sure window has passed
            // Time.Wait(125);
            Assert.True(WaitForHealthCountToUpdate(key, 250, output), "Health count stream failed to update");

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            output.WriteLine("Current CircuitBreaker Status : " + cmd1.Metrics.Healthcounts);
            Assert.False(cb.AllowRequest, "Request allowed when NOT expected!");
            Assert.True(cb.IsOpen, "Circuit is closed when it should be open!");
        }
        public async Task TestCircuitDoesNotTripOnFailuresBelowThreshold()
        {
            string key = "cmd-C";

            HystrixCommand <bool> cmd1 = new SuccessCommand(key, 0);
            ICircuitBreaker       cb   = cmd1._circuitBreaker;
            var stream = HealthCountsStream.GetInstance(HystrixCommandKeyDefault.AsKey(key), cmd1.CommandOptions);

            Assert.True(WaitForHealthCountToUpdate(key, 1000, output), "Health count stream failed to start");

            // this should start as allowing requests
            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");

            // success with high latency
            await cmd1.ExecuteAsync();

            HystrixCommand <bool> cmd2 = new SuccessCommand(key, 0);
            await cmd2.ExecuteAsync();

            HystrixCommand <bool> cmd3 = new FailureCommand(key, 0);
            await cmd3.ExecuteAsync();

            HystrixCommand <bool> cmd4 = new SuccessCommand(key, 0);
            await cmd4.ExecuteAsync();

            HystrixCommand <bool> cmd5 = new SuccessCommand(key, 0);
            await cmd5.ExecuteAsync();

            HystrixCommand <bool> cmd6 = new FailureCommand(key, 0);
            await cmd6.ExecuteAsync();

            HystrixCommand <bool> cmd7 = new SuccessCommand(key, 0);
            await cmd7.ExecuteAsync();

            HystrixCommand <bool> cmd8 = new FailureCommand(key, 0);
            await cmd8.ExecuteAsync();

            // Allow window to pass, this should remain closed as the failure threshold is below the percentage limit
            // Time.Wait(125);
            Assert.True(WaitForHealthCountToUpdate(key, 250, output), "Health count stream failed to update");

            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            output.WriteLine("Current CircuitBreaker Status : " + cmd1.Metrics.Healthcounts);
            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");
        }
Beispiel #5
0
        public async Task TestTripCircuitOnFailuresAboveThreshold()
        {
            string key = "cmd-B";

            HystrixCommand <bool>  cmd1 = new SuccessCommand(key, 60);
            IHystrixCircuitBreaker cb   = cmd1._circuitBreaker;

            // this should start as allowing requests
            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");

            // success with high latency
            _ = await cmd1.ExecuteAsync();

            HystrixCommand <bool> cmd2 = new SuccessCommand(key, 1);

            _ = await cmd2.ExecuteAsync();

            HystrixCommand <bool> cmd3 = new FailureCommand(key, 1);

            _ = await cmd3.ExecuteAsync();

            HystrixCommand <bool> cmd4 = new SuccessCommand(key, 1);

            _ = await cmd4.ExecuteAsync();

            HystrixCommand <bool> cmd5 = new FailureCommand(key, 1);

            _ = await cmd5.ExecuteAsync();

            HystrixCommand <bool> cmd6 = new SuccessCommand(key, 1);

            _ = await cmd6.ExecuteAsync();

            HystrixCommand <bool> cmd7 = new FailureCommand(key, 1);

            _ = await cmd7.ExecuteAsync();

            HystrixCommand <bool> cmd8 = new FailureCommand(key, 1);

            _ = await cmd8.ExecuteAsync();

            // this should trip the circuit as the error percentage is above the threshold
            Time.Wait(200);
            Assert.False(cb.AllowRequest, "Request allowed when NOT expected!");
            Assert.True(cb.IsOpen, "Circuit is closed when it should be open!");
        }
Beispiel #6
0
        public async Task TestTripCircuitOnTimeoutsAboveThreshold()
        {
            var key = "cmd-E";

            HystrixCommand <bool> cmd1 = new SuccessCommand(key, 0);
            var cb     = cmd1._circuitBreaker;
            var stream = HealthCountsStream.GetInstance(HystrixCommandKeyDefault.AsKey(key), cmd1.CommandOptions);

            Assert.True(WaitForHealthCountToUpdate(key, 1000, output), "Health count stream failed to start");

            // this should start as allowing requests
            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");

            // success with high latency
            HystrixCommand <bool> cmd2 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd3 = new TimeoutCommand(key);
            HystrixCommand <bool> cmd4 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd5 = new TimeoutCommand(key);
            HystrixCommand <bool> cmd6 = new TimeoutCommand(key);
            HystrixCommand <bool> cmd7 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd8 = new TimeoutCommand(key);
            HystrixCommand <bool> cmd9 = new TimeoutCommand(key);
            var taskList = new List <Task>
            {
                cmd1.ExecuteAsync(),
                cmd2.ExecuteAsync(),
                cmd3.ExecuteAsync(),
                cmd4.ExecuteAsync(),
                cmd5.ExecuteAsync(),
                cmd6.ExecuteAsync(),
                cmd7.ExecuteAsync(),
                cmd8.ExecuteAsync(),
                cmd9.ExecuteAsync(),
            };
            await Task.WhenAll(taskList);

            // Allow window to pass, this should trip the circuit as the error percentage is above the threshold
            // Time.Wait(200);
            Assert.True(WaitForHealthCountToUpdate(key, 250, output), "Health count stream failed to update");

            output.WriteLine("ReqLog" + "@ " + Time.CurrentTimeMillis + " : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            Assert.False(cb.AllowRequest, "Request allowed when NOT expected!");
            Assert.True(cb.IsOpen, "Circuit is closed when it should be open!");
        }
Beispiel #7
0
        public async Task TestCircuitDoesNotTripOnFailuresBelowThreshold()
        {
            string key = "cmd-C";

            HystrixCommand <bool>  cmd1 = new SuccessCommand(key, 60);
            IHystrixCircuitBreaker cb   = cmd1._circuitBreaker;

            // this should start as allowing requests
            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");

            // success with high latency
            await cmd1.ExecuteAsync();

            HystrixCommand <bool> cmd2 = new SuccessCommand(key, 1);
            await cmd2.ExecuteAsync();

            HystrixCommand <bool> cmd3 = new FailureCommand(key, 1);
            await cmd3.ExecuteAsync();

            HystrixCommand <bool> cmd4 = new SuccessCommand(key, 1);
            await cmd4.ExecuteAsync();

            HystrixCommand <bool> cmd5 = new SuccessCommand(key, 1);
            await cmd5.ExecuteAsync();

            HystrixCommand <bool> cmd6 = new FailureCommand(key, 1);
            await cmd6.ExecuteAsync();

            HystrixCommand <bool> cmd7 = new SuccessCommand(key, 1);
            await cmd7.ExecuteAsync();

            HystrixCommand <bool> cmd8 = new FailureCommand(key, 1);
            await cmd8.ExecuteAsync();

            // this should remain closed as the failure threshold is below the percentage limit
            Time.Wait(150);
            output.WriteLine("ReqLog : " + HystrixRequestLog.CurrentRequestLog.GetExecutedCommandsAsString());
            output.WriteLine("Current CircuitBreaker Status : " + cmd1.Metrics.Healthcounts);
            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");
        }
Beispiel #8
0
        public async Task TestTripCircuitAsync()
        {
            string key = "cmd-A";

            HystrixCommand <bool> cmd1 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd2 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd3 = new SuccessCommand(key, 0);
            HystrixCommand <bool> cmd4 = new SuccessCommand(key, 0);

            IHystrixCircuitBreaker cb = cmd1._circuitBreaker;

            _ = await cmd1.ExecuteAsync();

            _ = await cmd2.ExecuteAsync();

            _ = await cmd3.ExecuteAsync();

            _ = await cmd4.ExecuteAsync();

            // this should still allow requests as everything has been successful
            Assert.True(cb.AllowRequest, "Request NOT allowed when expected!");
            Assert.False(cb.IsOpen, "Circuit breaker is open when it should be closed!");

            // fail
            HystrixCommand <bool> cmd5 = new FailureCommand(key, 0);
            HystrixCommand <bool> cmd6 = new FailureCommand(key, 0);
            HystrixCommand <bool> cmd7 = new FailureCommand(key, 0);
            HystrixCommand <bool> cmd8 = new FailureCommand(key, 0);

            Assert.False(await cmd5.ExecuteAsync());
            Assert.False(await cmd6.ExecuteAsync());
            Assert.False(await cmd7.ExecuteAsync());
            Assert.False(await cmd8.ExecuteAsync());

            // everything has failed in the test window so we should return false now
            Time.Wait(300);
            Assert.False(cb.AllowRequest, "Request allowed when NOT expected!");
            Assert.True(cb.IsOpen, "Circuit is closed when it should be open!");
        }