livezk_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package livezk
  2. import (
  3. "context"
  4. "fmt"
  5. "path"
  6. "testing"
  7. "time"
  8. "go-common/library/naming"
  9. xtime "go-common/library/time"
  10. )
  11. var appdID = "main.arch.test6"
  12. var addr = "127.0.0.1:8080"
  13. var ins1 = &naming.Instance{
  14. AppID: appdID,
  15. Addrs: []string{"grpc://" + addr},
  16. Version: "1",
  17. Metadata: map[string]string{
  18. "test": "1",
  19. "color": "blue",
  20. },
  21. }
  22. var addrs = []string{"172.18.33.131:2181", "172.18.33.168:2181", "172.18.33.169:2181"}
  23. var zkConfig = &Zookeeper{
  24. Addrs: addrs,
  25. Timeout: xtime.Duration(time.Second),
  26. }
  27. func TestLiveZK(t *testing.T) {
  28. reg, err := New(zkConfig)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. cancel, err := reg.Register(context.TODO(), ins1)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. defer cancel()
  37. lzk := reg.(*livezk)
  38. nodePath := path.Join(basePath, appdID, addr)
  39. ok, _, err := lzk.zkConn.Exists(nodePath)
  40. if err != nil {
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. }
  45. if !ok {
  46. t.Errorf("path not exists %s", nodePath)
  47. }
  48. }
  49. func TestLiveZKCancel(t *testing.T) {
  50. reg, err := New(zkConfig)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. cancel, err := reg.Register(context.TODO(), ins1)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. cancel()
  59. lzk := reg.(*livezk)
  60. nodePath := path.Join(basePath, fmt.Sprintf("b%s", ins1.AppID), addr)
  61. ok, _, err := lzk.zkConn.Exists(nodePath)
  62. if err != nil {
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. }
  67. if ok {
  68. t.Errorf("path should not exists %s", nodePath)
  69. }
  70. }