环境
go version go1.17.4 linux/amd64
libprotoc 3.6.1
问题
我正在测试 Go 实现的 grpc 服务器和客户端。
首先,我做了 proto 定义并通过 protoc 命令生成 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
代码
你好.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)
}
元芳怎么了
哈士奇WWW
随时随地看视频慕课网APP
相关分类