Skip to content

Commit

Permalink
Fix grpc example (#3549)
Browse files Browse the repository at this point in the history
* Fix linter issues

* Use Printf for log with no KV argument

* Fix non-deterministic test
  • Loading branch information
raphael authored Jul 7, 2024
1 parent 2d7a89c commit c6dc4dc
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 25 deletions.
2 changes: 1 addition & 1 deletion codegen/service/templates/endpoint.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func (s *{{ .ServiceVarName }}srvc) {{ .VarName }}(ctx context.Context{{ if .Pay
{{- end }}
{{- end }}
{{- end }}
log.Print(ctx, "{{ .ServiceVarName }}.{{ .Name }}")
log.Printf(ctx, "{{ .ServiceVarName }}.{{ .Name }}")
return
}
4 changes: 2 additions & 2 deletions grpc/codegen/client-no-server-pkgpath.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
}
Expand Down
4 changes: 2 additions & 2 deletions grpc/codegen/client-no-server.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
}
Expand Down
4 changes: 2 additions & 2 deletions grpc/codegen/client-server-hosting-multiple-services.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
}
Expand Down
4 changes: 2 additions & 2 deletions grpc/codegen/client-server-hosting-service-subset.golden
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
}
Expand Down
4 changes: 2 additions & 2 deletions grpc/codegen/templates/do_grpc_cli.go.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
func doGRPC(scheme, host string, timeout int, debug bool) (goa.Endpoint, any, error) {
conn, err := grpc.Dial(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
func doGRPC(_, host string, _ int, _ bool) (goa.Endpoint, any, error) {
conn, err := grpc.NewClient(host, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
fmt.Fprintf(os.Stderr, "could not connect to gRPC server at %s: %v\n", host, err)
}
Expand Down
18 changes: 8 additions & 10 deletions grpc/middleware/canceler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"testing"

grpcm "goa.design/goa/v3/grpc/middleware"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"

grpcm "goa.design/goa/v3/grpc/middleware"
)

type (
Expand Down Expand Up @@ -46,15 +48,11 @@ func TestStreamCanceler(t *testing.T) {
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

go func() {
cancel()
}()

if err := grpcm.StreamCanceler(ctx)(nil, c.stream, stream, c.handler); err != nil {
if err.Error() != "server is stopping" {
t.Errorf("StreamCanceler error: %v", err)
}
interceptor := grpcm.StreamCanceler(ctx)
cancel()
err := interceptor(nil, c.stream, stream, c.handler)
if err != nil {
assert.ErrorContains(t, err, "server is stopping")
}
})
}
Expand Down

0 comments on commit c6dc4dc

Please sign in to comment.