|
4 | 4 | # Copyright, 2025, by Samuel Williams. |
5 | 5 |
|
6 | 6 | require "metrics/provider/async/http/server" |
7 | | -require "async/http/server" |
8 | | -require "async/http/endpoint" |
9 | | -require "async/http/client" |
10 | 7 | require "protocol/http/middleware" |
11 | | -require "sus/fixtures/async" |
| 8 | +require "sus/fixtures/async/http/server_context" |
12 | 9 |
|
13 | 10 | describe Async::HTTP::Server do |
14 | | - include Sus::Fixtures::Async::ReactorContext |
15 | | - |
16 | | - let(:endpoint) {Async::HTTP::Endpoint.parse("http://localhost:0")} |
| 11 | + include Sus::Fixtures::Async::HTTP::ServerContext |
17 | 12 |
|
18 | 13 | with "metrics provider" do |
19 | 14 | let(:app) do |
|
22 | 17 | end |
23 | 18 | end |
24 | 19 |
|
25 | | - let(:server) {subject.new(app, endpoint)} |
26 | | - |
27 | 20 | it "emits queue time metric when x-request-start header is present" do |
28 | | - # Start the server |
29 | | - server_task = server.run |
30 | | - bound_endpoint = server_task.wait_until_ready |
31 | | - |
32 | 21 | # Calculate a timestamp 100ms in the past (nginx format with 't=' prefix) |
33 | 22 | request_start = Process.clock_gettime(Process::CLOCK_REALTIME) - 0.1 |
34 | 23 |
|
|
39 | 28 | end |
40 | 29 |
|
41 | 30 | # Make a request with the x-request-start header |
42 | | - client = Async::HTTP::Client.new(bound_endpoint) |
43 | 31 | headers = [["x-request-start", "t=#{request_start}"]] |
44 | 32 | response = client.get("/", headers) |
45 | 33 |
|
46 | 34 | expect(response.status).to be == 200 |
47 | 35 | response.finish |
48 | | - |
49 | | - client.close |
50 | | - server_task.stop |
51 | 36 | end |
52 | 37 |
|
53 | 38 | it "handles nginx-style timestamp format (t=prefix)" do |
54 | | - server_task = server.run |
55 | | - bound_endpoint = server_task.wait_until_ready |
56 | | - |
57 | 39 | request_start = Process.clock_gettime(Process::CLOCK_REALTIME) - 0.05 |
58 | 40 |
|
59 | 41 | expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).to receive(:emit) do |value, tags:| |
60 | 42 | expect(value).to be > 0 |
61 | 43 | expect(value).to be < 1 |
62 | 44 | end |
63 | 45 |
|
64 | | - client = Async::HTTP::Client.new(bound_endpoint) |
65 | 46 | headers = [["x-request-start", "t=#{request_start}"]] |
66 | 47 | response = client.get("/", headers) |
67 | 48 |
|
68 | 49 | expect(response.status).to be == 200 |
69 | 50 | response.finish |
70 | | - |
71 | | - client.close |
72 | | - server_task.stop |
73 | 51 | end |
74 | 52 |
|
75 | 53 | it "handles plain Unix timestamp format" do |
76 | | - server_task = server.run |
77 | | - bound_endpoint = server_task.wait_until_ready |
78 | | - |
79 | 54 | request_start = Process.clock_gettime(Process::CLOCK_REALTIME) - 0.05 |
80 | 55 |
|
81 | 56 | expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).to receive(:emit) do |value, tags:| |
82 | 57 | expect(value).to be > 0 |
83 | 58 | expect(value).to be < 1 |
84 | 59 | end |
85 | 60 |
|
86 | | - client = Async::HTTP::Client.new(bound_endpoint) |
87 | 61 | headers = [["x-request-start", request_start.to_s]] |
88 | 62 | response = client.get("/", headers) |
89 | 63 |
|
90 | 64 | expect(response.status).to be == 200 |
91 | 65 | response.finish |
92 | | - |
93 | | - client.close |
94 | | - server_task.stop |
95 | 66 | end |
96 | 67 |
|
97 | 68 | it "does not emit queue time metric when x-request-start header is missing" do |
98 | | - server_task = server.run |
99 | | - bound_endpoint = server_task.wait_until_ready |
100 | | - |
101 | 69 | # Should not emit the queue time metric |
102 | 70 | expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).not.to receive(:emit) |
103 | 71 |
|
104 | | - client = Async::HTTP::Client.new(bound_endpoint) |
105 | 72 | response = client.get("/") |
106 | 73 |
|
107 | 74 | expect(response.status).to be == 200 |
108 | 75 | response.finish |
109 | | - |
110 | | - client.close |
111 | | - server_task.stop |
112 | 76 | end |
113 | 77 |
|
114 | 78 | it "ignores invalid timestamp formats" do |
115 | | - server_task = server.run |
116 | | - bound_endpoint = server_task.wait_until_ready |
117 | | - |
118 | 79 | # Should not emit the queue time metric for invalid timestamp |
119 | 80 | expect(Async::HTTP::Server::ASYNC_HTTP_SERVER_REQUEST_QUEUE_TIME).not.to receive(:emit) |
120 | 81 |
|
121 | | - client = Async::HTTP::Client.new(bound_endpoint) |
122 | 82 | headers = [["x-request-start", "invalid-timestamp"]] |
123 | 83 | response = client.get("/", headers) |
124 | 84 |
|
125 | 85 | expect(response.status).to be == 200 |
126 | 86 | response.finish |
127 | | - |
128 | | - client.close |
129 | | - server_task.stop |
130 | 87 | end |
131 | | - |
132 | 88 | end |
133 | 89 | end |
134 | 90 |
|
0 commit comments