Skip to content

Commit 71325c7

Browse files
committed
fix(driver-specs/runner): simplify HTTP request processing
1 parent eb29f02 commit 71325c7

2 files changed

Lines changed: 14 additions & 33 deletions

File tree

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: placeos-driver
2-
version: 7.13.2
2+
version: 7.13.3
33

44
dependencies:
55
action-controller:

src/placeos-driver/driver-specs/runner.cr

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,10 @@ class DriverSpecs
263263
@write_mutex = Mutex.new
264264
@event_mutex = Mutex.new
265265

266-
@received_http = [] of MockHTTP
267-
@expected_http = [] of Channel(MockHTTP)
266+
@received_http = Channel(MockHTTP).new(5)
268267
@http_server = HTTP::Server.new do |context|
269268
request = MockHTTP.new(context)
270-
@event_mutex.synchronize do
271-
if @expected_http.empty?
272-
@received_http << request
273-
else
274-
@expected_http.shift.send(request)
275-
end
276-
end
269+
@received_http.send(request)
277270
request.wait_for_data
278271
end
279272

@@ -639,41 +632,29 @@ class DriverSpecs
639632
#
640633
# for an example of how this works see [an existing driver](https://github.com/PlaceOS/drivers/blob/master/drivers/message_media/sms_spec.cr#L10-L25)
641634
def expect_http_request(timeout = 1.seconds, &)
642-
channel = nil
643-
644-
@event_mutex.synchronize do
645-
if @received_http.empty?
646-
channel = Channel(MockHTTP).new(1)
647-
@expected_http << channel
648-
end
635+
mock_http = select
636+
when temp_http = @received_http.receive
637+
temp_http
638+
when timeout(timeout)
639+
puts "level=ERROR : timeout waiting for expected HTTP request".colorize(:red)
640+
raise "timeout waiting for expected HTTP request"
649641
end
650642

651-
mock_http = if channel
652-
select
653-
when temp_http = channel.receive
654-
temp_http
655-
when timeout(timeout)
656-
puts "level=ERROR : timeout waiting for expected HTTP request".colorize(:red)
657-
@event_mutex.synchronize { @expected_http.delete(channel) }
658-
raise "timeout waiting for expected HTTP request"
659-
end
660-
else
661-
@event_mutex.synchronize { @received_http.shift }
662-
end
663-
664-
puts "-> expected HTTP request received"
643+
req = mock_http.context.request
665644

666645
# Make a copy of the body for debugging later
667-
io = mock_http.context.request.body
646+
io = req.body
668647
request_body = begin
669648
io ? String.new(io.peek || Bytes.new(0)) : ""
670649
rescue
671650
io ? io.peek.inspect : ""
672651
end
673652

653+
puts "-> received HTTP request: #{req.method} #{req.path}"
654+
674655
# Process the request
675656
begin
676-
yield mock_http.context.request, mock_http.context.response
657+
yield req, mock_http.context.response
677658
rescue e
678659
puts "-> ------"
679660
puts " unhandled error processing request:\n#{mock_http.context.request.inspect}"

0 commit comments

Comments
 (0)