github.com/turingchain2020/turingchain@v1.1.21/rpc/grpcclient/api_test.go (about) 1 package grpcclient_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/turingchain2020/turingchain/client/mocks" 9 qmocks "github.com/turingchain2020/turingchain/queue/mocks" 10 "github.com/turingchain2020/turingchain/rpc" 11 "github.com/turingchain2020/turingchain/rpc/grpcclient" 12 "github.com/turingchain2020/turingchain/types" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/mock" 15 "google.golang.org/grpc" 16 ) 17 18 func TestMultipleGRPC(t *testing.T) { 19 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 20 qapi := new(mocks.QueueProtocolAPI) 21 qapi.On("GetConfig", mock.Anything).Return(cfg) 22 qapi.On("Query", "ticket", "RandNumHash", mock.Anything).Return(&types.ReplyHash{Hash: []byte("hello")}, nil) 23 //testnode setup 24 rpcCfg := new(types.RPC) 25 rpcCfg.GrpcBindAddr = "127.0.0.1:8003" 26 rpcCfg.JrpcBindAddr = "127.0.0.1:8004" 27 rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"} 28 rpcCfg.JrpcFuncWhitelist = []string{"*"} 29 rpcCfg.GrpcFuncWhitelist = []string{"*"} 30 rpc.InitCfg(rpcCfg) 31 qm := &qmocks.Client{} 32 qm.On("GetConfig", mock.Anything).Return(cfg) 33 server := rpc.NewGRpcServer(qm, qapi) 34 assert.NotNil(t, server) 35 go server.Listen() 36 time.Sleep(time.Second) 37 //一个IP 有效,一个IP 无效 38 paraRemoteGrpcClient := "127.0.0.1:8004,127.0.0.1:8003,127.0.0.1" 39 conn, err := grpc.Dial(grpcclient.NewMultipleURL(paraRemoteGrpcClient), grpc.WithInsecure()) 40 assert.Nil(t, err) 41 grpcClient := types.NewTuringchainClient(conn) 42 param := &types.ReqRandHash{ 43 ExecName: "ticket", 44 BlockNum: 5, 45 Hash: []byte("hello"), 46 } 47 reply, err := grpcClient.QueryRandNum(context.Background(), param) 48 assert.Nil(t, err) 49 assert.Equal(t, reply.Hash, []byte("hello")) 50 } 51 52 func TestMultipleGRPCLocalhost(t *testing.T) { 53 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 54 qapi := new(mocks.QueueProtocolAPI) 55 qapi.On("GetConfig", mock.Anything).Return(cfg) 56 qapi.On("Query", "ticket", "RandNumHash", mock.Anything).Return(&types.ReplyHash{Hash: []byte("hello")}, nil) 57 //testnode setup 58 rpcCfg := new(types.RPC) 59 rpcCfg.GrpcBindAddr = "localhost:8003" 60 rpcCfg.JrpcBindAddr = "localhost:8004" 61 rpcCfg.Whitelist = []string{"localhost", "0.0.0.0"} 62 rpcCfg.JrpcFuncWhitelist = []string{"*"} 63 rpcCfg.GrpcFuncWhitelist = []string{"*"} 64 rpc.InitCfg(rpcCfg) 65 qm := &qmocks.Client{} 66 qm.On("GetConfig", mock.Anything).Return(cfg) 67 server := rpc.NewGRpcServer(qm, qapi) 68 assert.NotNil(t, server) 69 go server.Listen() 70 time.Sleep(time.Second) 71 //一个IP 有效,一个IP 无效 72 paraRemoteGrpcClient := "localhost:8004,localhost:8003,localhost" 73 conn, err := grpc.Dial(grpcclient.NewMultipleURL(paraRemoteGrpcClient), grpc.WithInsecure()) 74 assert.Nil(t, err) 75 grpcClient := types.NewTuringchainClient(conn) 76 param := &types.ReqRandHash{ 77 ExecName: "ticket", 78 BlockNum: 5, 79 Hash: []byte("hello"), 80 } 81 reply, err := grpcClient.QueryRandNum(context.Background(), param) 82 assert.Nil(t, err) 83 assert.Equal(t, reply.Hash, []byte("hello")) 84 } 85 86 func TestNewParaClient(t *testing.T) { 87 qapi := new(mocks.QueueProtocolAPI) 88 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 89 qapi.On("GetConfig", mock.Anything).Return(cfg) 90 qapi.On("Query", "ticket", "RandNumHash", mock.Anything).Return(&types.ReplyHash{Hash: []byte("hello")}, nil) 91 //testnode setup 92 rpcCfg := new(types.RPC) 93 rpcCfg.GrpcBindAddr = "127.0.0.1:8003" 94 rpcCfg.JrpcBindAddr = "127.0.0.1:8004" 95 rpcCfg.Whitelist = []string{"127.0.0.1", "0.0.0.0"} 96 rpcCfg.JrpcFuncWhitelist = []string{"*"} 97 rpcCfg.GrpcFuncWhitelist = []string{"*"} 98 rpc.InitCfg(rpcCfg) 99 qm := &qmocks.Client{} 100 qm.On("GetConfig", mock.Anything).Return(cfg) 101 server := rpc.NewGRpcServer(qm, qapi) 102 assert.NotNil(t, server) 103 go server.Listen() 104 time.Sleep(time.Second) 105 //一个IP 有效,一个IP 无效 106 paraRemoteGrpcClient := "127.0.0.1:8004,127.0.0.1:8003,127.0.0.1" 107 grpcClient, err := grpcclient.NewMainChainClient(cfg, paraRemoteGrpcClient) 108 assert.Nil(t, err) 109 param := &types.ReqRandHash{ 110 ExecName: "ticket", 111 BlockNum: 5, 112 Hash: []byte("hello"), 113 } 114 reply, err := grpcClient.QueryRandNum(context.Background(), param) 115 assert.Nil(t, err) 116 assert.Equal(t, reply.Hash, []byte("hello")) 117 } 118 119 func TestNewMainChainClient(t *testing.T) { 120 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 121 grpcClient1, err := grpcclient.NewMainChainClient(cfg, "") 122 assert.Nil(t, err) 123 grpcClient2, err := grpcclient.NewMainChainClient(cfg, "") 124 assert.Nil(t, err) 125 if grpcClient1 != grpcClient2 { 126 t.Error("grpc client is the same") 127 } 128 129 grpcClient3, err := grpcclient.NewMainChainClient(cfg, "127.0.0.1") 130 assert.Nil(t, err) 131 grpcClient4, err := grpcclient.NewMainChainClient(cfg, "127.0.0.1") 132 assert.Nil(t, err) 133 if grpcClient3 == grpcClient4 { 134 t.Error("grpc client is not the same") 135 } 136 }