test/toxiproxy_test.rb in toxiproxy-0.0.2 vs test/toxiproxy_test.rb in toxiproxy-0.1.0

- old
+ new

@@ -22,10 +22,61 @@ assert_equal "localhost:3306", proxy.upstream assert_equal "test_mysql_master", proxy.name end + def test_enable_and_disable_proxy + with_tcpserver do |port| + proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_rubby_server") + listen_addr = proxy.listen + + Toxiproxy::Toxic.new( + name: 'latency', + proxy: proxy, + direction: :upstream, + attrs: {'latency' => 123} + ).enable + + proxy.disable + assert_proxy_unavailable proxy + proxy.enable + assert_proxy_available proxy + + latency = proxy.toxics(:upstream).find { |toxic| toxic.name == 'latency' } + assert_equal 123, latency['latency'] + assert latency.enabled? + + assert_equal listen_addr, proxy.listen + end + end + + def test_reset + with_tcpserver do |port| + proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_rubby_server") + listen_addr = proxy.listen + + proxy.disable + assert_proxy_unavailable proxy + + Toxiproxy::Toxic.new( + name: 'latency', + proxy: proxy, + direction: :upstream, + attrs: {'latency' => 125} + ).enable + + Toxiproxy.reset + assert_proxy_available proxy + + latency = proxy.toxics(:upstream).find { |toxic| toxic.name == 'latency' } + assert_equal 125, latency['latency'] + assert !latency.enabled? + + assert_equal listen_addr, proxy.listen + end + end + def test_take_endpoint_down with_tcpserver do |port| proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_rubby_server") listen_addr = proxy.listen @@ -67,10 +118,30 @@ assert_proxy_available proxy1 end end end + def test_disable_on_proxy_collection + with_tcpserver do |port1| + with_tcpserver do |port2| + proxy1 = Toxiproxy.create(upstream: "localhost:#{port1}", name: "test_proxy1") + proxy2 = Toxiproxy.create(upstream: "localhost:#{port2}", name: "test_proxy2") + + assert_proxy_available proxy2 + assert_proxy_available proxy1 + + Toxiproxy.all.disable + assert_proxy_unavailable proxy1 + assert_proxy_unavailable proxy2 + Toxiproxy.all.enable + + assert_proxy_available proxy2 + assert_proxy_available proxy1 + end + end + end + def test_select_from_toxiproxy_collection with_tcpserver do |port| Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") proxies = Toxiproxy.select { |p| p.upstream == "localhost:#{port}" } @@ -101,12 +172,10 @@ assert_instance_of Toxiproxy::Collection, proxies end end def test_apply_upstream_toxic - $before = Time.now - with_tcpserver(receive: true) do |port| proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") proxy.upstream(:latency, latency: 100).apply do before = Time.now @@ -156,9 +225,50 @@ passed = Time.now - before assert_in_delta passed, 0.200, 0.01 end + end + end + + def test_apply_toxics_to_collection + with_tcpserver(receive: true) do |port1| + with_tcpserver(receive: true) do |port2| + proxy1 = Toxiproxy.create(upstream: "localhost:#{port1}", name: "test_proxy1") + proxy2 = Toxiproxy.create(upstream: "localhost:#{port2}", name: "test_proxy2") + + Toxiproxy[/test_proxy/].upstream(:latency, latency: 100).downstream(:latency, latency: 100).apply do + before = Time.now + + socket = connect_to_proxy(proxy1) + socket.write("omg\n") + socket.flush + socket.gets + + passed = Time.now - before + + assert_in_delta passed, 0.200, 0.01 + + before = Time.now + + socket = connect_to_proxy(proxy2) + socket.write("omg\n") + socket.flush + socket.gets + + passed = Time.now - before + + assert_in_delta passed, 0.200, 0.01 + end + end + end + end + + def test_populate_creates_proxies + proxies = Toxiproxy.populate("./test/fixtures/toxiproxy.json") + + proxies.each do |proxy| + assert_proxy_available(proxy) end end private