From 83e76b7af5093e5f0728e52426b5e652db830ec6 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 14 Oct 2022 20:03:24 +0200 Subject: [PATCH] Add go_grpc_library test to BCR test module --- tests/bcr/MODULE.bazel | 11 +++- tests/bcr/proto/BUILD.bazel | 20 +++++- tests/bcr/proto/foo.proto | 4 ++ tests/bcr/proto/foo_grpc_test.go | 62 +++++++++++++++++++ .../proto/{foo_test.go => foo_proto_test.go} | 0 5 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 tests/bcr/proto/foo_grpc_test.go rename tests/bcr/proto/{foo_test.go => foo_proto_test.go} (100%) diff --git a/tests/bcr/MODULE.bazel b/tests/bcr/MODULE.bazel index 374cda57ef..68bddca318 100644 --- a/tests/bcr/MODULE.bazel +++ b/tests/bcr/MODULE.bazel @@ -8,9 +8,18 @@ local_path_override( module_name = "rules_go", path = "../..", ) -bazel_dep(name = "protobuf", version = "3.19.2") +bazel_dep(name = "gazelle", version = "0.27.0") +bazel_dep(name = "protobuf", version = "3.19.6") # Test that this correctly downloads the SDK by requesting it from the commandline (see presubmit.yml). go_sdk = use_extension("@my_rules_go//go:extensions.bzl", "go_sdk") go_sdk.download(name = "go_sdk", version = "1.17.5") use_repo(go_sdk, "go_sdk") + +go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") +go_deps.module( + path = "google.golang.org/grpc", + version = "v1.50.0", + sum = "h1:fPVVDxY9w++VjTZsYvXWqEf9Rqar/e+9zYfxKK+W+YU=", +) +use_repo(go_deps, "org_golang_google_grpc") diff --git a/tests/bcr/proto/BUILD.bazel b/tests/bcr/proto/BUILD.bazel index c53c972417..1d1dfe60e5 100644 --- a/tests/bcr/proto/BUILD.bazel +++ b/tests/bcr/proto/BUILD.bazel @@ -16,9 +16,23 @@ go_proto_library( ) go_test( - name = "foo_test", - srcs = ["foo_test.go"], + name = "foo_proto_test", + srcs = ["foo_proto_test.go"], + deps = [":foo_go_proto"], +) + +go_grpc_library( + name = "foo_go_grpc", + importpath = "example.com/foo_proto", + protos = [":foo_proto"], +) + +go_test( + name = "foo_grpc_test", + srcs = ["foo_grpc_test.go"], deps = [ - ":foo_go_proto", + ":foo_go_grpc", + "@org_golang_google_grpc//:grpc", + "@org_golang_google_grpc//credentials/insecure", ], ) diff --git a/tests/bcr/proto/foo.proto b/tests/bcr/proto/foo.proto index 26a80978de..7198022641 100644 --- a/tests/bcr/proto/foo.proto +++ b/tests/bcr/proto/foo.proto @@ -9,3 +9,7 @@ message Foo { int64 value = 1; google.protobuf.Empty empty = 2; } + +service Fooer { + rpc RoundTripFoo (Foo) returns (Foo) {} +} diff --git a/tests/bcr/proto/foo_grpc_test.go b/tests/bcr/proto/foo_grpc_test.go new file mode 100644 index 0000000000..4879315f25 --- /dev/null +++ b/tests/bcr/proto/foo_grpc_test.go @@ -0,0 +1,62 @@ +package grpc_test + +import ( + "context" + "fmt" + "log" + "net" + "testing" + + "example.com/foo_proto" + + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +type fooerServer struct { +} + +func newServer() *fooerServer { + return &fooerServer{} +} + +func (*fooerServer) RoundTripFoo(ctx context.Context, foo *foo_proto.Foo) (*foo_proto.Foo, error) { + foo.Value += 1 + return foo, nil +} + +func TestRoundTripFoo(t *testing.T) { + // Start the server. + address := fmt.Sprintf("localhost:%d", 12345) + lis, err := net.Listen("tcp", address) + if err != nil { + log.Fatalf("failed to listen on %s: %v", address, err) + } + grpcServer := grpc.NewServer() + foo_proto.RegisterFooerServer(grpcServer, newServer()) + go func() { + grpcServer.Serve(lis) + }() + + // Start the client. + conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + log.Fatalf("fail to dial %s: %v", address, err) + } + defer conn.Close() + client := foo_proto.NewFooerClient(conn) + + // Send a message and verify that it is returned correctly. + msgIn := &foo_proto.Foo{ + Value: 42, + } + msgOut, err := client.RoundTripFoo(context.TODO(), msgIn) + if err != nil { + log.Fatalf("failed to round-trip message: %v", err) + } + if msgOut.Value != 43 { + log.Fatalf("message did not round-trip correctly: sent %v, got %v", msgIn, msgOut) + } + + grpcServer.GracefulStop() +} diff --git a/tests/bcr/proto/foo_test.go b/tests/bcr/proto/foo_proto_test.go similarity index 100% rename from tests/bcr/proto/foo_test.go rename to tests/bcr/proto/foo_proto_test.go