newrelic / newrelic-ruby-agent

New Relic RPM Ruby Agent
https://docs.newrelic.com/docs/apm/agents/ruby-agent/getting-started/introduction-new-relic-ruby/
Apache License 2.0
1.2k stars 599 forks source link

Add Tests for Falcon #2384

Open hannahramadan opened 10 months ago

hannahramadan commented 10 months ago

Tests are needed to show the agent correctly identifying the presence of a Falcon webserver.

Additional context

Tests were attempted at the initial instrumentation of Falcon #2383. The async nature of Falcon makes it difficult to interact with the webserver during tests. One idea is to build a simple test app that uses Falcon. Another is to build a Falcon server (similar to Unircorn's tests). We got as far as running the tests inside Falcon's async block, but weren't able to stop the server, which would allow the test to finish. Otherwise, the process sits and waits. Here is that draft work:

require 'async'
require 'async/http/endpoint'
require 'falcon'

class WebSocketApp
  def self.call(env)
    ::Rack::Builder.app(
      lambda { [200, {'Content-Type' => 'text/html'}, ['OK']] }
    )
  end
end

class FalconTest < Minitest::Test
  include MultiverseHelpers
  attr_accessor :server

  def setup
    NewRelic::Agent.stubs(:logger).returns(NewRelic::Agent::MemoryLogger.new)
    Async do
      websocket_endpoint = Async::HTTP::Endpoint.parse('http://127.0.0.1:3000')
      # An app and endpoint are needed to start a Falcon server
      app = Falcon::Server.middleware(WebSocketApp)
      server = Falcon::Server.new(WebSocketApp, websocket_endpoint)
      server.run

      trigger_agent_reconnect

      # Tests initially added inside the setup to since the server needs to be running
      assert_logged('Dispatcher: falcon')
      assert_logged('Deferring startup of agent reporting thread because falcon may fork.')
      # Once the server starts and this async block runs, other processes pause and we're left with a running server
      # We need a way to kill the server here so that the tests can complete.
    ensure
      File.unlink('log/newrelic_agent.log')
    end
  end

  def assert_logged(expected)
    logger = NewRelic::Agent.logger

    flattened = logger.messages.flatten

    found = flattened.any? { |msg| msg.to_s.include?(expected) }

    assert(found, "Didn't see message '#{expected}'")
  end
end
workato-integration[bot] commented 10 months ago

https://new-relic.atlassian.net/browse/NR-211507