server_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package server
  2. import (
  3. "fmt"
  4. "net/rpc"
  5. "testing"
  6. "time"
  7. "go-common/app/service/main/resource/conf"
  8. "go-common/app/service/main/resource/model"
  9. "go-common/app/service/main/resource/service"
  10. )
  11. // rpc server const
  12. const (
  13. addr = "127.0.0.1:6429"
  14. _resourceAll = "RPC.ResourceAll"
  15. _assignmentAll = "RPC.AssignmentAll"
  16. _defBanner = "RPC.DefBanner"
  17. _resource = "RPC.Resource"
  18. _resources = "RPC.Resources"
  19. _assignment = "RPC.Assignment"
  20. _banners = "RPC.Banners"
  21. _pasterAPP = "RPC.PasterAPP"
  22. _indexIcon = "RPC.IndexIcon"
  23. _playerIcon = "RPC.playerIcon"
  24. _cmtbox = "RPC.Cmtbox"
  25. _sidebars = "RPC.SideBars"
  26. _abtest = "RPC.AbTest"
  27. _pasterCID = "RPC.PasterCID"
  28. )
  29. // TestResource test rpc server
  30. func TestResource(t *testing.T) {
  31. if err := conf.Init(); err != nil {
  32. t.Errorf("conf.Init() error(%v)", err)
  33. t.FailNow()
  34. }
  35. svr := service.New(conf.Conf)
  36. New(conf.Conf, svr)
  37. time.Sleep(time.Second * 3)
  38. client, err := rpc.Dial("tcp", addr)
  39. defer client.Close()
  40. if err != nil {
  41. t.Errorf("rpc.Dial(tcp, \"%s\") error(%v)", addr, err)
  42. t.FailNow()
  43. }
  44. resourceAllRPC(client, t)
  45. assignmentAllRPC(client, t)
  46. defBannerRPC(client, t)
  47. resourceRPC(client, t)
  48. resourcesRPC(client, t)
  49. assignmentRPC(client, t)
  50. bannersRPC(client, t)
  51. pasterAPPRpc(client, t)
  52. indexIconRPC(client, t)
  53. playerIconRPC(client, t)
  54. cmtboxRPC(client, t)
  55. sideBarsRPC(client, t)
  56. abTestRPC(client, t)
  57. pasterCIDRPC(client, t)
  58. }
  59. func resourceAllRPC(client *rpc.Client, t *testing.T) {
  60. var res []*model.Resource
  61. arg := &struct{}{}
  62. if err := client.Call(_resourceAll, arg, &res); err != nil {
  63. t.Errorf("err: %v.", err)
  64. } else {
  65. result("resourceAll", t, res)
  66. }
  67. }
  68. func assignmentAllRPC(client *rpc.Client, t *testing.T) {
  69. var res []*model.Assignment
  70. arg := &struct{}{}
  71. if err := client.Call(_assignmentAll, arg, &res); err != nil {
  72. t.Errorf("err: %v.", err)
  73. } else {
  74. result("assignmentAll", t, res)
  75. }
  76. }
  77. func defBannerRPC(client *rpc.Client, t *testing.T) {
  78. var res model.Assignment
  79. arg := &struct{}{}
  80. if err := client.Call(_defBanner, arg, &res); err != nil {
  81. t.Errorf("err: %v.", err)
  82. } else {
  83. result("defBanner", t, res)
  84. }
  85. }
  86. func resourceRPC(client *rpc.Client, t *testing.T) {
  87. var res model.Resource
  88. arg := &model.ArgRes{
  89. ResID: 1187,
  90. }
  91. if err := client.Call(_resource, arg, &res); err != nil {
  92. t.Errorf("err: %v.", err)
  93. } else {
  94. result("resource", t, res)
  95. }
  96. }
  97. func resourcesRPC(client *rpc.Client, t *testing.T) {
  98. var res map[int]*model.Resource
  99. arg := &model.ArgRess{
  100. ResIDs: []int{1187, 1639},
  101. }
  102. if err := client.Call(_resources, arg, &res); err != nil {
  103. t.Errorf("err: %v.", err)
  104. } else {
  105. result("resources", t, res)
  106. }
  107. }
  108. func assignmentRPC(client *rpc.Client, t *testing.T) {
  109. var res []*model.Assignment
  110. arg := &model.ArgRes{
  111. ResID: 1187,
  112. }
  113. if err := client.Call(_assignment, arg, &res); err != nil {
  114. t.Errorf("err: %v.", err)
  115. } else {
  116. result("assignment", t, res)
  117. }
  118. }
  119. func bannersRPC(client *rpc.Client, t *testing.T) {
  120. var res *model.Banners
  121. arg := &model.ArgBanner{
  122. Plat: 1,
  123. ResIDs: "454,467",
  124. Build: 508000,
  125. MID: 1493031,
  126. Channel: "abc",
  127. IP: "211.139.80.6",
  128. Buvid: "123",
  129. Network: "wifi",
  130. MobiApp: "iphone",
  131. Device: "test",
  132. IsAd: true,
  133. OpenEvent: "abc",
  134. }
  135. if err := client.Call(_banners, arg, &res); err != nil {
  136. t.Errorf("err: %v.", err)
  137. } else {
  138. result("banners", t, res)
  139. }
  140. }
  141. func pasterAPPRpc(client *rpc.Client, t *testing.T) {
  142. var res model.Paster
  143. arg := &model.ArgPaster{
  144. Platform: int8(1),
  145. AdType: int8(1),
  146. Aid: "666666",
  147. TypeId: "11",
  148. Buvid: "666666",
  149. }
  150. if err := client.Call(_pasterAPP, arg, &res); err != nil {
  151. t.Errorf("err: %v.", err)
  152. } else {
  153. result("pasterAPPRpc", t, res)
  154. }
  155. }
  156. func indexIconRPC(client *rpc.Client, t *testing.T) {
  157. var res map[string][]*model.IndexIcon
  158. arg := &struct{}{}
  159. if err := client.Call(_indexIcon, arg, &res); err != nil {
  160. t.Errorf("err: %v.", err)
  161. } else {
  162. result("indexIconRpc", t, res)
  163. }
  164. }
  165. func playerIconRPC(client *rpc.Client, t *testing.T) {
  166. var res *model.PlayerIcon
  167. arg := &struct{}{}
  168. if err := client.Call(_playerIcon, arg, &res); err != nil {
  169. t.Errorf("err: %v.", err)
  170. } else {
  171. result("playerIconRPC", t, res)
  172. }
  173. }
  174. func cmtboxRPC(client *rpc.Client, t *testing.T) {
  175. var res model.Cmtbox
  176. arg := &model.ArgCmtbox{
  177. ID: 1,
  178. }
  179. if err := client.Call(_cmtbox, arg, &res); err != nil {
  180. t.Errorf("err: %v.", err)
  181. } else {
  182. result("resource", t, res)
  183. }
  184. }
  185. func sideBarsRPC(client *rpc.Client, t *testing.T) {
  186. var res []*model.SideBars
  187. arg := &struct{}{}
  188. if err := client.Call(_sidebars, arg, &res); err != nil {
  189. t.Errorf("err: %v.", err)
  190. } else {
  191. result("sideBars", t, res)
  192. }
  193. }
  194. func abTestRPC(client *rpc.Client, t *testing.T) {
  195. var res map[string]*model.AbTest
  196. arg := &model.ArgAbTest{
  197. Groups: "不显示热门tab,显示热门tab",
  198. IP: "127.0.0.1",
  199. }
  200. if err := client.Call(_abtest, arg, &res); err != nil {
  201. t.Errorf("err: %v.", err)
  202. } else {
  203. result("abTest", t, res)
  204. }
  205. }
  206. func pasterCIDRPC(client *rpc.Client, t *testing.T) {
  207. var res map[int64]int64
  208. arg := &struct{}{}
  209. if err := client.Call(_pasterCID, arg, &res); err != nil {
  210. t.Errorf("err: %v.", err)
  211. } else {
  212. result("pasterCID", t, res)
  213. }
  214. }
  215. func result(name string, t *testing.T, res interface{}) {
  216. fmt.Printf("res : %+v \n", res)
  217. t.Log("[==========" + name + "单元测试结果==========]")
  218. t.Log(res)
  219. t.Log("[↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑]\r\n")
  220. }