github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/cmd/lrpcurl/rpcurl_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "context" 6 "errors" 7 "fmt" 8 "github.com/nyan233/littlerpc/cmd/lrpcurl/mocks" 9 "github.com/nyan233/littlerpc/cmd/lrpcurl/proxy" 10 "github.com/nyan233/littlerpc/core/client" 11 "github.com/nyan233/littlerpc/core/common/errorhandler" 12 "github.com/nyan233/littlerpc/core/server" 13 "github.com/stretchr/testify/mock" 14 "io" 15 "testing" 16 ) 17 18 func TestFeature(t *testing.T) { 19 defer func() { 20 if err := recover(); err != nil { 21 t.Log(err) 22 } 23 }() 24 inter := newMockReflectionProxy(t) 25 caller := newMockCaller(t) 26 for _, opt := range allSupportOption { 27 *option = opt 28 *source = "Hello" 29 *service = "Hello.Hello" 30 *call = `["hello-world",123456]` 31 parserOption(context.Background(), inter, caller) 32 } 33 } 34 35 func TestMockInterface(t *testing.T) { 36 inter := newMockReflectionProxy(t) 37 testOption := []struct { 38 Format OutType 39 Writer io.Writer 40 }{ 41 { 42 Format: FormatJson, 43 Writer: new(bytes.Buffer), 44 }, 45 { 46 Format: Json, 47 Writer: new(bytes.Buffer), 48 }, 49 { 50 Format: Text, 51 Writer: new(bytes.Buffer), 52 }, 53 } 54 for k, option := range testOption { 55 t.Run(fmt.Sprintf("TestOption[%d]", k), func(t *testing.T) { 56 getAllSupportOption(option.Format, option.Writer) 57 getAllInstance(context.Background(), inter, option.Format, option.Writer) 58 getMethodTable(context.Background(), inter, "Hello", option.Format, option.Writer) 59 getAllCodec(context.Background(), inter, option.Format, option.Writer) 60 getAllPacker(context.Background(), inter, option.Format, option.Writer) 61 getArgType(context.Background(), inter, "Hello.Hello", option.Format, option.Writer) 62 callerMock := newMockCaller(t) 63 callFunc(context.Background(), callerMock, "Hello.Hello", [][]byte{[]byte(`"hello-world"`), []byte("123456")}, option.Format, option.Writer) 64 }) 65 } 66 } 67 68 func newMockReflectionProxy(t *testing.T) proxy.LittleRpcReflectionProxy { 69 reflectionMock := mocks.NewLittleRpcReflectionProxy(t) 70 inter := (proxy.LittleRpcReflectionProxy)(reflectionMock) 71 reflectionMock.On("AllCodec", context.Background()).Return( 72 func(ctx context.Context, opts ...client.CallOption) []string { 73 return []string{"json", "protobuf", "msgpack"} 74 }, 75 func(ctx context.Context, opts ...client.CallOption) error { return nil }, 76 ) 77 reflectionMock.On("AllInstance", context.Background()).Return( 78 func(ctx context.Context, opts ...client.CallOption) map[string]string { 79 return map[string]string{ 80 "TestInstance": "github.com/nyan233/littlerpc/1", 81 "LittleRpcReflection": "github.com/nyan233/littlerpc/1/3", 82 "TestInstance2": "github.com/nyan233/littlerpc/1/2", 83 } 84 }, 85 func(ctx context.Context, opts ...client.CallOption) error { 86 return nil 87 }, 88 ) 89 reflectionMock.On("AllPacker", context.Background()).Return( 90 func(ctx context.Context, opts ...client.CallOption) []string { 91 return []string{"text", "gzip", "tar.gz"} 92 }, 93 func(ctx context.Context, opts ...client.CallOption) error { return nil }, 94 ) 95 reflectionMock.On("MethodTable", context.Background(), "Hello").Return(func(ctx context.Context, sourceName string, opts ...client.CallOption) *server.MethodTable { 96 return &server.MethodTable{ 97 SourceName: sourceName, 98 Table: []string{"Hello", "SayHelloToJson", "SayHelloToProtoBuf"}, 99 } 100 }, func(ctx context.Context, sourceName string, opts ...client.CallOption) error { 101 if sourceName == "" { 102 return errors.New("method table not found") 103 } 104 return nil 105 }) 106 reflectionMock.On("MethodArgumentType", context.Background(), "Hello.Hello").Return( 107 func(ctx context.Context, serviceName string, opts ...client.CallOption) []server.ArgumentType { 108 return []server.ArgumentType{ 109 { 110 Kind: "string", 111 Type: "string", 112 }, 113 { 114 Kind: "integer", 115 Type: "int64", 116 }, 117 } 118 }, 119 func(ctx context.Context, serviceName string, opts ...client.CallOption) error { 120 if serviceName == "" { 121 return errors.New("service name not found") 122 } 123 return nil 124 }, 125 ) 126 return inter 127 } 128 129 func newMockCaller(t *testing.T) Caller { 130 callerMock := mocks.NewCaller(t) 131 callerMock.On("RawCall", mock.AnythingOfType("string"), *new([]client.CallOption), 132 context.Background(), mock.AnythingOfType("string"), mock.AnythingOfType("float64")).Return( 133 func(service string, opts []client.CallOption, args ...interface{}) []interface{} { 134 return args[1:] 135 }, 136 func(service string, opts []client.CallOption, args ...interface{}) error { 137 if service != "Hello.Hello" { 138 return errors.New("service name is not found") 139 } 140 return errorhandler.Success 141 }, 142 ) 143 return callerMock 144 }