Skip to content

Commit a547a41

Browse files
authored
Use Stoplight::TimeTravel in tests
1 parent 0c1f82f commit a547a41

10 files changed

Lines changed: 36 additions & 36 deletions

File tree

spec/properties/notifications_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def notifications(name)
4747
notifications_before_run = notifier.notifications(light.name).count
4848

4949
executions_sequence.each do |(should_fail, time_gap)|
50-
Timecop.freeze(Time.now + time_gap)
50+
Stoplight::TimeTravel.freeze(Time.now + time_gap)
5151
suppress(StandardError) { light.run { raise if should_fail } }
5252

5353
color_after_run = light.color

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
end
4848

4949
rspec.after do
50-
Timecop.return
50+
Stoplight::TimeTravel.return
5151
redis = Redis.new
5252
redis.del("stoplight:test_now_ms_stack")
5353
end

spec/support/data_store/transition_to_color.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
it { expect(transition_to_color(Stoplight::Color::GREEN)).to be(true) }
4141

4242
it "resets timestamps" do
43-
Timecop.freeze(current_time) do
43+
Stoplight::TimeTravel.freeze(current_time) do
4444
transition_to_color(Stoplight::Color::GREEN)
4545
end
4646

@@ -73,7 +73,7 @@
7373

7474
it "sets the recovery_started_at timestamp" do
7575
expect do
76-
Timecop.freeze(current_time) do
76+
Stoplight::TimeTravel.freeze(current_time) do
7777
transition_to_color(Stoplight::Color::YELLOW)
7878
end
7979
end.to change { state_snapshot }
@@ -83,7 +83,7 @@
8383

8484
context "when cleared" do
8585
it "looses persisted state" do
86-
Timecop.freeze(current_time) do
86+
Stoplight::TimeTravel.freeze(current_time) do
8787
transition_to_color(Stoplight::Color::YELLOW)
8888
end
8989
clear
@@ -113,7 +113,7 @@
113113

114114
it "sets the breached_at and recovery_scheduled_after timestamps" do
115115
expect do
116-
Timecop.freeze(current_time) do
116+
Stoplight::TimeTravel.freeze(current_time) do
117117
transition_to_color(Stoplight::Color::RED)
118118
end
119119
end.to change { state_snapshot }
@@ -123,7 +123,7 @@
123123

124124
context "when cleared" do
125125
it "looses persisted state" do
126-
Timecop.freeze(current_time) do
126+
Stoplight::TimeTravel.freeze(current_time) do
127127
transition_to_color(Stoplight::Color::YELLOW)
128128
end
129129
clear
@@ -143,7 +143,7 @@
143143

144144
it "sets the breached_at and recovery_scheduled_after timestamps" do
145145
expect do
146-
Timecop.freeze(current_time) do
146+
Stoplight::TimeTravel.freeze(current_time) do
147147
transition_to_color(Stoplight::Color::RED)
148148
end
149149
end.to change { state_snapshot }
@@ -153,7 +153,7 @@
153153

154154
context "when cleared" do
155155
it "looses persisted state" do
156-
Timecop.freeze(current_time) do
156+
Stoplight::TimeTravel.freeze(current_time) do
157157
transition_to_color(Stoplight::Color::YELLOW)
158158
end
159159
clear

spec/support/timecop.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
RSpec.configure do |config|
66
config.around(:each, freeze: ->(value) { value.present? }) do |example|
77
time = Time.now.to_i
8-
Timecop.freeze(time) { example.run }
8+
Stoplight::TimeTravel.freeze(time) { example.run }
99
end
1010
end

spec/unit/stoplight/domain/failure_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
describe ".from_error" do
2020
it "creates a failure" do
21-
Timecop.freeze do
21+
Stoplight::TimeTravel.freeze do
2222
failure = described_class.from_error(error, time: Time.now)
2323
expect(failure.error_class).to eql(error_class)
2424
expect(failure.error_message).to eql(error_message)

spec/unit/stoplight/infrastructure/data_store/metrics_snapshot.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
record_success
1414

1515
expect do
16-
Timecop.freeze(last_success_time) do
16+
Stoplight::TimeTravel.freeze(last_success_time) do
1717
record_success
1818
end
1919
end.to change { metrics_snapshot.last_success_at }.to(be_within(rounding_error).of(last_success_time))
2020
end
2121

2222
specify "when first success tracked" do
2323
expect do
24-
Timecop.freeze(last_success_time) do
24+
Stoplight::TimeTravel.freeze(last_success_time) do
2525
record_success
2626
end
2727
end.to change { metrics_snapshot.last_success_at }.from(nil).to(be_within(rounding_error).of(last_success_time))
@@ -35,15 +35,15 @@
3535
record_failure(error)
3636

3737
expect do
38-
Timecop.freeze(last_error_time) do
38+
Stoplight::TimeTravel.freeze(last_error_time) do
3939
record_failure(error)
4040
end
4141
end.to change { metrics_snapshot.last_error_at }.to(be_within(rounding_error).of(last_error_time))
4242
end
4343

4444
specify "when first failure tracked" do
4545
expect do
46-
Timecop.freeze(last_error_time) do
46+
Stoplight::TimeTravel.freeze(last_error_time) do
4747
record_failure(error)
4848
end
4949
end.to change { metrics_snapshot.last_error_at }.from(nil).to(be_within(rounding_error).of(last_error_time))

spec/unit/stoplight/infrastructure/data_store/recovery_metrics.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
record_success
1414

1515
expect do
16-
Timecop.freeze(last_success_time) do
16+
Stoplight::TimeTravel.freeze(last_success_time) do
1717
record_success
1818
end
1919
end.to change { get_metrics.last_success_at }.to(be_within(rounding_error).of(last_success_time))
2020
end
2121

