connect-go icon indicating copy to clipboard operation
connect-go copied to clipboard

Cannot test correct handling of `serverStream.Send` error

Open edmondop opened this issue 4 months ago • 5 comments

Describe the bug

ServerStream.Send() can return an error, which should be correctly handled in implementation of Handler.

// ServerStream is the handler's view of a server streaming RPC.
//
// It's constructed as part of [Handler] invocation, but doesn't currently have
// an exported constructor.
type ServerStream[Res any] struct {
	conn StreamingHandlerConn
}
type StreamingHandlerConn interface {
	Spec() Spec
	Peer() Peer

	Receive(any) error
	RequestHeader() http.Header

	Send(any) error
	ResponseHeader() http.Header
	ResponseTrailer() http.Header
}

Unfortunately, the lack of a public constructor for ServerStream doesn't allow to mock StreamingHandlerConn, and test for the correct behavior of the handler. For example, if the handler needs to do some special cleanup or action when Send fails, today it's impossible to unit test this

Additional context I'll be happy to contribute back the fix, though it's unclear to me whether we want to make the Handler depends on an interface rather than the ServerStream struct

edmondop avatar Mar 26 '24 19:03 edmondop