grpc testing code exit 1 with 'rpc error: code = Unimplemented desc = method Hello not implemented'

grpc testing code exit 1 with 'rpc error: code = Unimplemented desc = method Hello not implemented'

环境

go version go1.17.4 linux/amd64

libprotoc 3.6.1

问题

我正在测试 Go 实现的 grpc 服务器和客户端。
首先,我通过 protoc 命令定义了 proto 并生成了 pb 代码。 我提到了这个 。 以下错误是用最少的代码打印的,以重现我的错误。

错误

$ go test
--- FAIL: TestHello (0.00s)
    main_test.go:49: failed to Hello: rpc error: code = Unimplemented desc = method Hello not implemented
FAIL
exit status 1
FAIL    github.com/Asuha-a/test/test    0.003s

代码

hello.proto

syntax = "proto3";

option go_package = "github.com/Asuha-a/test/pb";

package hello;

service Hello {
  rpc Hello (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string foo = 1;
}

message HelloReply {
  string bar = 1;
}

main_test.go

package hello_test

import (
    "context"
    "log"
    "net"
    "testing"

    "github.com/Asuha-a/test/pb"
    "google.golang.org/grpc"
    "google.golang.org/grpc/test/bufconn"
)

type server struct {
    pb.UnimplementedHelloServer
}

const bufSize = 1024 * 1024

var lis *bufconn.Listener

func init() {
    lis = bufconn.Listen(bufSize)
    s := grpc.NewServer()
    pb.RegisterHelloServer(s, &server{})
    go func() {
        if err := s.Serve(lis); err != nil {
            log.Fatalf("server exited with error: %v", err)
        }
    }()
}

func bufDialer(context.Context, string) (net.Conn, error) {
    return lis.Dial()
}

func TestHello(t *testing.T) {
    ctx := context.Background()
    conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(bufDialer), grpc.WithInsecure())
    if err != nil {
        t.Fatalf("failed to dial bufnet: %v", err)
    }
    defer conn.Close()
    client := pb.NewHelloClient(conn)
    r, err := client.Hello(ctx, &pb.HelloRequest{
        Foo: "foo",
    })
    if err != nil {
        t.Fatalf("failed to Hello: %v", err) //49th line prints the error
    }
    log.Println(r)
}

在您的测试中,您使用 server 结构调用 pb.RegisterHelloServer(s, &server{})。但是,此结构仅嵌入 pb.UnimplementedHelloServer 且未声明任何方法。因此,您只能在调用其任何 RPC 时返回 codes.Unimplemented

您必须在 server 结构上实施 Hello rpc:

func (s *server) Hello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) {
    return &pb.HelloReply{}, nil
}

注意: 嵌入 pb.UnimplementedHelloServer 以编译时检查为代价使 gRPC 服务器实现向前兼容。要放弃向前兼容性并在编译时捕获这些错误,您可以改为嵌入 pb.UnsafeHelloServer.