在Go语言中测试GRPC流式发送函数

huangapple go评论83阅读模式
英文:

Testing around GRPC stream Send function in Go

问题

我有一个Go GRPC服务器端流式传输函数:

func (server *Server) GetClients(req *iam.GetClientsRequest, client iam.IAM_GetClientsServer) error {
    ctx := client.(interface{ Context() context.Context }).Context()

    userID, err := getUserIDStream(client)
    if err != nil {
        return err
    }

    clients, err := server.db.QueryByUserID(ctx, userID)
    if err != nil {
        return grpc.Errorf(codes.Internal, apiutils.ServerError)
    }

    for _, value := range clients {
        converted, err := server.fromInternalClient(value)
        if err != nil {
            return err
        }

        if err := client.Send(converted); err != nil {
            return err
        }
    }

    return nil
}

我这样测试它:

It("GetClients - Send fails - Error", func() {

    handler := createHandler(db)
    lis := bufconn.Listen(bufSize)
    server := grpc.NewServer()

    iam.RegisterIAMServer(server, NewServer(handler))
    go func() {
        if err := server.Serve(lis); err != nil {
            log.Fatalf("Server exited with error: %v", err)
        }
    }()

    defer lis.Close()
    defer server.GracefulStop()

    conn, err := grpc.DialContext(context.Background(), "bufnet",
        grpc.WithContextDialer(createBufDialier(lis)), grpc.WithInsecure())
    Expect(err).ShouldNot(HaveOccurred())
    defer conn.Close()
    client := iam.NewIAMClient(conn)

    cclient, _ := client.GetClients(addAccessToken(context.Background()), new(iam.GetClientsRequest))
    resp, err := cclient.Recv()

    Expect(resp).Should(BeNil())
    Expect(err).Should(HaveOccurred())
    Expect(err.Error()).Should(Equal(message))
})

我的问题是,我不确定如何在Send上引发失败,以便我可以测试响应。由于我正在使用实际的测试服务器和客户端,我不能只是模拟对象,而且我也不想走这条路。有没有办法可以做到这一点?

英文:

I have a Go GRPC server-side streaming function:

func (server *Server) GetClients(req *iam.GetClientsRequest, client iam.IAM_GetClientsServer) error {
	ctx := client.(interface{ Context() context.Context }).Context()

	userID, err := getUserIDStream(client)
	if err != nil {
		return err
	}

	clients, err := server.db.QueryByUserID(ctx, userID)
	if err != nil {
		return grpc.Errorf(codes.Internal, apiutils.ServerError)
	}

	for _, value := range clients {
		converted, err := server.fromInternalClient(value)
		if err != nil {
			return err
		}

		if err := client.Send(converted); err != nil {
			return err
		}
	}

	return nil
}

and I'm testing it like this:

It("GetClients - Send fails - Error", func() {

	handler := createHandler(db)
	lis := bufconn.Listen(bufSize)
	server := grpc.NewServer()

	iam.RegisterIAMServer(server, NewServer(handler))
	go func() {
		if err := server.Serve(lis); err != nil {
			log.Fatalf("Server exited with error: %v", err)
		}
	}()

	defer lis.Close()
	defer server.GracefulStop()

    conn, err := grpc.DialContext(context.Background(), "bufnet",
		grpc.WithContextDialer(createBufDialier(lis)), grpc.WithInsecure())
	Expect(err).ShouldNot(HaveOccurred())
	defer conn.Close()
	client := iam.NewIAMClient(conn)

	cclient, _ := client.GetClients(addAccessToken(context.Background()), new(iam.GetClientsRequest))
	resp, err := cclient.Recv()

	Expect(resp).Should(BeNil())
	Expect(err).Should(HaveOccurred())
	Expect(err.Error()).Should(Equal(message))
})

My issue is that I'm not sure how to induce a failure on Send so I can test the response. Since I'm using an actual test server and client, I can't just mock out the object and I'd prefer not to go that route anyway. Is there a way I can do this?

答案1

得分: 0

最初,我试图通过将bufSize设置为一个人为较低的值来强制Send失败。然而,这并没有产生错误,所以我决定尝试修改服务器上的maxSendMessageSize

opts := []grpc.ServerOption{}
if sendFails {
	opts = append(opts, grpc.MaxSendMsgSize(10))
}

lis := bufconn.Listen(bufSize)
server := grpc.NewServer(opts...)

这样做确实导致了错误的发生。

英文:

Originally, I was trying to force Send to fail by setting bufSize to an artificially low value. However, this wasn't producing an error so I decided to try modifying the maxSendMessageSize on the server:

opts := []grpc.ServerOption{}
if sendFails {
	opts = append(opts, grpc.MaxSendMsgSize(10))
}

lis := bufconn.Listen(bufSize)
server := grpc.NewServer(opts...)

And this worked in producing the error.

huangapple
  • 本文由 发表于 2022年10月21日 17:31:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/74151616.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定