Skip to content

Commit 2ec7620

Browse files
Tidy up tests.
1 parent 8d4c075 commit 2ec7620

2 files changed

Lines changed: 54 additions & 62 deletions

File tree

lib/async/deadline.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
# @namespace
99
module Async
10-
# Manages a countdown timer for timeout operations.
11-
# Provides precise deadline tracking for compound operations to prevent timeout accumulation where multiple operations could exceed user timeouts.
10+
# Represents a deadline timeout with decrementing remaining time.
11+
# Includes an efficient representation for zero (non-blocking) timeouts.
1212
# @public Since *Async v2.31*.
1313
class Deadline
1414
# Singleton module for immediate timeouts (zero or negative).
15-
# Avoids object allocation for performance.
15+
# Avoids object allocation for fast path (non-blocking) timeouts.
1616
module Zero
1717
# Check if the deadline has expired.
1818
# @returns [Boolean] Always returns true since zero timeouts are immediately expired.

test/async/clock.rb

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,41 @@
3131
expect(clock.total).to be_within(2 * Sus::Fixtures::Time::QUANTUM).of(0.02)
3232
end
3333

34+
with "#start" do
35+
it "handles multiple start/stop cycles" do
36+
3.times do
37+
clock.start!
38+
# Calling start! again should be idempotent - no time should be added
39+
first_total = clock.total
40+
clock.start!
41+
second_total = clock.total
42+
43+
# The total should not jump significantly just from calling start! again
44+
expect(second_total - first_total).to be < 0.001
45+
clock.stop!
46+
end
47+
48+
expect(clock.total).to be >= 0
49+
end
50+
end
51+
52+
with "#stop" do
53+
it "handles stop without start" do
54+
result = clock.stop!
55+
expect(result).to be == 0
56+
expect(clock.total).to be == 0
57+
end
58+
59+
it "handles multiple stops" do
60+
clock.start!
61+
first_stop = clock.stop!
62+
second_stop = clock.stop!
63+
64+
expect(first_stop).to be == second_stop
65+
expect(clock.total).to be == first_stop
66+
end
67+
end
68+
3469
with "#total" do
3570
with "initial duration" do
3671
let(:clock) {subject.new(1.5)}
@@ -48,6 +83,21 @@
4883
sleep(0.0001)
4984
expect(clock.total).to be >= total
5085
end
86+
87+
it "preserves total during start/stop cycles" do
88+
# First cycle
89+
clock.start!
90+
sleep(0.001)
91+
first_total = clock.stop!
92+
93+
# Second cycle
94+
clock.start!
95+
sleep(0.001)
96+
second_total = clock.stop!
97+
98+
expect(second_total).to be > first_total
99+
expect(clock.total).to be == second_total
100+
end
51101
end
52102

53103
with ".start" do
@@ -77,7 +127,7 @@
77127
end
78128
end
79129

80-
with "monotonicity" do
130+
with ".now" do
81131
it "produces monotonic timestamps" do
82132
first = Async::Clock.now
83133
second = Async::Clock.now
@@ -95,62 +145,4 @@
95145
expect(duration).to be >= 0
96146
end
97147
end
98-
99-
with "edge cases" do
100-
it "handles multiple start/stop cycles" do
101-
3.times do
102-
clock.start!
103-
# Calling start! again should not change the start time
104-
original_start = clock.instance_variable_get(:@started)
105-
clock.start!
106-
expect(clock.instance_variable_get(:@started)).to be == original_start
107-
clock.stop!
108-
end
109-
110-
expect(clock.total).to be >= 0
111-
end
112-
113-
it "handles stop without start" do
114-
result = clock.stop!
115-
expect(result).to be == 0
116-
expect(clock.total).to be == 0
117-
end
118-
119-
it "handles multiple stops" do
120-
clock.start!
121-
first_stop = clock.stop!
122-
second_stop = clock.stop!
123-
124-
expect(first_stop).to be == second_stop
125-
expect(clock.total).to be == first_stop
126-
end
127-
128-
it "preserves total during start/stop cycles" do
129-
# First cycle
130-
clock.start!
131-
sleep(0.001)
132-
first_total = clock.stop!
133-
134-
# Second cycle
135-
clock.start!
136-
sleep(0.001)
137-
second_total = clock.stop!
138-
139-
expect(second_total).to be > first_total
140-
expect(clock.total).to be == second_total
141-
end
142-
143-
it "includes running time in total" do
144-
base_total = clock.total
145-
expect(base_total).to be == 0
146-
147-
clock.start!
148-
sleep(0.001)
149-
running_total = clock.total
150-
151-
expect(running_total).to be > base_total
152-
expect(clock.instance_variable_get(:@started)).not.to be_nil
153-
end
154-
end
155-
156148
end

0 commit comments

Comments
 (0)