Skip to content

Commit

Permalink
Add go_grpc_library test to BCR test module
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Oct 14, 2022
1 parent 6eb432f commit 4fb24bc
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 5 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ go_sdk.download(name = "go_default_sdk", version = "1.18.3")
use_repo(go_sdk, "go_default_sdk")
register_toolchains("@go_default_sdk//:all")

bazel_dep(name = "gazelle", version = "0.26.0")
bazel_dep(name = "gazelle", version = "0.27.0")
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
use_repo(go_deps,
Expand Down
11 changes: 10 additions & 1 deletion tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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.26.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")
20 changes: 17 additions & 3 deletions tests/bcr/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
4 changes: 4 additions & 0 deletions tests/bcr/proto/foo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ message Foo {
int64 value = 1;
google.protobuf.Empty empty = 2;
}

service Fooer {
rpc RoundTripFoo (Foo) returns (Foo) {}
}
62 changes: 62 additions & 0 deletions tests/bcr/proto/foo_grpc_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
File renamed without changes.

0 comments on commit 4fb24bc

Please sign in to comment.