github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/server/reflecton_test.go (about) 1 package server 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "github.com/nyan233/littlerpc/core/common/metadata" 8 "reflect" 9 "testing" 10 11 "github.com/nyan233/littlerpc/core/container" 12 ) 13 14 func TestReflection(t *testing.T) { 15 noneServer := &Server{ 16 services: container.NewRCUMap[string, *metadata.Process](128), 17 sources: container.NewRCUMap[string, *metadata.Source](128), 18 } 19 noneServer.config.Store(&Config{ 20 DefaultProcessOption: metadata.ProcessOption{ 21 SyncCall: true, 22 CompleteReUsage: true, 23 UseMux: false, 24 UseRawGoroutine: false, 25 }, 26 }) 27 reflection := &LittleRpcReflection{ 28 rpcServer: noneServer, 29 } 30 err := noneServer.RegisterClass(ReflectionSource, reflection, nil) 31 if err != nil { 32 t.Fatal(err) 33 } 34 table, err := reflection.MethodTable(context.Background(), ReflectionSource) 35 if err != nil { 36 t.Fatal(err) 37 } 38 t.Log(*table) 39 allInstance, err := reflection.AllInstance(context.Background()) 40 if err != nil { 41 t.Fatal(err) 42 } 43 t.Log(allInstance) 44 argumentType, err := reflection.MethodArgumentType(context.Background(), fmt.Sprintf("%s.%s", ReflectionSource, "MethodArgumentType")) 45 if err != nil { 46 t.Fatal(err) 47 } 48 bytes, _ := json.Marshal(argumentType) 49 t.Log(string(bytes)) 50 allCodec, err := reflection.AllCodec(context.Background()) 51 if err != nil { 52 t.Fatal(err) 53 } 54 bytes, _ = json.Marshal(allCodec) 55 t.Log(string(bytes)) 56 allPacker, err := reflection.AllPacker(context.Background()) 57 if err != nil { 58 t.Fatal(err) 59 } 60 bytes, _ = json.Marshal(allPacker) 61 t.Log(string(bytes)) 62 } 63 64 func TestGetArgumentType(t *testing.T) { 65 type FuncArg1 struct { 66 Uid int64 67 Name string 68 Comment string 69 Cancel context.Context 70 Friend []FuncArg1 71 } 72 type Func1 func(ctx context.Context, a1 *FuncArg1, a2 string, a3 map[string]map[uint64]FuncArg1) 73 typ := reflect.TypeOf((Func1)(nil)) 74 for i := 0; i < typ.NumIn(); i++ { 75 bytes, err := json.Marshal(getArgumentType(typ.In(i))) 76 if err != nil { 77 t.Fatal(err) 78 } 79 t.Log(string(bytes)) 80 } 81 }