rpcserver_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package gorpc
  2. import (
  3. "context"
  4. "flag"
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "go-common/app/service/main/up/model"
  9. "go-common/library/net/rpc"
  10. xtime "go-common/library/time"
  11. "github.com/davecgh/go-spew/spew"
  12. )
  13. const (
  14. /*clientConfigStr = `
  15. proto = "tcp"
  16. timeout = "1s"
  17. timer = 1000
  18. token = "123456"
  19. addr = "127.0.0.1:6079"
  20. [breaker]
  21. window = "3s"
  22. sleep = "100ms"
  23. bucket = 10
  24. ratio = 0.5
  25. request = 100`*/
  26. _Special = "RPC.Special"
  27. _Info = "RPC.Info"
  28. _SetUpSwitch = "RPC.SetUpSwitch"
  29. _UpSwitch = "RPC.UpSwitch"
  30. _UpCards = "RPC.UpCards"
  31. )
  32. func init() {
  33. dir, _ := filepath.Abs("../../cmd/up-service.toml")
  34. flag.Set("conf", dir)
  35. }
  36. func initSvrAndClient(t *testing.T) (client *rpc.Client, err error) {
  37. client = rpc.Dial("127.0.0.1:6079", xtime.Duration(time.Second), nil)
  38. return
  39. }
  40. func TestInfo(t *testing.T) {
  41. client, err := initSvrAndClient(t)
  42. if err != nil {
  43. t.Errorf("rpc.Dial error(%v)", err)
  44. t.FailNow()
  45. }
  46. defer client.Close()
  47. //time.Sleep(1 * time.Second)
  48. info(client, t)
  49. }
  50. func info(client *rpc.Client, t *testing.T) {
  51. var res *model.UpInfo
  52. arg := &model.ArgInfo{
  53. Mid: 2089809,
  54. From: 1,
  55. }
  56. err := client.Call(context.TODO(), _Info, arg, &res)
  57. if err != nil {
  58. t.Logf("err:%v.", err)
  59. }
  60. spew.Dump(res)
  61. }
  62. func TestSpecial(t *testing.T) {
  63. client, err := initSvrAndClient(t)
  64. if err != nil {
  65. t.Errorf("rpc.Dial error(%v)", err)
  66. t.FailNow()
  67. }
  68. defer client.Close()
  69. //time.Sleep(1 * time.Second)
  70. special(client, t)
  71. }
  72. func special(client *rpc.Client, t *testing.T) {
  73. var res []model.UpSpecial
  74. arg := &model.ArgSpecial{
  75. GroupID: 2,
  76. }
  77. err := client.Call(context.TODO(), _Special, arg, &res)
  78. if err != nil {
  79. t.Logf("err:%v.", err)
  80. }
  81. spew.Dump(res)
  82. }
  83. func Test_UpSwitch(t *testing.T) {
  84. client, err := initSvrAndClient(t)
  85. if err != nil {
  86. t.Errorf("rpc.Dial error(%v)", err)
  87. t.FailNow()
  88. }
  89. defer client.Close()
  90. var res *model.PBUpSwitch
  91. arg := &model.ArgUpSwitch{
  92. Mid: 1,
  93. From: 0,
  94. }
  95. err = client.Call(context.TODO(), _UpSwitch, arg, &res)
  96. if err != nil {
  97. t.Logf("err:%v.", err)
  98. }
  99. spew.Dump(11111, res)
  100. }
  101. func Test_SetUpSwitch(t *testing.T) {
  102. client, err := initSvrAndClient(t)
  103. if err != nil {
  104. t.Errorf("rpc.Dial error(%v)", err)
  105. t.FailNow()
  106. }
  107. defer client.Close()
  108. var res *model.PBSetUpSwitchRes
  109. arg := &model.ArgUpSwitch{
  110. Mid: 1,
  111. From: 0,
  112. State: 1,
  113. }
  114. err = client.Call(context.TODO(), _SetUpSwitch, arg, &res)
  115. if err != nil {
  116. t.Logf("err:%v.", err)
  117. }
  118. spew.Dump(11111, res)
  119. }
  120. func Test_UpCards(t *testing.T) {
  121. client, err := initSvrAndClient(t)
  122. if err != nil {
  123. t.Errorf("rpc.Dial error(%v)", err)
  124. t.FailNow()
  125. }
  126. defer client.Close()
  127. arg := &model.ListUpCardInfoArg{
  128. Pn: 1,
  129. Ps: 1,
  130. }
  131. var res *model.UpCardInfoPage
  132. err = client.Call(context.TODO(), _UpCards, arg, &res)
  133. if err != nil {
  134. t.Logf("err:%v.", err)
  135. }
  136. spew.Dump(11111, res)
  137. }