@@ -10,7 +10,7 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
1010 done: false ,
1111 from: nil ,
1212 grpc_stream: build ( :client_stream ) ,
13- responses: [ ] ,
13+ responses: :queue . new ( ) ,
1414 compressor: nil ,
1515 send_headers_or_trailers: false
1616 }
@@ -41,7 +41,7 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
4141 state: state ,
4242 data: { _ , _ , full_message }
4343 } do
44- expected_response_message = build ( :hello_reply_rpc )
44+ expected_response_message = { :ok , build ( :hello_reply_rpc ) }
4545
4646 response =
4747 StreamResponseProcess . handle_call (
@@ -52,35 +52,32 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
5252
5353 assert { :reply , :ok , new_state , { :continue , :produce_response } } = response
5454 assert new_state . buffer == << >>
55- assert [ { :ok , response_message } ] = new_state . responses
56- assert expected_response_message == response_message
55+ assert :queue . to_list ( new_state . responses ) == [ expected_response_message ]
5756 end
5857
5958 test "append incoming message to existing buffer" , % { state: state , data: { part1 , part2 , _ } } do
6059 state = % { state | buffer: part1 }
61- expected_response_message = build ( :hello_reply_rpc )
60+ expected_response_message = { :ok , build ( :hello_reply_rpc ) }
6261
6362 response =
6463 StreamResponseProcess . handle_call ( { :consume_response , { :data , part2 } } , self ( ) , state )
6564
6665 assert { :reply , :ok , new_state , { :continue , :produce_response } } = response
6766 assert new_state . buffer == << >>
68- assert [ { :ok , response_message } ] = new_state . responses
69- assert expected_response_message == response_message
67+ assert :queue . to_list ( new_state . responses ) == [ expected_response_message ]
7068 end
7169
7270 test "decode message and put rest on buffer" , % { state: state , data: { _ , _ , full } } do
7371 extra_data = << 0 , 1 , 2 >>
7472 data = full <> extra_data
75- expected_response_message = build ( :hello_reply_rpc )
73+ expected_response_message = { :ok , build ( :hello_reply_rpc ) }
7674
7775 response =
7876 StreamResponseProcess . handle_call ( { :consume_response , { :data , data } } , self ( ) , state )
7977
8078 assert { :reply , :ok , new_state , { :continue , :produce_response } } = response
8179 assert new_state . buffer == extra_data
82- assert [ { :ok , response_message } ] = new_state . responses
83- assert expected_response_message == response_message
80+ assert :queue . to_list ( new_state . responses ) == [ expected_response_message ]
8481 end
8582 end
8683
@@ -106,9 +103,10 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
106103 state
107104 )
108105
106+ expected_error = { :error , % GRPC.RPCError { message: "Internal Server Error" , status: 2 } }
107+
109108 assert { :reply , :ok , new_state , { :continue , :produce_response } } = response
110- assert [ { :error , error } ] = new_state . responses
111- assert % GRPC.RPCError { message: "Internal Server Error" , status: 2 } == error
109+ assert :queue . to_list ( new_state . responses ) == [ expected_error ]
112110 end ,
113111 do: [
114112 { % { type: :headers , is_header_enabled: false } } ,
@@ -139,17 +137,10 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
139137 state
140138 )
141139
142- assert { :reply , :ok , new_state , { :continue , :produce_response } } = response
143- assert [ { type_response , response_headers } ] = new_state . responses
144- assert type == type_response
140+ expected_response = { type , Map . new ( headers ) }
145141
146- assert % {
147- "content-length" => "0" ,
148- "content-type" => "application/grpc+proto" ,
149- "grpc-message" => "" ,
150- "grpc-status" => "0" ,
151- "server" => "Cowboy"
152- } == response_headers
142+ assert { :reply , :ok , new_state , { :continue , :produce_response } } = response
143+ assert :queue . to_list ( new_state . responses ) == [ expected_response ]
153144 end ,
154145 do: [ { :headers } , { :trailers } ]
155146 )
@@ -174,7 +165,7 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
174165 )
175166
176167 assert { :reply , :ok , new_state , { :continue , :produce_response } } = response
177- assert [ ] == new_state . responses
168+ assert :queue . is_empty ( new_state . responses )
178169 end ,
179170 do: [ { :headers } , { :trailers } ]
180171 )
@@ -238,8 +229,7 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
238229 )
239230
240231 assert { :reply , :ok , new_state , { :continue , :produce_response } } = response
241- assert [ response_error ] = new_state . responses
242- assert response_error == error
232+ assert :queue . to_list ( new_state . responses ) == [ error ]
243233 end
244234 end
245235
@@ -282,11 +272,11 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
282272 end
283273
284274 test "send response to caller when there are responses in the queue" , % { state: state } do
285- state = % { state | from: { self ( ) , :tag } , done: false , responses: [ 1 , 2 ] }
275+ state = % { state | from: { self ( ) , :tag } , done: false , responses: :queue . from_list ( [ 1 , 2 ] ) }
286276 { :noreply , new_state } = StreamResponseProcess . handle_continue ( :produce_response , state )
287277 % { from: from , responses: responses } = new_state
288- assert nil == from
289- assert [ 2 ] == responses
278+ assert is_nil ( from )
279+ assert :queue . to_list ( responses ) == [ 2 ]
290280 assert_receive { :tag , 1 }
291281 end
292282 end
@@ -321,6 +311,27 @@ defmodule GRPC.Client.Adapters.Mint.StreamResponseProcessTest do
321311 assert { :ok , build ( :hello_reply_rpc ) } == data
322312 end
323313
314+ test "preserves response messages order" , % { pid: pid } do
315+ hello_luis =
316+ << 0 , 0 , 0 , 0 , 12 , 10 , 10 , 72 , 101 , 108 , 108 , 111 , 32 , 76 , 117 , 105 , 115 >>
317+
318+ bye_luis =
319+ << 0 , 0 , 0 , 0 , 10 , 10 , 8 , 66 , 121 , 101 , 32 , 76 , 117 , 105 , 115 >>
320+
321+ stream = StreamResponseProcess . build_stream ( pid )
322+ StreamResponseProcess . consume ( pid , :data , hello_luis )
323+ StreamResponseProcess . consume ( pid , :data , bye_luis )
324+ StreamResponseProcess . done ( pid )
325+
326+ expected_elements =
327+ [
328+ ok: build ( :hello_reply_rpc ) ,
329+ ok: build ( :bye_reply_rpc )
330+ ]
331+
332+ assert Enum . to_list ( stream ) == expected_elements
333+ end
334+
324335 test_with_params (
325336 "emits headers to stream" ,
326337 % { pid: pid } ,
0 commit comments