Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@

it "blocks subsequent calls when rate limited with Retry-After header (date)" do
stub_request(:post, "http://localhost:8080/ofrep/v1/evaluate/flags/my_flag")
.to_return(status: 429, headers: {"Retry-After" => (Time.now + 1).httpdate})
.to_return(status: 429, headers: {"Retry-After" => (Time.now + 60).httpdate})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While increasing the delay to 60 seconds makes this test less likely to fail, it doesn't fully eliminate the root cause of flakiness, which is the use of Time.now in tests. This makes the test's outcome dependent on execution speed.

A more robust approach is to control time by stubbing Time.now. This makes your time-dependent tests deterministic, faster (by avoiding sleep in other tests), and more reliable.

For example, this test could be rewritten as:

it 'blocks subsequent calls when rate limited with Retry-After header (date)' do
  start_time = Time.new(2023, 1, 1, 12, 0, 0)
  allow(Time).to receive(:now).and_return(start_time)

  stub_request(:post, 'http://localhost:8080/ofrep/v1/evaluate/flags/my_flag')
    .to_return(status: 429, headers: { 'Retry-After' => (start_time + 2).httpdate })

  expect {
    client.evaluate(flag_key: 'my_flag', evaluation_context: default_evaluation_context)
  }.to raise_error(OpenFeature::OFREP::RateLimited)

  # Advance time, but stay within the retry window
  allow(Time).to receive(:now).and_return(start_time + 1)

  expect {
    client.evaluate(flag_key: 'other_flag', evaluation_context: default_evaluation_context)
  }.to raise_error(OpenFeature::OFREP::RateLimited)
end

Applying this pattern across your time-sensitive tests would be a significant improvement to the test suite's stability and prevent future flakiness.


expect {
client.evaluate(flag_key: "my_flag", evaluation_context: default_evaluation_context)
Expand Down
Loading