-
-
Notifications
You must be signed in to change notification settings - Fork 661
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add go_grpc_library test to BCR test module
- Loading branch information
Showing
6 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.