Skip to content

Commit c6dc4dc

Browse files
authored
Fix grpc example (#3549)
* Fix linter issues * Use Printf for log with no KV argument * Fix non-deterministic test
1 parent 2d7a89c commit c6dc4dc

9 files changed

+23
-25
lines changed

codegen/service/templates/endpoint.go.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ func (s *{{ .ServiceVarName }}srvc) {{ .VarName }}(ctx context.Context{{ if .Pay
2424
{{- end }}
2525
{{- end }}
2626
{{- end }}
27-
log.Print(ctx, "{{ .ServiceVarName }}.{{ .Name }}")
27+
log.Printf(ctx, "{{ .ServiceVarName }}.{{ .Name }}")
2828
return
2929
}

grpc/codegen/client-no-server-pkgpath.golden

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"google.golang.org/grpc/credentials/insecure"
99
)
1010

11-
func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
12-
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
11+
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
12+
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
1313
if err != nil {
1414
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
1515
}

grpc/codegen/client-no-server.golden

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"google.golang.org/grpc/credentials/insecure"
99
)
1010

11-
func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
12-
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
11+
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
12+
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
1313
if err != nil {
1414
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
1515
}

grpc/codegen/client-server-hosting-multiple-services-pkgpath.golden

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"google.golang.org/grpc/credentials/insecure"
99
)
1010

11-
func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
12-
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
11+
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
12+
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
1313
if err != nil {
1414
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
1515
}

grpc/codegen/client-server-hosting-multiple-services.golden

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"google.golang.org/grpc/credentials/insecure"
99
)
1010

11-
func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
12-
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
11+
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
12+
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
1313
if err != nil {
1414
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
1515
}

grpc/codegen/client-server-hosting-service-subset-pkgpath.golden

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"google.golang.org/grpc/credentials/insecure"
99
)
1010

11-
func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
12-
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
11+
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
12+
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
1313
if err != nil {
1414
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
1515
}

grpc/codegen/client-server-hosting-service-subset.golden

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"google.golang.org/grpc/credentials/insecure"
99
)
1010

11-
func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
12-
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
11+
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
12+
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
1313
if err != nil {
1414
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
1515
}

grpc/codegen/templates/do_grpc_cli.go.tpl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
2-
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
1+
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
2+
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
33
if err != nil {
44
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
55
}

grpc/middleware/canceler_test.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"testing"
66

7-
grpcm "goa.design/goa/v3/grpc/middleware"
7+
"github.com/stretchr/testify/assert"
88
"google.golang.org/grpc"
9+
10+
grpcm "goa.design/goa/v3/grpc/middleware"
911
)
1012

1113
type (
@@ -46,15 +48,11 @@ func TestStreamCanceler(t *testing.T) {
4648
for _, c := range cases {
4749
t.Run(c.name, func(t *testing.T) {
4850
ctx, cancel := context.WithCancel(context.Background())
49-
50-
go func() {
51-
cancel()
52-
}()
53-
54-
if err := grpcm.StreamCanceler(ctx)(nil, c.stream, stream, c.handler); err != nil {
55-
if err.Error() != "server is stopping" {
56-
t.Errorf("StreamCanceler error: %v", err)
57-
}
51+
interceptor := grpcm.StreamCanceler(ctx)
52+
cancel()
53+
err := interceptor(nil, c.stream, stream, c.handler)
54+
if err != nil {
55+
assert.ErrorContains(t, err, "server is stopping")
5856
}
5957
})
6058
}

0 commit comments

Comments
 (0)