-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrack_middleware_test.rb
More file actions
105 lines (83 loc) · 2.95 KB
/
rack_middleware_test.rb
File metadata and controls
105 lines (83 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: true
require "test_helper"
require "rack"
class RackMiddlewareTest < SpeedshopCloudwatchTest
def setup
super
@app = ->(env) { [200, {}, ["OK"]] }
end
def teardown
@middleware = nil
super
end
private
def configure_cloudwatch_for_test(**overrides)
Speedshop::Cloudwatch.configure do |config|
config.client = @client
config.interval = 60
config.logger = Logger.new(nil)
overrides.each { |k, v| config.public_send(:"#{k}=", v) }
end
end
def call_middleware_with_header(header_name, queue_start_ms_ago: 100)
@middleware = Speedshop::Cloudwatch::RackMiddleware.new(@app)
queue_start = (Time.now.to_f * 1000) - queue_start_ms_ago
env = {header_name => "t=#{queue_start}"}
@middleware.call(env)
end
public
def test_processes_request_with_x_request_start
status, _headers, _body = call_middleware_with_header("HTTP_X_REQUEST_START")
assert_equal 200, status
end
def test_processes_request_with_x_queue_start
status, _headers, _body = call_middleware_with_header("HTTP_X_QUEUE_START")
assert_equal 200, status
end
def test_handles_missing_queue_header
@middleware = Speedshop::Cloudwatch::RackMiddleware.new(@app)
env = {}
status, _headers, _body = @middleware.call(env)
assert_equal 200, status
end
def test_errors_during_metric_reporting_do_not_prevent_response
configure_cloudwatch_for_test
reporter = Speedshop::Cloudwatch.reporter
reporter.stub :report, ->(*) { raise "Metric reporting error" } do
status, _headers, _body = call_middleware_with_header("HTTP_X_REQUEST_START")
assert_equal 200, status
end
end
def test_uses_configured_namespace
Speedshop::Cloudwatch.configure do |config|
config.client = @client
config.interval = 60
config.namespaces[:rack] = "MyApp/Rack"
end
reporter = Speedshop::Cloudwatch.reporter
reported_kwargs = nil
reporter.stub :report, ->(**kwargs) { reported_kwargs = kwargs } do
call_middleware_with_header("HTTP_X_REQUEST_START")
end
assert_equal :RequestQueueTime, reported_kwargs[:metric]
end
def test_logs_error_when_collection_fails
error_logged = false
logger = Object.new
logger.define_singleton_method(:error) { |msg| error_logged = true if msg.include?("Failed to collect Rack metrics") }
logger.define_singleton_method(:debug) { |msg| }
logger.define_singleton_method(:info) { |msg| }
Speedshop::Cloudwatch.configure do |config|
config.client = @client
config.logger = logger
end
@middleware = Speedshop::Cloudwatch::RackMiddleware.new(@app)
reporter = Speedshop::Cloudwatch.reporter
reporter.stub :report, ->(*) { raise "boom" } do
env = {"HTTP_X_REQUEST_START" => "t=#{(Time.now.to_f * 1000) - 100}"}
status, _headers, _body = @middleware.call(env)
assert_equal 200, status
end
assert error_logged, "Expected error to be logged"
end
end