server_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package server
  2. import (
  3. "fmt"
  4. "net/rpc"
  5. "testing"
  6. "time"
  7. "go-common/app/service/main/location/conf"
  8. "go-common/app/service/main/location/model"
  9. "go-common/app/service/main/location/service"
  10. )
  11. // rpc server const
  12. const (
  13. addr = "127.0.0.1:6293"
  14. _archive = "RPC.Archive"
  15. _archive2 = "RPC.Archive2"
  16. _group = "RPC.Group"
  17. _authPIDs = "RPC.AuthPIDs"
  18. _info = "RPC.Info"
  19. _infos = "RPC.Infos"
  20. _infoComplete = "RPC.InfoComplete"
  21. _infosComplete = "RPC.InfosComplete"
  22. )
  23. // TestLocation test rpc server
  24. func TestLocation(t *testing.T) {
  25. if err := conf.Init(); err != nil {
  26. t.Errorf("conf.Init() error(%v)", err)
  27. t.FailNow()
  28. }
  29. svr := service.New(conf.Conf)
  30. New(conf.Conf, svr)
  31. time.Sleep(time.Second * 3)
  32. client, err := rpc.Dial("tcp", addr)
  33. defer client.Close()
  34. if err != nil {
  35. t.Errorf("rpc.Dial(tcp, \"%s\") error(%v)", addr, err)
  36. t.FailNow()
  37. }
  38. archiveRPC(client, t)
  39. archive2RPC(client, t)
  40. groupRPC(client, t)
  41. authPIDsRPC(client, t)
  42. infoRPC(client, t)
  43. infosRPC(client, t)
  44. infoCompleteRPC(client, t)
  45. infosCompleteRPC(client, t)
  46. }
  47. func archiveRPC(client *rpc.Client, t *testing.T) {
  48. var res int64
  49. arg := &model.Archive{
  50. Aid: 740955,
  51. Mid: 0,
  52. IP: "2.20.32.123",
  53. CIP: "127.0.0.1",
  54. }
  55. err := client.Call(_archive, arg, &res)
  56. if err != nil {
  57. t.Errorf("err:%v.", err)
  58. } else {
  59. result("archive", t, res)
  60. }
  61. t.Logf("%+v", res)
  62. }
  63. func archive2RPC(client *rpc.Client, t *testing.T) {
  64. res := &model.Auth{}
  65. arg := &model.Archive{
  66. Aid: 740955,
  67. Mid: 0,
  68. IP: "2.20.32.123",
  69. CIP: "127.0.0.1",
  70. }
  71. err := client.Call(_archive2, arg, &res)
  72. if err != nil {
  73. t.Errorf("err:%v.", err)
  74. } else {
  75. result("archive2", t, res)
  76. }
  77. t.Logf("%+v", res)
  78. }
  79. func groupRPC(client *rpc.Client, t *testing.T) {
  80. res := &model.Auth{}
  81. arg := &model.Group{
  82. Gid: 317,
  83. Mid: 0,
  84. IP: "2.20.32.123",
  85. CIP: "127.0.0.1",
  86. }
  87. err := client.Call(_group, arg, &res)
  88. if err != nil {
  89. t.Errorf("err:%v.", err)
  90. } else {
  91. result("group", t, res)
  92. }
  93. t.Logf("%+v", res)
  94. }
  95. func authPIDsRPC(client *rpc.Client, t *testing.T) {
  96. arg := &model.ArgPids{IP: "61.216.166.156", Pids: "150,92"}
  97. res := map[int64]*model.Auth{}
  98. err := client.Call(_authPIDs, arg, &res)
  99. if err != nil {
  100. t.Errorf("err:%v.", err)
  101. } else {
  102. result("authPIDs", t, res)
  103. }
  104. t.Logf("%+v,err:%v.", res, err)
  105. }
  106. func infoRPC(client *rpc.Client, t *testing.T) {
  107. var res = new(model.Info)
  108. arg := &model.ArgIP{
  109. IP: "139.214.144.59",
  110. }
  111. err := client.Call(_info, arg, res)
  112. if err != nil {
  113. t.Errorf("err:%v.", err)
  114. } else {
  115. result("info", t, res)
  116. }
  117. t.Logf("%+v,err:%v.", res, err)
  118. }
  119. func infosRPC(client *rpc.Client, t *testing.T) {
  120. arg := []string{"61.216.166.156", "211.139.80.6"}
  121. res := map[string]*model.Info{}
  122. err := client.Call(_infos, arg, &res)
  123. if err != nil {
  124. t.Errorf("err:%v.", err)
  125. } else {
  126. result("infos", t, res)
  127. }
  128. t.Logf("%+v,err:%v.", res, err)
  129. }
  130. func infoCompleteRPC(client *rpc.Client, t *testing.T) {
  131. var res = new(model.InfoComplete)
  132. arg := &model.ArgIP{
  133. IP: "139.214.144.59",
  134. }
  135. err := client.Call(_infoComplete, arg, res)
  136. if err != nil {
  137. t.Errorf("err:%v.", err)
  138. } else {
  139. result("infoComplete", t, res)
  140. }
  141. t.Logf("%+v,err:%v.", res, err)
  142. }
  143. func infosCompleteRPC(client *rpc.Client, t *testing.T) {
  144. arg := []string{"61.216.166.156", "211.139.80.6"}
  145. res := map[string]*model.InfoComplete{}
  146. err := client.Call(_infosComplete, arg, &res)
  147. if err != nil {
  148. t.Errorf("err:%v.", err)
  149. } else {
  150. result("infosComplete", t, res)
  151. }
  152. t.Logf("%+v,err:%v.", res, err)
  153. }
  154. func result(name string, t *testing.T, res interface{}) {
  155. fmt.Printf("res : %+v \n", res)
  156. t.Log("[==========" + name + "单元测试结果==========]")
  157. t.Log(res)
  158. t.Log("[↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑]\r\n")
  159. }