Skip to content

Commit 4a2d6a8

Browse files
committed
fix: mock the connect method when using it in net-http test case
1 parent c3edc8d commit 4a2d6a8

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

instrumentation/net_http/test/opentelemetry/instrumentation/net/http/instrumentation_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@
151151
end
152152

153153
it 'does not create a span on connect when request ignored using a regexp' do
154+
# this works because http://bazqux.com is reachable site; try http://asdfasdfsef.com will fail
154155
uri = URI.parse('http://bazqux.com')
155156
http = Net::HTTP.new(uri.host, uri.port)
156157
http.send(:connect)
@@ -169,7 +170,16 @@
169170
it 'creates a span on connect for a non-ignored request' do
170171
uri = URI.parse('http://example.com')
171172
http = Net::HTTP.new(uri.host, uri.port)
172-
http.send(:connect)
173+
174+
fake_socket = Object.new
175+
def fake_socket.setsockopt(*args); end
176+
def fake_socket.close; end
177+
178+
# Replace the TCP socket creation with our fake socket
179+
TCPSocket.stub(:open, fake_socket) do
180+
http.send(:connect)
181+
end
182+
173183
http.send(:do_finish)
174184
_(exporter.finished_spans.size).must_equal 1
175185
_(span.name).must_equal('connect')
@@ -199,6 +209,10 @@
199209
OpenTelemetry::Common::Utilities.untraced do
200210
uri = URI.parse('http://example.com/body')
201211
http = Net::HTTP.new(uri.host, uri.port)
212+
213+
# Mock the connect
214+
http.define_singleton_method(:connect) { true }
215+
202216
http.send(:connect)
203217
http.send(:do_finish)
204218
end

0 commit comments

Comments
 (0)