aboutsummaryrefslogtreecommitdiff
path: root/tests/bcr/proto/foo_grpc_test.go
blob: 4879315f25b6741f8c3d20c9947b6ed844d6026b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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()
}