@@ -606,6 +606,49 @@ struct BatchTests {
606606 await client . disconnect ( )
607607 await server . stop ( )
608608 }
609+
610+ @ Test ( " A batch whose send fails rejects withBatch and the returned tasks " )
611+ func sendFailure ( ) async throws {
612+ let transport = MockTransport ( )
613+ let client = Client ( name : " c " , version : " 1 " , configuration : . handshake )
614+
615+ let answer = Task {
616+ try await transport . waitForSentMessages ( )
617+ let data = try #require ( await transport . sentMessages . last ? . data ( using : . utf8 ) )
618+ let request = try JSONDecoder ( ) . decode ( Request < Initialize > . self , from : data )
619+ try await transport . queue (
620+ response : Initialize . response (
621+ id : request . id ,
622+ result : . init (
623+ protocolVersion : Version . latestHandshake , capabilities : . init ( ) ,
624+ serverInfo : . init ( name : " s " , version : " 1 " ) , instructions : nil ) ) )
625+ }
626+
627+ try await client . connect ( transport : transport )
628+ try await answer . value
629+
630+ await transport . setFailSend ( true )
631+
632+ nonisolated ( unsafe ) var added : [ Task < Ping . Result , Error > ] = [ ]
633+ await #expect ( throws : MCPError . self ) {
634+ try await client . withBatch { batch in
635+ added . append ( try await batch . addRequest ( Ping . request ( ) ) )
636+ added . append ( try await batch . addRequest ( Ping . request ( ) ) )
637+ }
638+ }
639+
640+ #expect ( added . count == 2 )
641+ for task in added {
642+ // A request that is never failed does not respond to cancellation.
643+ let result = await outcome ( within : . seconds ( 5 ) ) { try await task . value }
644+ #expect ( ! result . didTimeOut , " A request of a batch whose send failed never completed " )
645+ guard case . transportError = result . mcpError else {
646+ Issue . record ( " Expected the send's transport error, got \( result ) " )
647+ continue
648+ }
649+ }
650+ await client . disconnect ( )
651+ }
609652}
610653
611654@ Suite ( " Server replies " , . timeLimit ( . minutes ( 1 ) ) )
0 commit comments