Created
March 12, 2019 09:55
-
-
Save egonelbre/589593413af2411445b47104a453dd9b to your computer and use it in GitHub Desktop.
grpc bidi streaming testcase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"errors" | |
"fmt" | |
"net" | |
"time" | |
"google.golang.org/grpc" | |
pb "google.golang.org/grpc/examples/features/proto/echo" | |
) | |
func main() { | |
listener, err := net.Listen("tcp", "127.0.0.1:0") | |
check(err) | |
go runClient(listener.Addr().String()) | |
runServer(listener) | |
} | |
func check(err error) { | |
if err != nil { | |
panic(err) | |
} | |
} | |
func runClient(addr string) { | |
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock()) | |
check(err) | |
defer conn.Close() | |
client := pb.NewEchoClient(conn) | |
stream, err := client.BidirectionalStreamingEcho(context.TODO()) | |
check(err) | |
time.Sleep(5 * time.Second) | |
err = stream.Send(&pb.EchoRequest{}) | |
if err != nil { | |
closeErr := stream.CloseSend() | |
recvMsg, recvErr := stream.Recv() | |
fmt.Println("CLIENT", err, closeErr, recvErr, recvMsg) | |
} | |
fmt.Println("SENT") | |
} | |
func runServer(listener net.Listener) { | |
server := grpc.NewServer() | |
pb.RegisterEchoServer(server, &Server{}) | |
err := server.Serve(listener) | |
check(err) | |
} | |
type Server struct{} | |
func (s *Server) BidirectionalStreamingEcho(stream pb.Echo_BidirectionalStreamingEchoServer) error { | |
err1 := stream.Send(&pb.EchoResponse{Message: "1"}) | |
err2 := stream.Send(&pb.EchoResponse{Message: "1"}) | |
fmt.Println("SERVER", err1, err2) | |
return errors.New("processing failed") | |
} | |
func (s *Server) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) { | |
return nil, errors.New("processing failed") | |
} | |
func (s *Server) ServerStreamingEcho(req *pb.EchoRequest, stream pb.Echo_ServerStreamingEchoServer) error { | |
return errors.New("processing failed") | |
} | |
func (s *Server) ClientStreamingEcho(pb.Echo_ClientStreamingEchoServer) error { | |
return errors.New("processing failed") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment