|
| 1 | +using Bump.Api.Services; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace Bump.Api.Tests; |
| 5 | + |
| 6 | +public sealed class OutagePolicyTests |
| 7 | +{ |
| 8 | + private static List<string> History(params string[] statuses) => statuses.ToList(); |
| 9 | + |
| 10 | + [Fact] |
| 11 | + public void TrailingDownStreak_counts_only_the_trailing_run() |
| 12 | + { |
| 13 | + var history = History( |
| 14 | + ServiceStatuses.Down, // earlier run, does not count |
| 15 | + ServiceStatuses.Operational, // breaks it |
| 16 | + ServiceStatuses.Down, |
| 17 | + ServiceStatuses.Down); |
| 18 | + Assert.Equal(2, OutagePolicy.TrailingDownStreak(history)); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void Degraded_breaks_the_run() |
| 23 | + { |
| 24 | + // A slow-but-reachable probe is not down and must reset the streak. |
| 25 | + var history = History( |
| 26 | + ServiceStatuses.Down, |
| 27 | + ServiceStatuses.Degraded, |
| 28 | + ServiceStatuses.Down); |
| 29 | + Assert.Equal(1, OutagePolicy.TrailingDownStreak(history)); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void Empty_history_has_no_streak() |
| 34 | + { |
| 35 | + Assert.Equal(0, OutagePolicy.TrailingDownStreak(new List<string>())); |
| 36 | + } |
| 37 | + |
| 38 | + [Theory] |
| 39 | + [InlineData(1, 3, false)] // one failure, threshold 3 -> not yet |
| 40 | + [InlineData(2, 3, false)] // two failures, threshold 3 -> not yet |
| 41 | + [InlineData(3, 3, true)] // threshold reached |
| 42 | + [InlineData(4, 3, true)] // still open past the threshold |
| 43 | + public void OutageConfirmed_requires_threshold_consecutive_downs(int downCount, int threshold, bool expected) |
| 44 | + { |
| 45 | + var history = Enumerable.Repeat(ServiceStatuses.Down, downCount).ToList(); |
| 46 | + Assert.Equal(expected, OutagePolicy.OutageConfirmed(history, threshold)); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void A_single_operational_probe_before_the_run_still_confirms() |
| 51 | + { |
| 52 | + var history = History( |
| 53 | + ServiceStatuses.Operational, |
| 54 | + ServiceStatuses.Down, |
| 55 | + ServiceStatuses.Down, |
| 56 | + ServiceStatuses.Down); |
| 57 | + Assert.True(OutagePolicy.OutageConfirmed(history, 3)); |
| 58 | + } |
| 59 | + |
| 60 | + [Theory] |
| 61 | + [InlineData(0)] |
| 62 | + [InlineData(-5)] |
| 63 | + public void Threshold_of_one_or_less_opens_on_the_first_down(int threshold) |
| 64 | + { |
| 65 | + var history = History(ServiceStatuses.Down); |
| 66 | + Assert.True(OutagePolicy.OutageConfirmed(history, threshold)); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public void Threshold_of_one_does_not_open_on_a_reachable_probe() |
| 71 | + { |
| 72 | + Assert.False(OutagePolicy.OutageConfirmed(History(ServiceStatuses.Degraded), 1)); |
| 73 | + Assert.False(OutagePolicy.OutageConfirmed(History(ServiceStatuses.Operational), 1)); |
| 74 | + } |
| 75 | +} |
0 commit comments