|
| 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.new(max_failure_rate: 0.5, window: 10)} |
| 11 | + |
| 12 | + with "inheritance" do |
| 13 | + it "inherits from Container::Policy" do |
| 14 | + expect(Async::Service::Policy).to be < Async::Container::Policy |
| 15 | + end |
| 16 | + |
| 17 | + it "has DEFAULT constant" do |
| 18 | + expect(Async::Service::Policy::DEFAULT).not.to be_nil |
| 19 | + end |
| 20 | + |
| 21 | + it "DEFAULT is frozen" do |
| 22 | + expect(Async::Service::Policy::DEFAULT).to be(:frozen?) |
| 23 | + end |
| 24 | + |
| 25 | + it "DEFAULT is a Service::Policy" do |
| 26 | + expect(Async::Service::Policy::DEFAULT).to be_a(Async::Service::Policy) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + with "helper methods" do |
| 31 | + it "inherits helper methods from Container::Policy" do |
| 32 | + expect(policy).to respond_to(:segfault?) |
| 33 | + expect(policy).to respond_to(:success?) |
| 34 | + expect(policy).to respond_to(:signal) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + with "failure rate monitoring" do |
| 39 | + let(:mock_container) do |
| 40 | + Class.new do |
| 41 | + attr_reader :stopped, :statistics |
| 42 | + |
| 43 | + def initialize |
| 44 | + @stopped = false |
| 45 | + @statistics = Async::Container::Statistics.new(window: 10) |
| 46 | + end |
| 47 | + |
| 48 | + def stop(graceful) |
| 49 | + @stopped = true |
| 50 | + end |
| 51 | + end.new |
| 52 | + end |
| 53 | + |
| 54 | + let(:mock_status) do |
| 55 | + Class.new do |
| 56 | + def success? |
| 57 | + false |
| 58 | + end |
| 59 | + end.new |
| 60 | + end |
| 61 | + |
| 62 | + it "stops container when failure rate exceeds threshold" do |
| 63 | + policy = subject.new(max_failure_rate: 0.5, window: 10) |
| 64 | + |
| 65 | + # Simulate failures that exceed the rate (0.5/sec = 5 per 10 seconds) |
| 66 | + # Add 6 failures using the real time (they'll all be in the same second) |
| 67 | + 6.times do |
| 68 | + mock_container.statistics.failure! |
| 69 | + end |
| 70 | + |
| 71 | + # All failures in the same second = high rate |
| 72 | + rate = mock_container.statistics.failure_rate.per_second |
| 73 | + expect(rate).to be > 0.5 |
| 74 | + expect(mock_container.stopped).to be == false |
| 75 | + |
| 76 | + # Trigger policy check |
| 77 | + policy.child_exit(mock_container, nil, status: mock_status, name: "test", key: nil) |
| 78 | + |
| 79 | + expect(mock_container.stopped).to be == true |
| 80 | + end |
| 81 | + |
| 82 | + it "does not stop container when failure rate is acceptable" do |
| 83 | + policy = subject.new(max_failure_rate: 1.0, window: 10) |
| 84 | + |
| 85 | + # Add failures below threshold (3 in a 10 second window = 0.3/sec) |
| 86 | + 3.times do |
| 87 | + mock_container.statistics.failure! |
| 88 | + end |
| 89 | + |
| 90 | + rate = mock_container.statistics.failure_rate.per_second |
| 91 | + expect(rate).to be < 1.0 |
| 92 | + |
| 93 | + # Trigger policy check |
| 94 | + policy.child_exit(mock_container, nil, status: mock_status, name: "test", key: nil) |
| 95 | + |
| 96 | + expect(mock_container.stopped).to be == false |
| 97 | + end |
| 98 | + |
| 99 | + it "does nothing on successful exit" do |
| 100 | + policy = subject.new(max_failure_rate: 0.1, window: 10) |
| 101 | + |
| 102 | + success_status = Object.new |
| 103 | + def success_status.success?; true; end |
| 104 | + |
| 105 | + # Even with high failure rate, success shouldn't trigger stop |
| 106 | + policy.child_exit(mock_container, nil, status: success_status, name: "test", key: nil) |
| 107 | + |
| 108 | + expect(mock_container.stopped).to be == false |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + with "custom parameters" do |
| 113 | + it "can be initialized with custom threshold" do |
| 114 | + policy = subject.new(max_failure_rate: 2.0, window: 30) |
| 115 | + |
| 116 | + expect(policy).to be_a(Async::Service::Policy) |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments