You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.8 KiB
77 lines
1.8 KiB
3 years ago
|
package grpc
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"crypto/tls"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/middleware"
|
||
|
"github.com/go-kratos/kratos/v2/registry"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"google.golang.org/grpc"
|
||
|
)
|
||
|
|
||
|
func TestWithEndpoint(t *testing.T) {
|
||
|
o := &clientOptions{}
|
||
|
v := "abc"
|
||
|
WithEndpoint(v)(o)
|
||
|
assert.Equal(t, v, o.endpoint)
|
||
|
}
|
||
|
|
||
|
func TestWithMiddleware(t *testing.T) {
|
||
|
o := &clientOptions{}
|
||
|
v := []middleware.Middleware{
|
||
|
func(middleware.Handler) middleware.Handler { return nil },
|
||
|
}
|
||
|
WithMiddleware(v...)(o)
|
||
|
assert.Equal(t, v, o.middleware)
|
||
|
}
|
||
|
|
||
|
type mockRegistry struct{}
|
||
|
|
||
|
func (m *mockRegistry) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
func (m *mockRegistry) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
func TestWithDiscovery(t *testing.T) {
|
||
|
o := &clientOptions{}
|
||
|
v := &mockRegistry{}
|
||
|
WithDiscovery(v)(o)
|
||
|
assert.Equal(t, v, o.discovery)
|
||
|
}
|
||
|
|
||
|
func TestWithTLSConfig(t *testing.T) {
|
||
|
o := &clientOptions{}
|
||
|
v := &tls.Config{}
|
||
|
WithTLSConfig(v)(o)
|
||
|
assert.Equal(t, v, o.tlsConf)
|
||
|
}
|
||
|
|
||
|
func TestWithUnaryInterceptor(t *testing.T) {
|
||
|
o := &clientOptions{}
|
||
|
v := []grpc.UnaryClientInterceptor{
|
||
|
func(ctx context.Context, method string, req, reply interface{},
|
||
|
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||
|
return nil
|
||
|
},
|
||
|
func(ctx context.Context, method string, req, reply interface{},
|
||
|
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
||
|
return nil
|
||
|
},
|
||
|
}
|
||
|
WithUnaryInterceptor(v...)(o)
|
||
|
assert.Equal(t, v, o.ints)
|
||
|
}
|
||
|
|
||
|
func TestWithOptions(t *testing.T) {
|
||
|
o := &clientOptions{}
|
||
|
v := []grpc.DialOption{
|
||
|
grpc.EmptyDialOption{},
|
||
|
}
|
||
|
WithOptions(v...)(o)
|
||
|
assert.Equal(t, v, o.grpcOpts)
|
||
|
}
|