github.com/letsencrypt/boulder@v0.20251208.0/mocks/grpc.go (about)

     1  package mocks
     2  
     3  import (
     4  	"io"
     5  
     6  	"google.golang.org/grpc"
     7  )
     8  
     9  // ServerStreamClient is a mock which satisfies the grpc.ClientStream interface,
    10  // allowing it to be returned by methods where the server returns a stream of
    11  // results. It can be populated with a list of results to return, or an error
    12  // to return.
    13  type ServerStreamClient[T any] struct {
    14  	grpc.ClientStream
    15  	Results []*T
    16  	Err     error
    17  }
    18  
    19  // Recv returns the error, if populated. Otherwise it returns the next item from
    20  // the list of results. If it has returned all items already, it returns EOF.
    21  func (c *ServerStreamClient[T]) Recv() (*T, error) {
    22  	if c.Err != nil {
    23  		return nil, c.Err
    24  	}
    25  	if len(c.Results) == 0 {
    26  		return nil, io.EOF
    27  	}
    28  	res := c.Results[0]
    29  	c.Results = c.Results[1:]
    30  	return res, nil
    31  }