rpc_test.go 802 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package server
  2. import (
  3. "net/rpc"
  4. "sync"
  5. "testing"
  6. "time"
  7. "go-common/app/service/main/usersuit/conf"
  8. "go-common/app/service/main/usersuit/service"
  9. "go-common/library/log"
  10. )
  11. const (
  12. addr = "127.0.0.1:7269"
  13. _testPing = "RPC.Ping"
  14. )
  15. var (
  16. _noArg = &struct{}{}
  17. client *rpc.Client
  18. once sync.Once
  19. )
  20. func startServer() {
  21. if err := conf.Init(); err != nil {
  22. panic(err)
  23. }
  24. log.Init(conf.Conf.Xlog)
  25. defer log.Close()
  26. svr := service.New(conf.Conf)
  27. New(conf.Conf, svr)
  28. time.Sleep(time.Second * 3)
  29. var err error
  30. client, err = rpc.Dial("tcp", addr)
  31. if err != nil {
  32. panic(err)
  33. }
  34. }
  35. func TestRPC_Ping(t *testing.T) {
  36. once.Do(startServer)
  37. if err := client.Call(_testPing, &_noArg, &_noArg); err != nil {
  38. t.Errorf("client.Call(%s) error(%v)", _testPing, err)
  39. t.FailNow()
  40. }
  41. }