2222
specify "when first success tracked" do
2323
expect do
24-
Timecop.freeze(last_success_time) do
24+
Stoplight::TimeTravel.freeze(last_success_time) do
2525
record_success
2626
end
2727
end.to change { get_metrics.last_success_at }.from(nil).to(be_within(rounding_error).of(last_success_time))
@@ -35,15 +35,15 @@
3535
record_failure(error)
3636

3737
expect do
38-
Timecop.freeze(last_error_time) do
38+
Stoplight::TimeTravel.freeze(last_error_time) do
3939
record_failure(error)
4040
end
4141
end.to change { get_metrics.last_error_at }.to(be_within(rounding_error).of(last_error_time))
4242
end
4343

4444
specify "when first failure tracked" do
4545
expect do
46-
Timecop.freeze(last_error_time) do
46+
Stoplight::TimeTravel.freeze(last_error_time) do
4747
record_failure(error)
4848
end
4949
end.to change { get_metrics.last_error_at }.to(be_within(rounding_error).of(last_error_time))

spec/unit/stoplight/infrastructure/data_store/window_metrics_snapshot.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
let(:window_size) { 5000 }
1010

1111
before do
12-
Timecop.freeze(-window_size - 10) do
12+
Stoplight::TimeTravel.freeze(-window_size - 10) do
1313
record_success
1414
end
1515
end
@@ -27,7 +27,7 @@
2727
let(:window_size) { 5000 }
2828

2929
before do
30-
Timecop.freeze(-window_size - 10) do
30+
Stoplight::TimeTravel.freeze(-window_size - 10) do
3131
record_failure(error)
3232
end
3333
end
@@ -79,10 +79,10 @@
7979
let(:window_size) { 300 }
8080

8181
it "excludes it from the count" do
82-
Timecop.freeze(Time.now) do
82+
Stoplight::TimeTravel.freeze(Time.now) do
8383
record_success
8484

85-
Timecop.freeze(window_size) do
85+
Stoplight::TimeTravel.freeze(window_size) do
8686
expect(metrics_snapshot.successes).to eq(0)
8787
end
8888
end
@@ -93,10 +93,10 @@
9393
let(:window_size) { 300 }
9494

9595
it "excludes it from the count" do
96-
Timecop.freeze(Time.now) do
96+
Stoplight::TimeTravel.freeze(Time.now) do
9797
record_failure(error)
9898

99-
Timecop.freeze(window_size) do
99+
Stoplight::TimeTravel.freeze(window_size) do
100100
expect(metrics_snapshot.errors).to eq(0)
101101
end
102102
end
@@ -110,10 +110,10 @@
110110
let(:bucket_count) { window_size }
111111

112112
it "evicts the whole backlog without raising, keeping successes correct" do
113-
Timecop.freeze(Time.now) do
114-
bucket_count.times { |second| Timecop.freeze(second) { record_success } }
113+
Stoplight::TimeTravel.freeze do
114+
bucket_count.times { |second| Stoplight::TimeTravel.freeze(second) { record_success } }
115115

116-
Timecop.freeze(bucket_count + window_size + 10) do
116+
Stoplight::TimeTravel.freeze(bucket_count + window_size + 10) do
117117
expect { record_success }.not_to raise_error
118118

119119
expect(metrics_snapshot.successes).to eq(1)
@@ -122,10 +122,10 @@
122122
end
123123

124124
it "evicts the whole backlog without raising, keeping errors correct" do
125-
Timecop.freeze(Time.now) do
126-
bucket_count.times { |second| Timecop.freeze(second) { record_failure(error) } }
125+
Stoplight::TimeTravel.freeze do
126+
bucket_count.times { |second| Stoplight::TimeTravel.freeze(second) { record_failure(error) } }
127127

128-
Timecop.freeze(bucket_count + window_size + 10) do
128+
Stoplight::TimeTravel.freeze(bucket_count + window_size + 10) do
129129
expect { record_failure(error) }.not_to raise_error
130130

131131
expect(metrics_snapshot.errors).to eq(1)
@@ -134,12 +134,12 @@
134134
end
135135

136136
it "subtracts successes and failures independently when buckets mix both" do
137-
Timecop.freeze(Time.now) do
137+
Stoplight::TimeTravel.freeze do
138138
bucket_count.times do |second|
139-
Timecop.freeze(second) { second.even? ? record_success : record_failure(error) }
139+
Stoplight::TimeTravel.freeze(second) { second.even? ? record_success : record_failure(error) }
140140
end
141141

142-
Timecop.freeze(bucket_count + window_size + 10) do
142+
Stoplight::TimeTravel.freeze(bucket_count + window_size + 10) do
143143
record_success
144144

145145
expect(metrics_snapshot.successes).to eq(1)

spec/unit/stoplight/infrastructure/redis/storage/unbounded_metrics_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def record_success = unbounded_metrics.record_success
2626
it "keeps the TTL on the metrics key for a subsequent out-of-order failure" do
2727
unbounded_metrics.record_failure(StandardError.new)
2828

29-
Timecop.freeze(Time.now - 30) do
29+
Stoplight::TimeTravel.freeze(Time.now - 30) do
3030
unbounded_metrics.record_failure(StandardError.new)
3131
end
3232

spec/unit/stoplight/infrastructure/system_clock_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
subject(:current_time) { clock.current_time }
88

99
around do |example|
10-
Timecop.freeze { example.run }
10+
Stoplight::TimeTravel.freeze { example.run }
1111
end
1212

1313
it "returns current wall-clock time" do

0 commit comments

Comments
 (0)