server_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package grpc
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. pb "go-common/app/service/main/location/api"
  8. "go-common/app/service/main/location/conf"
  9. "go-common/app/service/main/location/service"
  10. "go-common/library/net/rpc/warden"
  11. xtime "go-common/library/time"
  12. )
  13. // rpc server const
  14. const (
  15. addr = "127.0.0.1:9000"
  16. )
  17. // TestLocation test rpc server
  18. func TestLocation(t *testing.T) {
  19. if err := conf.Init(); err != nil {
  20. t.Errorf("conf.Init() error(%v)", err)
  21. t.FailNow()
  22. }
  23. svr := service.New(conf.Conf)
  24. New(conf.Conf.WardenServer, svr)
  25. time.Sleep(time.Second * 3)
  26. cfg := &warden.ClientConfig{
  27. Dial: xtime.Duration(time.Second * 3),
  28. Timeout: xtime.Duration(time.Second * 3),
  29. }
  30. cc, err := warden.NewClient(cfg).Dial(context.Background(), addr)
  31. if err != nil {
  32. t.Errorf("rpc.Dial(tcp, \"%s\") error(%v)", addr, err)
  33. t.FailNow()
  34. }
  35. client := pb.NewLocationClient(cc)
  36. infoGRPC(client, t)
  37. }
  38. func infoGRPC(client pb.LocationClient, t *testing.T) {
  39. arg := &pb.InfoReq{
  40. Addr: "211.139.80.6",
  41. }
  42. res, err := client.Info(context.TODO(), arg)
  43. if err != nil {
  44. t.Error(err)
  45. } else {
  46. result("info", t, res)
  47. }
  48. }
  49. func result(name string, t *testing.T, res interface{}) {
  50. fmt.Printf("res : %+v \n", res)
  51. t.Log("[==========" + name + "单元测试结果==========]")
  52. t.Log(res)
  53. t.Log("[↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑]\r\n")
  54. }