rpc_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package server
  2. import (
  3. "net/rpc"
  4. "sync"
  5. "testing"
  6. "time"
  7. "go-common/app/service/main/passport-auth/conf"
  8. "go-common/app/service/main/passport-auth/model"
  9. "go-common/app/service/main/passport-auth/service"
  10. "go-common/library/log"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. const (
  14. addr = "127.0.0.1:7579"
  15. _testPing = "RPC.Ping"
  16. // token
  17. _tokenInfo = "RPC.TokenInfo"
  18. _delToken = "RPC.DelToken"
  19. // cookie
  20. _cookieInfo = "RPC.CookieInfo"
  21. _delCookie = "RPC.DelCookie"
  22. )
  23. var (
  24. _noArg = &struct{}{}
  25. client *rpc.Client
  26. once sync.Once
  27. )
  28. func startServer() {
  29. if err := conf.Init(); err != nil {
  30. panic(err)
  31. }
  32. log.Init(conf.Conf.Xlog)
  33. defer log.Close()
  34. svr := service.New(conf.Conf)
  35. New(conf.Conf, svr)
  36. time.Sleep(time.Second * 3)
  37. var err error
  38. client, err = rpc.Dial("tcp", addr)
  39. if err != nil {
  40. panic(err)
  41. }
  42. }
  43. func TestRPC_Ping(t *testing.T) {
  44. once.Do(startServer)
  45. if err := client.Call(_testPing, &_noArg, &_noArg); err != nil {
  46. t.Errorf("client.Call(%s) error(%v)", _testPing, err)
  47. t.FailNow()
  48. }
  49. }
  50. func TestRPC_TokenInfo(t *testing.T) {
  51. var (
  52. err error
  53. arg = "64294c76972aee8cf4af51566c76ed0d"
  54. res *model.Token
  55. )
  56. once.Do(startServer)
  57. Convey("Test RPC server get token by token", t, func() {
  58. err = client.Call(_tokenInfo, arg, res)
  59. So(err, ShouldNotBeNil)
  60. So(res, ShouldNotBeEmpty)
  61. })
  62. }
  63. func TestRPC_DelToken(t *testing.T) {
  64. var (
  65. err error
  66. arg = "64294c76972aee8cf4af51566c76ed0d"
  67. res int64
  68. )
  69. once.Do(startServer)
  70. time.Sleep(3 * time.Second)
  71. Convey("Test RPC server del token by token", t, func() {
  72. err = client.Call(_delToken, arg, res)
  73. So(err, ShouldNotBeNil)
  74. So(res, ShouldNotBeEmpty)
  75. })
  76. }
  77. func TestRPC_CookieInfo(t *testing.T) {
  78. var (
  79. err error
  80. arg = "c1300f65,1519273116,f05bd5ef"
  81. res *model.Cookie
  82. )
  83. once.Do(startServer)
  84. Convey("Test RPC server get coookie by ssda", t, func() {
  85. err = client.Call(_cookieInfo, arg, res)
  86. So(err, ShouldNotBeNil)
  87. So(res, ShouldNotBeEmpty)
  88. })
  89. }
  90. func TestRPC_DelCookie(t *testing.T) {
  91. var (
  92. err error
  93. arg = "c1300f65,1519273116,f05bd5ef"
  94. res int64
  95. )
  96. once.Do(startServer)
  97. time.Sleep(3 * time.Second)
  98. Convey("Test RPC server del coookie by cookie", t, func() {
  99. err = client.Call(_delCookie, arg, res)
  100. So(err, ShouldNotBeNil)
  101. So(res, ShouldNotBeEmpty)
  102. })
  103. }