-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make gRPC ping time and timeout into parameters
Signed-off-by: Arve Knudsen <[email protected]>
- Loading branch information
Showing
6 changed files
with
165 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package grpcclient | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/grafana/dskit/crypto/tls" | ||
middleware "github.com/grpc-ecosystem/go-grpc-middleware" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/keepalive" | ||
) | ||
|
||
type fakeDialOption struct { | ||
grpc.EmptyDialOption | ||
|
||
callOpts []grpc.CallOption | ||
isInsecure bool | ||
unaryClientInterceptor grpc.UnaryClientInterceptor | ||
streamClientInterceptor grpc.StreamClientInterceptor | ||
keepaliveParams keepalive.ClientParameters | ||
} | ||
|
||
func (o fakeDialOption) Equal(other fakeDialOption) bool { | ||
if len(o.callOpts) != len(other.callOpts) { | ||
return false | ||
} | ||
|
||
for i, arg := range o.callOpts { | ||
if maxRecv, ok := arg.(grpc.MaxRecvMsgSizeCallOption); ok { | ||
if maxRecv != other.callOpts[i].(grpc.MaxRecvMsgSizeCallOption) { | ||
return false | ||
} | ||
continue | ||
} | ||
if maxSend, ok := arg.(grpc.MaxSendMsgSizeCallOption); ok { | ||
if maxSend != other.callOpts[i].(grpc.MaxSendMsgSizeCallOption) { | ||
return false | ||
} | ||
continue | ||
} | ||
} | ||
|
||
hasUnaryInterceptor := o.unaryClientInterceptor != nil | ||
otherHasUnaryInterceptor := other.unaryClientInterceptor != nil | ||
hasStreamInterceptor := o.streamClientInterceptor != nil | ||
otherHasStreamInterceptor := other.streamClientInterceptor != nil | ||
|
||
return o.isInsecure == other.isInsecure && hasUnaryInterceptor == otherHasUnaryInterceptor && | ||
hasStreamInterceptor == otherHasStreamInterceptor && o.keepaliveParams == other.keepaliveParams | ||
} | ||
|
||
func TestConfig(t *testing.T) { | ||
origWithDefaultCallOptions := withDefaultCallOptions | ||
origWithUnaryInterceptor := withUnaryInterceptor | ||
origWithStreamInterceptor := withStreamInterceptor | ||
origWithKeepaliveParams := withKeepaliveParams | ||
origWithInsecure := tls.WithInsecure | ||
t.Cleanup(func() { | ||
withDefaultCallOptions = origWithDefaultCallOptions | ||
withUnaryInterceptor = origWithUnaryInterceptor | ||
withStreamInterceptor = origWithStreamInterceptor | ||
withKeepaliveParams = origWithKeepaliveParams | ||
tls.WithInsecure = origWithInsecure | ||
}) | ||
|
||
withDefaultCallOptions = func(cos ...grpc.CallOption) grpc.DialOption { | ||
t.Log("Received call options", "options", cos) | ||
return fakeDialOption{callOpts: cos} | ||
} | ||
withUnaryInterceptor = func(f grpc.UnaryClientInterceptor) grpc.DialOption { | ||
t.Log("Received unary client interceptor", f) | ||
return fakeDialOption{unaryClientInterceptor: f} | ||
} | ||
withStreamInterceptor = func(f grpc.StreamClientInterceptor) grpc.DialOption { | ||
t.Log("Received stream client interceptor", f) | ||
return fakeDialOption{streamClientInterceptor: f} | ||
} | ||
withKeepaliveParams = func(kp keepalive.ClientParameters) grpc.DialOption { | ||
t.Log("Received keepalive params", kp) | ||
return fakeDialOption{ | ||
keepaliveParams: kp, | ||
} | ||
} | ||
tls.WithInsecure = func() grpc.DialOption { | ||
return fakeDialOption{isInsecure: true} | ||
} | ||
|
||
cfg := Config{ | ||
PingTime: 10, | ||
PingTimeout: 20, | ||
} | ||
expOpts := []grpc.DialOption{ | ||
fakeDialOption{isInsecure: true}, | ||
fakeDialOption{callOpts: []grpc.CallOption{ | ||
grpc.MaxCallRecvMsgSize(0), | ||
grpc.MaxCallSendMsgSize(0), | ||
}}, | ||
fakeDialOption{ | ||
unaryClientInterceptor: middleware.ChainUnaryClient(), | ||
}, | ||
fakeDialOption{ | ||
streamClientInterceptor: middleware.ChainStreamClient(), | ||
}, | ||
fakeDialOption{ | ||
keepaliveParams: keepalive.ClientParameters{ | ||
Time: 10 * time.Second, | ||
Timeout: 20 * time.Second, | ||
PermitWithoutStream: true, | ||
}, | ||
}, | ||
} | ||
|
||
opts, err := cfg.DialOption(nil, nil) | ||
require.NoError(t, err) | ||
|
||
assert.Empty(t, cmp.Diff(expOpts, opts)) | ||
} |