Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(grpc): the unary server interceptor was already set and may not be reset #419

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions _integration-tests/tests/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package grpc
import (
"context"
"net"
"sync/atomic"
"testing"
"time"

Expand All @@ -31,17 +32,50 @@ func (tc *TestCase) Setup(t *testing.T) {
require.NoError(t, err)
tc.addr = lis.Addr().String()

tc.Server = grpc.NewServer()
var (
interceptedDirect atomic.Bool
interceptedChain atomic.Bool
)
tc.Server = grpc.NewServer(
// Register a bunch of interceptors to ensure ours does not cause a runtime crash.
grpc.UnaryInterceptor(func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
interceptedDirect.Store(true)
return handler(ctx, req)
}),
grpc.ChainUnaryInterceptor(func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
interceptedChain.Store(true)
return handler(ctx, req)
}),
)
helloworld.RegisterGreeterServer(tc.Server, &server{})

go func() { assert.NoError(t, tc.Server.Serve(lis)) }()
t.Cleanup(func() {
tc.Server.GracefulStop()
assert.True(t, interceptedDirect.Load(), "original interceptor was not called")
assert.True(t, interceptedChain.Load(), "original chained interceptor was not called")
Comment on lines +55 to +56
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RomainMuller Are the messages correct? If intercepted* are true, doesn't it mean they were called? The same for the same messages below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message is for when it fails... But I'm a litle ambivalent as to whether they should mention what should've happened or what was observed that is a failure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you are right! I usually go with something like original interceptor expected a call, it didn't happen. Anyway, it's fine as long as it's understandable in the testing logs' context.

})
}

func (tc *TestCase) Run(t *testing.T) {
conn, err := grpc.NewClient(tc.addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
var (
interceptedDirect atomic.Bool
interceptedChain atomic.Bool
)

conn, err := grpc.NewClient(
tc.addr,
// Register a bunch of interceptors to ensure ours does not cause a runtime crash.
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
interceptedDirect.Store(true)
return invoker(ctx, method, req, reply, cc, opts...)
}),
grpc.WithChainUnaryInterceptor(func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
interceptedChain.Store(true)
return invoker(ctx, method, req, reply, cc, opts...)
}),
)
require.NoError(t, err)
defer func() { require.NoError(t, conn.Close()) }()

Expand All @@ -52,6 +86,9 @@ func (tc *TestCase) Run(t *testing.T) {
resp, err := client.SayHello(ctx, &helloworld.HelloRequest{Name: "rob"})
require.NoError(t, err)
require.Equal(t, "Hello rob", resp.GetMessage())

assert.True(t, interceptedDirect.Load(), "original interceptor was not called")
assert.True(t, interceptedChain.Load(), "original chained interceptor was not called")
}

func (*TestCase) ExpectedTraces() trace.Traces {
Expand Down
Loading
Loading