Skip to content

Commit 47783ac

Browse files
authored
Invalidate memorized object http on host changes (#43)
Currently http is cached and could be never changed. Make sure there is possible to update toxiproxy client to different server, invalidate cache for http.
1 parent eaf9791 commit 47783ac

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

lib/toxiproxy.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def self.all
6868
# Sets the toxiproxy host to use.
6969
def self.host=(host)
7070
@uri = host.is_a?(::URI) ? host : ::URI.parse(host)
71+
reset_http_client!
72+
@uri
7173
end
7274

7375
# Convenience method to create a proxy.
@@ -223,6 +225,15 @@ def toxics
223225
}
224226
end
225227

228+
def self.reset_http_client!
229+
if defined? @http
230+
@http.finish() if @http && @http.started?
231+
@http = nil
232+
end
233+
234+
@http
235+
end
236+
226237
private
227238

228239
def self.http_request(request)

test/toxiproxy_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,15 @@ def test_whitelisting_webmock_does_not_override_other_configuration
453453
end
454454
end
455455

456+
def test_invalidate_cache_http_on_host
457+
old_value = Toxiproxy.uri
458+
assert_equal 8474, Toxiproxy.http.port
459+
Toxiproxy.host = "http://127.0.0.1:8475"
460+
assert_equal 8475, Toxiproxy.http.port
461+
ensure
462+
Toxiproxy.host = old_value
463+
end
464+
456465
private
457466

458467
def with_webmock_enabled

0 commit comments

Comments
 (0)