Skip to content

Commit

Permalink
Chase down some flakey tests
Browse files Browse the repository at this point in the history
* Ensure we're waiting for futures after each test
* Reset to a known good state before suite
* Reset configuration in a standard manner
* Fix to breadcrumbs triggering failsafe
  • Loading branch information
nikz committed May 4, 2024
1 parent 6df4bc6 commit 630ba91
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 22 deletions.
6 changes: 5 additions & 1 deletion lib/raygun.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ def track_exception(exception_instance, env = {}, user = nil, retry_count = 1)

exception_instance.set_backtrace(caller) if exception_instance.is_a?(Exception) && exception_instance.backtrace.nil?

if configuration.send_in_background
result = if configuration.send_in_background
track_exception_async(exception_instance, env, user, retry_count)
else
track_exception_sync(exception_instance, env, user, retry_count)
end

result
end

def track_exceptions
Expand Down Expand Up @@ -137,6 +139,8 @@ def track_exception_async(exception_instance, env, user, retry_count)
@@active_futures.delete(future)
end, :call)
@@active_futures << future

future
end

def track_exception_sync(exception_instance, env, user, retry_count)
Expand Down
1 change: 0 additions & 1 deletion lib/raygun/breadcrumbs/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def self.should_record?(crumb)
levels = Raygun::Breadcrumbs::BREADCRUMB_LEVELS

active_level = levels.index(Raygun.configuration.breadcrumb_level)
puts crumb.inspect if crumb.is_a?(Array)
crumb_level = levels.index(crumb.level) || -1

discard = crumb_level < active_level
Expand Down
7 changes: 2 additions & 5 deletions lib/raygun/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,9 @@ def build_payload_hash(exception_instance, env = {}, user = nil)

# If we have breadcrumbs passed to us as context from another thread, then include them
# Otherwise, use the default store (which is thread-local)
store = if env.key?(:rg_breadcrumb_store)
::Raygun::Breadcrumbs::Store.initialize(with: env.delete(:rg_breadcrumb_store))
else
::Raygun::Breadcrumbs::Store
end
::Raygun::Breadcrumbs::Store.initialize(with: env.delete(:rg_breadcrumb_store)) if env.key?(:rg_breadcrumb_store)

store = ::Raygun::Breadcrumbs::Store
error_details[:breadcrumbs] = store.take_until_size(MAX_BREADCRUMBS_SIZE).map(&:build_payload) if store.any?

Raygun.log('set details and breadcrumbs')
Expand Down
12 changes: 7 additions & 5 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
require_relative "./rails_helper"
require_relative "../lib/raygun.rb"

# Ensure we start with a known state
Raygun.reset_configuration

class FakeLogger
def initialize
@logger = StringIO.new
Expand Down Expand Up @@ -42,6 +45,8 @@ def setup
end

def teardown
Raygun.wait_for_futures
Raygun.reset_configuration
end

end
Expand All @@ -53,17 +58,14 @@ def setup
end

def teardown
reset_configuration
Raygun.wait_for_futures
Raygun.reset_configuration
end

def fake_successful_entry
stub_request(:post, 'https://api.raygun.com/entries').to_return(status: 202)
end

def reset_configuration
Raygun.configuration = Raygun::Configuration.new
end

def setup_logging
logger = FakeLogger.new
Raygun.configuration.debug = true
Expand Down
4 changes: 0 additions & 4 deletions test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ def setup
end
end

def teardown
Raygun.reset_configuration
end

def test_setting_api_key_and_version
assert_equal 9.9, Raygun.configuration.version
assert_equal "a test api key", Raygun.configuration.api_key
Expand Down
11 changes: 9 additions & 2 deletions test/unit/error_subscriber_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

class ErrorSubscriberTest < Raygun::UnitTest

def setup
super
Raygun.configuration.send_in_background = false
end


def test_tracking_exception_via_subscriber
body_matcher = lambda do |body|
json = JSON.parse(body)
Expand All @@ -19,7 +25,7 @@ def test_tracking_exception_via_subscriber
)
.to_return(status: 202).times(1)

Raygun::ErrorSubscriber.new.report(
result = Raygun::ErrorSubscriber.new.report(
StandardError.new("test error"),
handled: true,
severity: "warning",
Expand All @@ -29,7 +35,8 @@ def test_tracking_exception_via_subscriber
source: "application"
)

Raygun.wait_for_futures
assert result && result.success?, "Expected success, got #{result.class}, #{result.inspect}"

assert_requested request_stub
end

Expand Down
7 changes: 5 additions & 2 deletions test/unit/resque_failure_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ class ResqueFailureTest < Raygun::UnitTest

def setup
super
Raygun.configuration.send_in_background = false

stub_request(:post, 'https://api.raygun.com/entries').to_return(status: 202)
fake_successful_entry
end

def test_failure_backend_appears_to_work
assert Resque::Failure::Raygun.new(
result = Resque::Failure::Raygun.new(
StandardError.new("Worker Problem"),
"TestWorker PID 123",
"super_important_jobs",
class: "SendCookies", args: [ "nik" ]
).save.success?
).save

assert result && result.success?, "Expected success, got #{result.inspect}"
end

end
7 changes: 5 additions & 2 deletions test/unit/sidekiq_failure_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ class SidekiqFailureTest < Raygun::UnitTest

def setup
super
Raygun.configuration.send_in_background = false

stub_request(:post, 'https://api.raygun.com/entries').to_return(status: 202)
fake_successful_entry
end

def test_failure_backend_appears_to_work
assert Raygun::SidekiqReporter.call(
response = Raygun::SidekiqReporter.call(
StandardError.new("Oh no! Your Sidekiq has failed!"),
{ sidekick_name: "robin" },
{} # config
).success?
)

assert response && response.success?, "Expected success, got #{response.class}: #{response.inspect}"
end

def test_we_are_in_sidekiqs_list_of_error_handlers
Expand Down

0 comments on commit 630ba91

Please sign in to comment.