-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
client: use "localhost:port" as authority if target is ":port" #4017
client: use "localhost:port" as authority if target is ":port" #4017
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, but please add a test for this in test/
(verify the :authority
header metadata in a simple server).
@dfawley Added the test in authority_test.go. |
test/authority_test.go
Outdated
@@ -33,44 +34,48 @@ import ( | |||
testpb "google.golang.org/grpc/test/grpc_testing" | |||
) | |||
|
|||
func authorityChecker(expectedAuthority *string) func(context.Context, *testpb.Empty) (*testpb.Empty, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about passing a pointer here without synchronization. It may fail the race test.
I'd do this instead:
func authorityChecker(ctx context.Context, expectedAuthority string) (*testpb.Empty, error) {
// Same body as closure
}
...
ss := &stubServer{
emptyCall: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
// Simple case:
return authorityChecker(ctx, wantAuthority)
// Complex case:
wantAuthorityMu.Lock()
defer wantAuthorityMu.Unlock()
return authorityChecker(ctx, wantAuthority)
}
}
Or use a channel to pass the expected authority to the handler, but that's probably more trouble than it's worth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made this change.
test/authority_test.go
Outdated
} | ||
if err := ss.Start(nil); err != nil { | ||
t.Fatalf("Error starting endpoint server: %v", err) | ||
return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return-after-fatal is unncessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed return
test/authority_test.go
Outdated
s := strings.Split(ss.address, ":") | ||
if len(s) != 2 { | ||
t.Fatalf("No port in address: %v", ss.address) | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use net.ParseIP
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using net package for this now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, just one minor nit.
test/authority_test.go
Outdated
expectedAuthority = "localhost:" + s[1] | ||
authorityMu.Lock() | ||
expectedAuthority = "localhost:" + port | ||
authorityMu.Unlock() | ||
// ss.Start dials, but not the ":[port]" target that is being tested here. | ||
// Dial again, with ":[port]" as the target. | ||
cc, err := grpc.Dial(":"+s[1], grpc.WithInsecure()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/s[1]/port/
Fixes #3983 .