client_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // to avoid recycle imports,
  2. // the test must be in different package with liverpc...
  3. // otherwise, test import => generated pb
  4. // generated pb => import liverpc (which includes the test)
  5. package liverpc
  6. import (
  7. "context"
  8. "math/rand"
  9. "testing"
  10. "time"
  11. "go-common/library/net/rpc/liverpc"
  12. "go-common/library/net/rpc/liverpc/testdata"
  13. v1 "go-common/library/net/rpc/liverpc/testdata/v1"
  14. v2 "go-common/library/net/rpc/liverpc/testdata/v2"
  15. "github.com/pkg/errors"
  16. )
  17. func TestDialClient(t *testing.T) {
  18. cli := testdata.New(nil)
  19. var req = &v1.RoomGetInfoReq{Id: 1002}
  20. var hd = &liverpc.Header{
  21. Platform: "ios",
  22. Src: "test",
  23. Buvid: "AUTO3315341311353015",
  24. TraceId: "18abb1a2596c43ea:18abb1a2596c43ea:0:0",
  25. Uid: 10,
  26. Caller: "live-api.rpc",
  27. UserIp: "127.0.0.1",
  28. SourceGroup: "default",
  29. }
  30. var ctx = context.TODO()
  31. reply, err := cli.V1Room.GetInfo(ctx, req, &liverpc.HeaderOption{Header: hd})
  32. if err != nil {
  33. t.Error(err)
  34. t.FailNow()
  35. }
  36. t.Logf("reply:%v %v %v", reply.Code, reply.Msg, reply.Data)
  37. _, err = cli.GetRawCli().CallRaw(context.TODO(), 2, "Room.get_by_ids", &liverpc.Args{})
  38. if err != nil {
  39. t.Error(err)
  40. t.FailNow()
  41. }
  42. }
  43. func TestCallRaw(t *testing.T) {
  44. var cli = liverpc.NewClient(&liverpc.ClientConfig{AppID: "live.room"})
  45. var hd = &liverpc.Header{
  46. Platform: "ios",
  47. Src: "test",
  48. Buvid: "AUTO3315341311353015",
  49. TraceId: "18abb1a2596c43ea:18abb1a2596c43ea:0:0",
  50. Uid: 10,
  51. Caller: "live-api.rpc",
  52. UserIp: "127.0.0.1",
  53. SourceGroup: "default",
  54. }
  55. var req = &liverpc.Args{Body: map[string]interface{}{"id": 1002}, Header: hd}
  56. hd = nil
  57. reply, err := cli.CallRaw(context.TODO(), 1, "Room.get_info", req, &liverpc.TimeoutOption{Timeout: 200 * time.Millisecond})
  58. if err != nil {
  59. t.Error(err)
  60. t.FailNow()
  61. }
  62. t.Logf("reply:%v %v %v", reply.Code, reply.Message, string(reply.Data))
  63. _, err = cli.CallRaw(context.TODO(), 1, "Room.get_info", req, &liverpc.TimeoutOption{Timeout: 2 * time.Millisecond})
  64. if err == nil {
  65. t.Error(errors.New("should fail"))
  66. t.FailNow()
  67. }
  68. }
  69. func TestMap(t *testing.T) {
  70. client := liverpc.NewClient(&liverpc.ClientConfig{AppID: "live.room"})
  71. var rpcClient = v2.NewRoomRPCClient(client)
  72. var req = &v2.RoomGetByIdsReq{Ids: []int64{1002}}
  73. var header = &liverpc.Header{
  74. Platform: "ios",
  75. Src: "test",
  76. Buvid: "AUTO3315341311353015",
  77. TraceId: "18abb1a2596c43ea:18abb1a2596c43ea:0:0",
  78. Uid: 10,
  79. Caller: "live-api.rpc",
  80. UserIp: "127.0.0.1",
  81. SourceGroup: "default",
  82. }
  83. var ctx = context.TODO()
  84. reply, err := rpcClient.GetByIds(ctx, req, liverpc.HeaderOption{Header: header})
  85. if err != nil {
  86. t.Error(err)
  87. t.FailNow()
  88. }
  89. t.Logf("reply:%v %v %v", reply.Code, reply.Msg, reply.Data)
  90. }
  91. func TestDiscoveryClient(t *testing.T) {
  92. conf := &liverpc.ClientConfig{
  93. AppID: "live.room",
  94. }
  95. cli := liverpc.NewClient(conf)
  96. arg := &v1.RoomGetInfoReq{Id: 1001}
  97. var rpcClient = v1.NewRoomRPCClient(cli)
  98. reply, err := rpcClient.GetInfo(context.TODO(), arg)
  99. if err != nil {
  100. t.Error(err)
  101. t.FailNow()
  102. }
  103. t.Logf("reply:%+v", reply)
  104. }
  105. func TestCancel(t *testing.T) {
  106. conf := &liverpc.ClientConfig{
  107. AppID: "live.room",
  108. }
  109. cli := liverpc.NewClient(conf)
  110. arg := &v1.RoomGetInfoReq{Id: 1001}
  111. var rpcClient = v1.NewRoomRPCClient(cli)
  112. ctx, cancel := context.WithTimeout(context.TODO(), time.Millisecond)
  113. defer cancel()
  114. var err error
  115. _, err = rpcClient.GetInfo(ctx, arg)
  116. if err == nil || errors.Cause(err) != context.DeadlineExceeded {
  117. t.Error(err)
  118. t.FailNow()
  119. }
  120. }
  121. func TestCallRawCancel(t *testing.T) {
  122. var cli = liverpc.NewClient(&liverpc.ClientConfig{AppID: "live.room"})
  123. var hd = &liverpc.Header{
  124. Platform: "ios",
  125. Src: "test",
  126. Buvid: "AUTO3315341311353015",
  127. TraceId: "18abb1a2596c43ea:18abb1a2596c43ea:0:0",
  128. Uid: 10,
  129. Caller: "live-api.rpc",
  130. UserIp: "127.0.0.1",
  131. SourceGroup: "default",
  132. }
  133. var req = &liverpc.Args{Body: map[string]interface{}{"id": 1002}, Header: hd}
  134. hd = nil
  135. ctx, cancel := context.WithTimeout(context.TODO(), time.Millisecond)
  136. defer cancel()
  137. _, err := cli.CallRaw(ctx, 1, "Room.get_info", req)
  138. if err == nil || errors.Cause(err) != context.DeadlineExceeded {
  139. t.Error(err)
  140. t.FailNow()
  141. }
  142. t.Logf("err is +%v", err)
  143. }
  144. func BenchmarkDialClient(b *testing.B) {
  145. rand.Seed(time.Now().UnixNano())
  146. var header = &liverpc.Header{
  147. Platform: "ios",
  148. Src: "test",
  149. Buvid: "AUTO3315341311353015",
  150. TraceId: "18abb1a2596c43ea:18abb1a2596c43ea:0:0",
  151. Uid: 10,
  152. Caller: "live-api.rpc",
  153. UserIp: "127.0.0.1",
  154. SourceGroup: "default",
  155. }
  156. var ctx = context.TODO()
  157. for i := 0; i < b.N; i++ { //use b.N for looping
  158. id := rand.Intn(10000)
  159. arg := &v1.RoomGetInfoReq{Id: int64(id)}
  160. cli := liverpc.NewClient(&liverpc.ClientConfig{AppID: "live.room"})
  161. var rpcClient = v1.NewRoomRPCClient(cli)
  162. _, err := rpcClient.GetInfo(ctx, arg, liverpc.HeaderOption{Header: header})
  163. if err != nil {
  164. b.Errorf("%s %d", err, i)
  165. b.FailNow()
  166. }
  167. }
  168. }