|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2018-2025, by Samuel Williams. |
| 5 | + |
| 6 | +require "async/deadline" |
| 7 | + |
| 8 | +describe Async::Deadline do |
| 9 | + with ".start" do |
| 10 | + it "returns nil for nil timeout" do |
| 11 | + deadline = subject.start(nil) |
| 12 | + expect(deadline).to be_nil |
| 13 | + end |
| 14 | + |
| 15 | + it "returns Zero module for zero timeout" do |
| 16 | + deadline = subject.start(0) |
| 17 | + expect(deadline).to be == Async::Deadline::Zero |
| 18 | + end |
| 19 | + |
| 20 | + it "returns Zero module for negative timeout" do |
| 21 | + deadline = subject.start(-1) |
| 22 | + expect(deadline).to be == Async::Deadline::Zero |
| 23 | + end |
| 24 | + |
| 25 | + it "returns new instance for positive timeout" do |
| 26 | + deadline = subject.start(5.0) |
| 27 | + expect(deadline).to be_a(Async::Deadline) |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + describe Async::Deadline::Zero do |
| 32 | + let(:zero) {subject} |
| 33 | + |
| 34 | + it "is always expired" do |
| 35 | + expect(zero.expired?).to be == true |
| 36 | + end |
| 37 | + |
| 38 | + it "has zero remaining time" do |
| 39 | + expect(zero.remaining).to be == 0 |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + with "#remaining" do |
| 44 | + it "initializes with reasonable remaining time" do |
| 45 | + deadline = subject.new(5.0) |
| 46 | + expect(deadline.remaining).to be <= 5.0 |
| 47 | + end |
| 48 | + |
| 49 | + it "decreases remaining time as time passes" do |
| 50 | + deadline = subject.new(1.0) |
| 51 | + |
| 52 | + # Get initial remaining time |
| 53 | + first_remaining = deadline.remaining |
| 54 | + expect(first_remaining).to be <= 1.0 |
| 55 | + |
| 56 | + # Wait a tiny bit and check again |
| 57 | + sleep(0.001) |
| 58 | + |
| 59 | + second_remaining = deadline.remaining |
| 60 | + expect(second_remaining).to be < first_remaining |
| 61 | + end |
| 62 | + |
| 63 | + it "can return negative remaining time when expired" do |
| 64 | + # Create a deadline with very short timeout |
| 65 | + deadline = subject.new(0.001) # 1 millisecond |
| 66 | + |
| 67 | + # Wait longer than the timeout |
| 68 | + sleep(0.002) |
| 69 | + |
| 70 | + remaining = deadline.remaining |
| 71 | + expect(remaining).to be < 0 # Should be negative |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + with "#expired?" do |
| 76 | + it "returns false for fresh deadline" do |
| 77 | + deadline = subject.new(2.0) |
| 78 | + expect(deadline.expired?).to be == false |
| 79 | + end |
| 80 | + |
| 81 | + it "returns true when deadline has expired" do |
| 82 | + # Create very short deadline that will expire quickly |
| 83 | + deadline = subject.new(0.001) |
| 84 | + |
| 85 | + # Wait for it to expire |
| 86 | + sleep(0.01) |
| 87 | + |
| 88 | + expect(deadline.expired?).to be == true |
| 89 | + end |
| 90 | + |
| 91 | + it "updates remaining time when checked" do |
| 92 | + deadline = subject.new(1.0) |
| 93 | + |
| 94 | + # Get initial remaining time |
| 95 | + first_remaining = deadline.remaining |
| 96 | + |
| 97 | + # Check if expired (which calls remaining internally) |
| 98 | + expired = deadline.expired? |
| 99 | + expect(expired).to be == false |
| 100 | + |
| 101 | + # Get remaining time again - should be less |
| 102 | + second_remaining = deadline.remaining |
| 103 | + expect(second_remaining).to be < first_remaining |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + it "handles sequential operations correctly" do |
| 108 | + deadline = subject.new(1.0) # 1 second timeout |
| 109 | + |
| 110 | + # First check - should have close to full time |
| 111 | + first_remaining = deadline.remaining |
| 112 | + expect(first_remaining).to be <= 1.0 |
| 113 | + expect(first_remaining).to be > 0.5 # Should still have most of the time |
| 114 | + |
| 115 | + # Short delay |
| 116 | + sleep(0.001) |
| 117 | + |
| 118 | + # Second check - should be less |
| 119 | + second_remaining = deadline.remaining |
| 120 | + expect(second_remaining).to be < first_remaining |
| 121 | + |
| 122 | + # Should still not be expired |
| 123 | + expect(deadline.expired?).to be == false |
| 124 | + end |
| 125 | +end |
0 commit comments