@@ -42,10 +42,10 @@ struct LambdaRuntimeClientTests {
4242                . success( ( self . requestId,  self . event) ) 
4343            } 
4444
45-             func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < Void ,  ProcessResponseError >  { 
45+             func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < String ? ,  ProcessResponseError >  { 
4646                #expect( self . requestId ==  requestId) 
4747                #expect( self . event ==  response) 
48-                 return  . success( ( ) ) 
48+                 return  . success( nil ) 
4949            } 
5050
5151            func  processError( requestId:  String ,  error:  ErrorResponse )  ->  Result < Void ,  ProcessErrorError >  { 
@@ -102,9 +102,9 @@ struct LambdaRuntimeClientTests {
102102            . success( ( self . requestId,  self . event) ) 
103103        } 
104104
105-         func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < Void ,  ProcessResponseError >  { 
105+         func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < String ? ,  ProcessResponseError >  { 
106106            #expect( self . requestId ==  requestId) 
107-             return  . success( ( ) ) 
107+             return  . success( nil ) 
108108        } 
109109
110110        mutating  func  captureHeaders( _ headers:  HTTPHeaders )  { 
@@ -197,10 +197,10 @@ struct LambdaRuntimeClientTests {
197197                . success( ( self . requestId,  self . event) ) 
198198            } 
199199
200-             func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < Void ,  ProcessResponseError >  { 
200+             func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < String ? ,  ProcessResponseError >  { 
201201                #expect( self . requestId ==  requestId) 
202202                #expect( self . event ==  response) 
203-                 return  . success( ( ) ) 
203+                 return  . success( nil ) 
204204            } 
205205
206206            func  processError( requestId:  String ,  error:  ErrorResponse )  ->  Result < Void ,  ProcessErrorError >  { 
@@ -239,31 +239,56 @@ struct LambdaRuntimeClientTests {
239239        } 
240240    } 
241241
242-     @Test ( " Server closing the connection when waiting for next invocation throws an error " )  
243-     func  testChannelCloseFutureWithWaitingForNextInvocation( )  async  throws  { 
244-         struct  DisconnectBehavior :  LambdaServerBehavior  { 
245-             func  getInvocation( )  ->  GetInvocationResult  { 
246-                 // Return "disconnect" to trigger server closing the connection
247-                 . success( ( " disconnect " ,  " 0 " ) ) 
248-             } 
242+     struct  DisconnectAfterSendingResponseBehavior :  LambdaServerBehavior  { 
243+         func  getInvocation( )  ->  GetInvocationResult  { 
244+             . success( ( UUID ( ) . uuidString,  " hello " ) ) 
245+         } 
249246
250-             func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < Void ,  ProcessResponseError >  { 
251-                 Issue . record ( " should not process response " ) 
252-                 return  . failure( . internalServerError) 
253-             } 
247+         func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < String ? ,  ProcessResponseError >  { 
248+             // Return "disconnect" to trigger server closing the connection
249+             // after having accepted a response
250+             . success( " delayed-disconnect " ) 
251+         } 
254252
255-              func  processError( requestId:  String ,  error:  ErrorResponse )  ->  Result < Void ,  ProcessErrorError >  { 
256-                  Issue . record ( " should not report error " ) 
257-                  return  . failure( . internalServerError) 
258-              } 
253+         func  processError( requestId:  String ,  error:  ErrorResponse )  ->  Result < Void ,  ProcessErrorError >  { 
254+             Issue . record ( " should not report error " ) 
255+             return  . failure( . internalServerError) 
256+         } 
259257
260-             func  processInitError( error:  ErrorResponse )  ->  Result < Void ,  ProcessErrorError >  { 
261-                 Issue . record ( " should not report init error " ) 
262-                 return  . failure( . internalServerError) 
263-             } 
258+         func  processInitError( error:  ErrorResponse )  ->  Result < Void ,  ProcessErrorError >  { 
259+             Issue . record ( " should not report init error " ) 
260+             return  . failure( . internalServerError) 
261+         } 
262+     } 
263+ 
264+     struct  DisconnectBehavior :  LambdaServerBehavior  { 
265+         func  getInvocation( )  ->  GetInvocationResult  { 
266+             // Return "disconnect" to trigger server closing the connection
267+             . success( ( " disconnect " ,  " 0 " ) ) 
264268        } 
265269
266-         try await  withMockServer ( behaviour:  DisconnectBehavior ( ) )  {  port in 
270+         func  processResponse( requestId:  String ,  response:  String ? )  ->  Result < String ? ,  ProcessResponseError >  { 
271+             Issue . record ( " should not process response " ) 
272+             return  . failure( . internalServerError) 
273+         } 
274+ 
275+         func  processError( requestId:  String ,  error:  ErrorResponse )  ->  Result < Void ,  ProcessErrorError >  { 
276+             Issue . record ( " should not report error " ) 
277+             return  . failure( . internalServerError) 
278+         } 
279+ 
280+         func  processInitError( error:  ErrorResponse )  ->  Result < Void ,  ProcessErrorError >  { 
281+             Issue . record ( " should not report init error " ) 
282+             return  . failure( . internalServerError) 
283+         } 
284+     } 
285+ 
286+     @Test (  
287+         " Server closing the connection when waiting for next invocation throws an error " ,  
288+         arguments:  [ DisconnectAfterSendingResponseBehavior ( ) ,  DisconnectBehavior ( ) ]  as  [ any  LambdaServerBehavior ]  
289+     )  
290+     func  testChannelCloseFutureWithWaitingForNextInvocation( behavior:  LambdaServerBehavior )  async  throws  { 
291+         try await  withMockServer ( behaviour:  behavior)  {  port in 
267292            let  configuration  =  LambdaRuntimeClient . Configuration ( ip:  " 127.0.0.1 " ,  port:  port) 
268293
269294            try await  LambdaRuntimeClient . withRuntimeClient ( 
@@ -273,7 +298,12 @@ struct LambdaRuntimeClientTests {
273298            )  {  runtimeClient in 
274299                do  { 
275300                    // This should fail when server closes connection
301+                     let  ( _,  writer)  =  try await  runtimeClient. nextInvocation ( ) 
302+                     let  response  =  ByteBuffer ( string:  " hello " ) 
303+                     try await  writer. writeAndFinish ( response) 
304+ 
276305                    let  _ =  try await  runtimeClient. nextInvocation ( ) 
306+ 
277307                    Issue . record ( " Expected connection error but got successful invocation " ) 
278308
279309                }  catch  let  errorLambdaRuntimeError  { 
0 commit comments