Skip to content

Commit 529c3a9

Browse files
committed
Fix race conditions in trailer tests.
1 parent d5fecf9 commit 529c3a9

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

fixtures/async/http/a_protocol.rb

+37-16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Copyright, 2020, by Igor Sidorov.
66

77
require 'async'
8+
require 'async/variable'
89
require 'async/clock'
910
require 'async/http/client'
1011
require 'async/http/server'
@@ -128,29 +129,22 @@ module HTTP
128129
end
129130
end
130131

131-
with 'with trailer' do
132+
with 'with request trailer' do
133+
let(:request_received) {Async::Variable.new}
134+
132135
let(:app) do
133136
::Protocol::HTTP::Middleware.for do |request|
134137
if trailer = request.headers['trailer']
135138
expect(request.headers).not.to have_keys('etag')
139+
140+
request_received.value = true
136141
request.finish
142+
137143
expect(request.headers).to have_keys('etag')
138144

139145
::Protocol::HTTP::Response[200, [], "request trailer"]
140146
else
141-
headers = ::Protocol::HTTP::Headers.new
142-
headers.add('trailer', 'etag')
143-
144-
body = Async::HTTP::Body::Writable.new
145-
146-
Async do |task|
147-
body.write("response trailer")
148-
sleep(0.01)
149-
headers.add('etag', 'abcd')
150-
body.close
151-
end
152-
153-
::Protocol::HTTP::Response[200, headers, body]
147+
::Protocol::HTTP::Response[400, headers, body]
154148
end
155149
end
156150
end
@@ -164,16 +158,41 @@ module HTTP
164158

165159
Async do |task|
166160
body.write("Hello")
167-
sleep(0.01)
161+
162+
request_received.wait
168163
headers.add('etag', 'abcd')
164+
169165
body.close
170166
end
171167

172168
response = client.post("/", headers, body)
173169
expect(response.read).to be == "request trailer"
174-
175170
expect(response).to be(:success?)
176171
end
172+
end
173+
174+
with "with response trailer" do
175+
let(:response_received) {Async::Variable.new}
176+
177+
let(:app) do
178+
::Protocol::HTTP::Middleware.for do |request|
179+
headers = ::Protocol::HTTP::Headers.new
180+
headers.add('trailer', 'etag')
181+
182+
body = Async::HTTP::Body::Writable.new
183+
184+
Async do |task|
185+
body.write("response trailer")
186+
187+
response_received.wait
188+
headers.add('etag', 'abcd')
189+
190+
body.close
191+
end
192+
193+
::Protocol::HTTP::Response[200, headers, body]
194+
end
195+
end
177196

178197
it "can receive response trailer" do
179198
skip "Protocol does not support trailers!" unless subject.bidirectional?
@@ -183,6 +202,8 @@ module HTTP
183202
headers = response.headers
184203
expect(headers).not.to have_keys('etag')
185204

205+
response_received.value = true
206+
186207
expect(response.read).to be == "response trailer"
187208
expect(response).to be(:success?)
188209

0 commit comments

Comments
 (0)