Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Make Net::HTTP Deterministic #1390

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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 @@ -16,18 +16,24 @@

before do
exporter.reset
stub_request(:get, 'http://example.com/success').to_return(status: 200)
stub_request(:post, 'http://example.com/failure').to_return(status: 500)
stub_request(:get, 'https://example.com/timeout').to_timeout

# this is currently a noop but this will future proof the test
# ORDER MATTERS!
# Reinstalling the instrumentation results in overriding the Webmock patches, which results in real HTTP requests being made.
@orig_propagation = OpenTelemetry.propagation
propagator = OpenTelemetry::Trace::Propagation::TraceContext.text_map_propagator
OpenTelemetry.propagation = propagator
instrumentation.install

WebMock.enable!
WebMock.disable_net_connect!
stub_request(:get, 'http://example.com/success').to_return(status: 200)
stub_request(:post, 'http://example.com/failure').to_return(status: 500)
stub_request(:get, 'https://example.com/timeout').to_timeout
end

after do
WebMock.enable_net_connect!
WebMock.disable!
# Force re-install of instrumentation
instrumentation.instance_variable_set(:@installed, false)

Expand Down Expand Up @@ -225,8 +231,6 @@
_(span.name).must_equal 'connect'
_(span.attributes['net.peer.name']).must_equal('localhost')
_(span.attributes['net.peer.port']).wont_be_nil
ensure
WebMock.disable_net_connect!
end

it 'captures errors' do
Expand All @@ -246,8 +250,6 @@
_(span_event.name).must_equal 'exception'
_(span_event.attributes['exception.type']).wont_be_nil
_(span_event.attributes['exception.message']).must_match(/Failed to open TCP connection to invalid.com:99999/)
ensure
WebMock.disable_net_connect!
end

it 'emits an HTTP CONNECT span when connecting through an SSL proxy' do
Expand All @@ -270,8 +272,6 @@
_(span.kind).must_equal(:client)
_(span.attributes['net.peer.name']).must_equal('localhost')
_(span.attributes['net.peer.port']).must_equal(443)
ensure
WebMock.disable_net_connect!
end

it 'emits a "connect" span when connecting through an non-ssl proxy' do
Expand All @@ -294,8 +294,6 @@
_(span.kind).must_equal(:internal)
_(span.attributes['net.peer.name']).must_equal('localhost')
_(span.attributes['net.peer.port']).must_equal(443)
ensure
WebMock.disable_net_connect!
end
end
end
Loading