|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2026, by Samuel Williams. |
| 5 | + |
| 6 | +require "async/service/policy" |
| 7 | +require "async/container/statistics" |
| 8 | + |
| 9 | +describe Async::Service::Policy do |
| 10 | + let(:policy) {subject.for(maximum_failures: 5, window: 10)} |
| 11 | + |
| 12 | + with "::DEFAULT" do |
| 13 | + it "exists and is frozen" do |
| 14 | + expect(Async::Service::Policy::DEFAULT).not.to be_nil |
| 15 | + expect(Async::Service::Policy::DEFAULT).to be(:frozen?) |
| 16 | + end |
| 17 | + |
| 18 | + it "is a Service::Policy instance" do |
| 19 | + expect(Async::Service::Policy::DEFAULT).to be_a(Async::Service::Policy) |
| 20 | + end |
| 21 | + |
| 22 | + it "has working helper methods" do |
| 23 | + status = Object.new |
| 24 | + def status.termsig; Signal.list["SEGV"]; end |
| 25 | + |
| 26 | + expect(Async::Service::Policy::DEFAULT).to be(:segfault?, status) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + with "failure rate monitoring" do |
| 31 | + let(:mock_container) do |
| 32 | + Class.new do |
| 33 | + attr_reader :stopped, :statistics |
| 34 | + |
| 35 | + def initialize |
| 36 | + @stopped = false |
| 37 | + @statistics = Async::Container::Statistics.new(window: 10) |
| 38 | + end |
| 39 | + |
| 40 | + def stop(graceful) |
| 41 | + @stopped = true |
| 42 | + end |
| 43 | + end.new |
| 44 | + end |
| 45 | + |
| 46 | + let(:mock_status) do |
| 47 | + Class.new do |
| 48 | + def success? |
| 49 | + false |
| 50 | + end |
| 51 | + end.new |
| 52 | + end |
| 53 | + |
| 54 | + it "stops container when failure rate exceeds threshold" do |
| 55 | + # Policy with max 5 failures in 10 second window |
| 56 | + policy = subject.for(maximum_failures: 5, window: 10) |
| 57 | + |
| 58 | + # Add 6 failures (exceeds threshold of 5 in 10 seconds) |
| 59 | + 6.times do |
| 60 | + mock_container.statistics.failure! |
| 61 | + end |
| 62 | + |
| 63 | + # 6 failures in same second = 0.6/sec which exceeds 5/10sec = 0.5/sec |
| 64 | + rate = mock_container.statistics.failure_rate.per_second |
| 65 | + expect(rate).to be > 0.5 |
| 66 | + expect(mock_container.stopped).to be == false |
| 67 | + |
| 68 | + # Trigger policy check |
| 69 | + policy.child_exit(mock_container, nil, mock_status, name: "test", key: nil) |
| 70 | + |
| 71 | + expect(mock_container.stopped).to be == true |
| 72 | + end |
| 73 | + |
| 74 | + it "does not stop container when failure rate is acceptable" do |
| 75 | + # Policy with max 10 failures in 10 second window |
| 76 | + policy = subject.for(maximum_failures: 10, window: 10) |
| 77 | + |
| 78 | + # Add only 3 failures (below threshold) |
| 79 | + 3.times do |
| 80 | + mock_container.statistics.failure! |
| 81 | + end |
| 82 | + |
| 83 | + # 3 failures = 0.3/sec which is below 10/10sec = 1.0/sec |
| 84 | + rate = mock_container.statistics.failure_rate.per_second |
| 85 | + expect(rate).to be < 1.0 |
| 86 | + |
| 87 | + # Trigger policy check |
| 88 | + policy.child_exit(mock_container, nil, mock_status, name: "test", key: nil) |
| 89 | + |
| 90 | + expect(mock_container.stopped).to be == false |
| 91 | + end |
| 92 | + |
| 93 | + it "does nothing on successful exit" do |
| 94 | + policy = subject.for(maximum_failures: 1, window: 10) |
| 95 | + |
| 96 | + success_status = Object.new |
| 97 | + def success_status.success?; true; end |
| 98 | + |
| 99 | + # Even with low threshold, success shouldn't trigger stop |
| 100 | + policy.child_exit(mock_container, nil, success_status, name: "test", key: nil) |
| 101 | + |
| 102 | + expect(mock_container.stopped).to be == false |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + with "custom parameters" do |
| 107 | + it "can be created with custom threshold" do |
| 108 | + policy = subject.for(maximum_failures: 20, window: 30) |
| 109 | + |
| 110 | + expect(policy).to be_a(Async::Service::Policy) |
| 111 | + end |
| 112 | + |
| 113 | + it "can be initialized with raw threshold" do |
| 114 | + policy = subject.new(0.5) |
| 115 | + |
| 116 | + expect(policy).to be_a(Async::Service::Policy) |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments