require "helper" require "twirl/instrumentation/statsd" class StatsdInstrumentationTest < Minitest::Test def setup @statsd = Statsd.new @socket = FakeUDPSocket.new @cluster = build(:cluster, { instrumenter: ActiveSupport::Notifications, queues: %w(testing), }) Thread.current[:statsd_socket] = @socket Twirl::Instrumentation::StatsdSubscriber.client = @statsd end def teardown Thread.current[:statsd_socket] = nil Twirl::Instrumentation::StatsdSubscriber.client = nil end def test_set @cluster.set "testing", "data" assert_timer @socket, "twirl.op_set" assert_timer @socket, "twirl.queue_testing_op_set" assert_counter @socket, "twirl.bytes_op_set" assert_counter @socket, "twirl.bytes_queue_testing_op_set" end def test_get @cluster.set "testing", "data" @cluster.get "testing" assert_timer @socket, "twirl.op_get" assert_timer @socket, "twirl.queue_testing_op_get" assert_counter @socket, "twirl.bytes_op_get" assert_counter @socket, "twirl.bytes_queue_testing_op_get" end def test_item_close @cluster.set "testing", "data" item = @cluster.reserve("testing") item.close assert_timer @socket, "twirl.op_item_close" assert_timer @socket, "twirl.queue_testing_op_item_close" end def test_item_abort @cluster.set "testing", "data" item = @cluster.reserve("testing") item.abort assert_timer @socket, "twirl.op_item_abort" assert_timer @socket, "twirl.queue_testing_op_item_abort" end def test_reserve @cluster.set "testing", "data" @cluster.reserve "testing" assert_timer @socket, "twirl.op_reserve" assert_timer @socket, "twirl.queue_testing_op_reserve" assert_counter @socket, "twirl.bytes_op_reserve" assert_counter @socket, "twirl.bytes_queue_testing_op_reserve" end def test_peek @cluster.set "testing", "data" @cluster.peek "testing" assert_timer @socket, "twirl.op_peek" assert_timer @socket, "twirl.queue_testing_op_peek" assert_counter @socket, "twirl.bytes_op_peek" assert_counter @socket, "twirl.bytes_queue_testing_op_peek" end def test_delete @cluster.delete "testing" assert_timer @socket, "twirl.op_delete" assert_timer @socket, "twirl.queue_testing_op_delete" end def test_flush @cluster.flush "testing" assert_timer @socket, "twirl.op_flush" assert_timer @socket, "twirl.queue_testing_op_flush" end def test_flush_all @cluster.flush_all assert_timer @socket, "twirl.op_flush_all" end def test_version @cluster.version assert_timer @socket, "twirl.op_version" end def test_ping @cluster.ping assert_timer @socket, "twirl.op_ping" end def test_quit @cluster.quit assert_timer @socket, "twirl.op_quit" end def test_stats @cluster.stats assert_timer @socket, "twirl.op_stats" end def test_disconnect @cluster.disconnect assert_timer @socket, "twirl.op_disconnect" end def test_rotate cluster = build(:cluster, { commands_per_client: 1, instrumenter: ActiveSupport::Notifications, }) # set data to avoid nil rotation cluster[0].set "testing", "data" cluster[1].set "testing", "data" cluster.get("testing") cluster.get("testing") assert_counter @socket, "twirl.op_rotate" end def test_get_nil_instruments_rotate @cluster.get("testing") @cluster.get("testing") assert_counter @socket, "twirl.op_rotate" end def test_get_retries_instrumented @cluster.get("testing") assert_no_timer @socket, "twirl.retries_op_get" # force all clients to error @cluster.each do |client| def client.get(*args) raise KJess::NetworkError end end begin @cluster.get("testing") flunk "Should raise error and not get here." rescue KJess::NetworkError assert_counter @socket, "twirl.retries_op_get" end end def test_set_retries_instrumented @cluster.set("testing", "data") assert_no_timer @socket, "twirl.retries_op_set" # force all clients to error @cluster.each do |client| def client.set(*args) raise KJess::NetworkError end end begin @cluster.set("testing", "data") flunk "Should raise error and not get here." rescue KJess::NetworkError assert_counter @socket, "twirl.retries_op_set" end end end