rpc_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package server
  2. import (
  3. "flag"
  4. "net/rpc"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "go-common/app/interface/main/dm2/conf"
  9. "go-common/app/interface/main/dm2/model"
  10. "go-common/app/interface/main/dm2/service"
  11. rpcx "go-common/library/net/rpc"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. var (
  15. client *rpc.Client
  16. _noArg = &struct{}{}
  17. )
  18. const (
  19. _addr = "127.0.0.1:6709"
  20. _subjectInfos = "RPC.SubjectInfos"
  21. _buyAdvance = "RPC.BuyAdvance"
  22. _advanceState = "RPC.AdvanceState"
  23. _advances = "RPC.Advances"
  24. _passAdvance = "RPC.PassAdvance"
  25. _denyAdvance = "RPC.DenyAdvance"
  26. _cancelAdvance = "RPC.CancelAdvance"
  27. _mask = "RPC.Mask"
  28. )
  29. func TestMain(m *testing.M) {
  30. var err error
  31. dir, _ := filepath.Abs("../../cmd/dm2-test.toml")
  32. if err = flag.Set("conf", dir); err != nil {
  33. panic(err)
  34. }
  35. if err = conf.Init(); err != nil {
  36. panic(err)
  37. }
  38. svr := service.New(conf.Conf)
  39. r := &RPC{s: svr}
  40. server := rpcx.NewServer(conf.Conf.RPCServer)
  41. if err = server.Register(r); err != nil {
  42. panic(err)
  43. }
  44. if client, err = rpc.Dial("tcp", _addr); err != nil {
  45. panic(err)
  46. }
  47. os.Exit(m.Run())
  48. }
  49. func TestSubjectInfos(t *testing.T) {
  50. var (
  51. tp int32 = 1
  52. oids = []int64{1221, 1231}
  53. res = make(map[int64]*model.SubjectInfo)
  54. )
  55. Convey("get dm subject info", t, func() {
  56. arg := model.ArgOids{Type: tp, Oids: oids}
  57. err := client.Call(_subjectInfos, arg, &res)
  58. So(err, ShouldBeNil)
  59. So(res, ShouldNotBeEmpty)
  60. for cid, r := range res {
  61. t.Logf("=====cid:%d Infos:%+v", cid, r)
  62. }
  63. })
  64. }
  65. func TestBuyAdvance(t *testing.T) {
  66. var (
  67. mid int64 = 27515260
  68. cid int64 = 10107292
  69. mode = "sp"
  70. )
  71. Convey("buy advance dm", t, func() {
  72. arg := &model.ArgAdvance{
  73. Mid: mid,
  74. Cid: cid,
  75. Mode: mode,
  76. }
  77. err := client.Call(_buyAdvance, arg, _noArg)
  78. So(err, ShouldBeNil)
  79. })
  80. }
  81. func TestAdvanceState(t *testing.T) {
  82. var (
  83. mid int64 = 27515330
  84. cid int64 = 10107292
  85. mode = "sp"
  86. res = &model.AdvState{}
  87. )
  88. Convey("get advance dm state", t, func() {
  89. arg := &model.ArgAdvance{
  90. Mid: mid,
  91. Cid: cid,
  92. Mode: mode,
  93. }
  94. err := client.Call(_advanceState, arg, res)
  95. So(err, ShouldBeNil)
  96. So(res, ShouldNotBeEmpty)
  97. })
  98. }
  99. func TestAdvances(t *testing.T) {
  100. var (
  101. mid int64 = 27515260
  102. res = make([]*model.Advance, 10)
  103. )
  104. Convey("get advances dm", t, func() {
  105. arg := &model.ArgMid{
  106. Mid: mid,
  107. }
  108. err := client.Call(_advances, arg, &res)
  109. So(err, ShouldBeNil)
  110. So(res, ShouldNotBeEmpty)
  111. })
  112. }
  113. func TestPassAdvance(t *testing.T) {
  114. var (
  115. mid int64 = 7158471
  116. id int64 = 2
  117. )
  118. Convey("pass advance dm ", t, func() {
  119. arg := &model.ArgUpAdvance{
  120. Mid: mid,
  121. ID: id,
  122. }
  123. err := client.Call(_passAdvance, arg, _noArg)
  124. So(err, ShouldBeNil)
  125. })
  126. }
  127. func TestDenyAdvance(t *testing.T) {
  128. var (
  129. mid int64 = 27515615
  130. id int64 = 107
  131. )
  132. Convey("deny advance dm", t, func() {
  133. arg := &model.ArgUpAdvance{
  134. Mid: mid,
  135. ID: id,
  136. }
  137. err := client.Call(_denyAdvance, arg, _noArg)
  138. So(err, ShouldBeNil)
  139. })
  140. }
  141. func TestCancelAdvance(t *testing.T) {
  142. var (
  143. mid int64 = 27515615
  144. id int64 = 122
  145. )
  146. Convey("cancel advance dm", t, func() {
  147. arg := &model.ArgUpAdvance{
  148. Mid: mid,
  149. ID: id,
  150. }
  151. err := client.Call(_cancelAdvance, arg, _noArg)
  152. So(err, ShouldBeNil)
  153. })
  154. }
  155. func TestMask(t *testing.T) {
  156. var (
  157. cid int64 = 32
  158. res = &model.Mask{}
  159. )
  160. Convey("test mask list", t, func() {
  161. arg := &model.ArgMask{
  162. Cid: cid,
  163. }
  164. err := client.Call(_mask, arg, res)
  165. t.Logf("=========%+v", res)
  166. So(err, ShouldBeNil)
  167. })
  168. }