test/toxiproxy_test.rb in toxiproxy-0.1.4 vs test/toxiproxy_test.rb in toxiproxy-1.0.0

- old
+ new

@@ -29,57 +29,62 @@ assert !Toxiproxy.running?, 'toxiproxy should not be running' ensure Toxiproxy.host = Toxiproxy::DEFAULT_URI end - def test_enable_and_disable_proxy + def test_enable_and_disable_proxy_with_toxic 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 + Toxiproxy::Toxic.new(type: 'latency', attributes: { latency: 123 }, proxy: proxy).save 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? + latency = proxy.toxics.find { |toxic| toxic.name == 'latency_downstream' } + assert_equal 123, latency.attributes['latency'] assert_equal listen_addr, proxy.listen end end + def test_delete_toxic + with_tcpserver do |port| + proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_rubby_server") + listen_addr = proxy.listen + + latency = Toxiproxy::Toxic.new(type: 'latency', attributes: { latency: 123 }, proxy: proxy).save + + assert_proxy_available proxy + + latency = proxy.toxics.find { |toxic| toxic.name == 'latency_downstream' } + assert_equal 123, latency.attributes['latency'] + + latency.destroy + + assert proxy.toxics.empty? + 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::Toxic.new(type: 'latency', attributes: { latency: 123 }, proxy: proxy).save Toxiproxy.reset assert_proxy_available proxy - latency = proxy.toxics(:upstream).find { |toxic| toxic.name == 'latency' } - assert_equal 125, latency['latency'] - assert !latency.enabled? - + assert proxy.toxics.empty? assert_equal listen_addr, proxy.listen end end def test_take_endpoint_down @@ -102,11 +107,11 @@ Toxiproxy[:does_not_exist] end end def test_proxies_all_returns_proxy_collection - assert_instance_of Toxiproxy::Collection, Toxiproxy.all + assert_instance_of Toxiproxy::ProxyCollection, Toxiproxy.all end def test_down_on_proxy_collection_disables_entire_collection with_tcpserver do |port1| with_tcpserver do |port2| @@ -152,33 +157,33 @@ Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") proxies = Toxiproxy.select { |p| p.upstream == "localhost:#{port}" } assert_equal 1, proxies.size - assert_instance_of Toxiproxy::Collection, proxies + assert_instance_of Toxiproxy::ProxyCollection, proxies end end def test_grep_returns_toxiproxy_collection with_tcpserver do |port| Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") proxies = Toxiproxy.grep(/\Atest/) assert_equal 1, proxies.size - assert_instance_of Toxiproxy::Collection, proxies + assert_instance_of Toxiproxy::ProxyCollection, proxies end end def test_indexing_allows_regexp with_tcpserver do |port| Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") proxies = Toxiproxy[/\Atest/] assert_equal 1, proxies.size - assert_instance_of Toxiproxy::Collection, proxies + assert_instance_of Toxiproxy::ProxyCollection, proxies end end def test_apply_upstream_toxic with_tcpserver(receive: true) do |port| @@ -216,10 +221,28 @@ assert_in_delta passed, 0.100, 0.01 end end end + def test_toxic_applies_a_downstream_toxic + with_tcpserver(receive: true) do |port| + proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") + + proxy.toxic(:latency, latency: 100).apply do + latency = proxy.toxics.find { |toxic| toxic.name == 'latency_downstream' } + + assert_equal 100, latency.attributes['latency'] + assert_equal 'downstream', latency.stream + end + end + end + + def test_toxic_default_name_is_type_and_stream + toxic = Toxiproxy::Toxic.new(type: "latency", stream: "downstream") + assert_equal "latency_downstream", toxic.name + end + def test_apply_prolong_toxics with_tcpserver(receive: true) do |port| proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") proxy.upstream(:latency, latency: 100).downstream(:latency, latency: 100).apply do @@ -357,9 +380,51 @@ assert_proxy_available(proxies2.first) end def test_running_helper assert_equal true, Toxiproxy.running? + end + + def test_version + assert_instance_of String, Toxiproxy.version + end + + def test_multiple_of_same_toxic_type + with_tcpserver(receive: true) do |port| + proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") + proxy.toxic(:latency, latency: 100).toxic(:latency, latency: 100, name: "second_latency_downstream").apply do + before = Time.now + + socket = connect_to_proxy(proxy) + socket.write("omg\n") + socket.flush + socket.gets + + passed = Time.now - before + + assert_in_delta passed, 0.200, 0.01 + end + end + end + + def test_multiple_of_same_toxic_type_with_same_name + with_tcpserver(receive: true) do |port| + proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_proxy") + + assert_raises ArgumentError do + proxy.toxic(:latency, latency: 100).toxic(:latency, latency: 100).apply { } + end + end + end + + def test_invalid_direction + with_tcpserver(receive: true) do |port| + proxy = Toxiproxy.create(upstream: "localhost:#{port}", name: "test_rubby_server") + + assert_raises Toxiproxy::InvalidToxic do + Toxiproxy::Toxic.new(type: 'latency', attributes: { latency: 123 }, proxy: proxy, stream: 'lolstream').save + end + end end private def assert_proxy_available(proxy)