From 335f14d7ad5bfdf95dbec751d9661750ab5d4593 Mon Sep 17 00:00:00 2001 From: Ivo Anjo Date: Thu, 9 Feb 2023 09:26:47 +0000 Subject: [PATCH] Fix flaky spec on Ruby 3.2 **What does this PR do?**: This PR fixes a flaky spec for Ruby 3.2 I observed in CI in This issue can also be reproduced locally (again on 3.2) by running `bundle exec rspec ./spec/datadog/core/environment/cgroup_spec.rb:17`. ``` Datadog::Core::Environment::Cgroup ::descriptors when the '/proc/self/cgroup' file is not present is expected to receive exist?("/proc/self/cgroup") 1 time (FAILED - 1) Failures: 1) Datadog::Core::Environment::Cgroup::descriptors when the '/proc/self/cgroup' file is not present is expected to receive exist?("/proc/self/cgroup") 1 time Failure/Error: super # received :exist? with unexpected arguments expected: ("/proc/self/cgroup") got: ("") # ./lib/datadog/core/logger.rb:16:in `initialize' # ./lib/datadog/core/configuration.rb:265:in `new' # ./lib/datadog/core/configuration.rb:265:in `logger_without_components' # ./lib/datadog/core/configuration.rb:174:in `logger' # ./spec/datadog/core/environment/cgroup_spec.rb:17:in `block (4 levels) in ' # ./spec/spec_helper.rb:220:in `block (2 levels) in ' # ./spec/spec_helper.rb:112:in `block (2 levels) in ' # /home/ivo.anjo/.rvm/gems/ruby-3.2.0/gems/webmock-3.13.0/lib/webmock/rspec.rb:37:in `block (2 levels) in ' ``` This issue is caused by the built-in `Logger` in Ruby 3.2 (which we subclass in our `Datadog::Core::Logger`) also using `File.exist?`. Thus, because in this test we setup a very restrictive expectation on this method, this was causing the `Logger` class to fail. This test was flaky because this initialization relied on no logger existing before -- if any other test had triggered the logger initialization, then this spec would pass. I've fixed this by making sure the logger is initialized before we setup any expectations on `File`. **Motivation**: Let's get to ZERO flaky specs! **Additional Notes**: (N/A) **How to test the change?**: See instructions above -- the spec fails when executed individually before this change, and is fixed by this PR. --- spec/datadog/core/environment/cgroup_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/datadog/core/environment/cgroup_spec.rb b/spec/datadog/core/environment/cgroup_spec.rb index 6fa2c2498c1..71c5457bd3f 100644 --- a/spec/datadog/core/environment/cgroup_spec.rb +++ b/spec/datadog/core/environment/cgroup_spec.rb @@ -10,11 +10,11 @@ context 'when the \'/proc/self/cgroup\' file is not present' do before do + expect(Datadog.logger).to_not receive(:error) + expect(File).to receive(:exist?) .with('/proc/self/cgroup') .and_return(false) - - expect(Datadog.logger).to_not receive(:error) end it do