Skip to content

Commit f702785

Browse files
samuel-williams-shopifyioquatix
authored andcommitted
Add Clock#as_json and Clock#to_json.
1 parent 71cd4e0 commit f702785

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

lib/async/clock.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def initialize(total = 0)
3636
@started = nil
3737
end
3838

39+
# @returns [Numeric | Nil] The time when the clock was started, or nil if not started.
40+
attr :started
41+
3942
# Start measuring a duration.
4043
def start!
4144
@started ||= Clock.now
@@ -70,5 +73,22 @@ def reset!
7073
@started = Clock.now
7174
end
7275
end
76+
77+
# Convert the clock to a JSON-compatible hash.
78+
#
79+
# @returns [Hash] The JSON-compatible hash.
80+
def as_json(...)
81+
{
82+
started: self.started,
83+
total: self.total,
84+
}
85+
end
86+
87+
# Convert the clock to a JSON string.
88+
#
89+
# @returns [String] The JSON string.
90+
def to_json(...)
91+
self.as_json.to_json(...)
92+
end
7393
end
7494
end

test/async/clock.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,83 @@
146146
expect(duration).to be >= 0
147147
end
148148
end
149+
150+
with "#as_json" do
151+
it "returns a hash with started and total keys" do
152+
json = clock.as_json
153+
154+
expect(json).to be_a Hash
155+
expect(json).to have_keys(:started, :total)
156+
end
157+
158+
it "includes nil started when not started" do
159+
json = clock.as_json
160+
161+
expect(json[:started]).to be_nil
162+
expect(json[:total]).to be == 0
163+
end
164+
165+
it "includes started time when started" do
166+
clock.start!
167+
json = clock.as_json
168+
169+
expect(json[:started]).to be_a Numeric
170+
expect(json[:started]).to be > 0
171+
expect(json[:total]).to be >= 0
172+
end
173+
174+
it "includes accumulated total" do
175+
clock.start!
176+
sleep(0.001)
177+
clock.stop!
178+
json = clock.as_json
179+
180+
expect(json[:started]).to be_nil
181+
expect(json[:total]).to be > 0
182+
end
183+
184+
it "includes total with initial duration" do
185+
clock = subject.new(5.0)
186+
json = clock.as_json
187+
188+
expect(json[:total]).to be == 5.0
189+
end
190+
end
191+
192+
with "#to_json" do
193+
it "returns a JSON string" do
194+
json_string = clock.to_json
195+
196+
expect(json_string).to be_a String
197+
end
198+
199+
it "can be parsed back to a hash" do
200+
clock.start!
201+
sleep(0.001)
202+
clock.stop!
203+
204+
json_string = clock.to_json
205+
parsed = JSON.parse(json_string)
206+
207+
expect(parsed).to be_a Hash
208+
expect(parsed).to have_keys("started", "total")
209+
expect(parsed["total"]).to be > 0
210+
end
211+
212+
it "preserves nil started value" do
213+
json_string = clock.to_json
214+
parsed = JSON.parse(json_string)
215+
216+
expect(parsed["started"]).to be_nil
217+
end
218+
219+
it "preserves started time when running" do
220+
clock.start!
221+
json_string = clock.to_json
222+
parsed = JSON.parse(json_string)
223+
224+
expect(parsed["started"]).to be_a Numeric
225+
expect(parsed["started"]).to be > 0
226+
end
227+
end
149228
end

0 commit comments

Comments
 (0)