example_test.go 621 B

123456789101112131415161718192021222324252627282930
  1. package election
  2. import (
  3. "go-common/library/log"
  4. "testing"
  5. "time"
  6. )
  7. func TestElection(t *testing.T) {
  8. //log.AddFilter("all", log.DEBUG, log.NewConsoleLogWriter())
  9. var hosts = []string{"172.18.33.50:2199"}
  10. var root = "/microservice/upcredit-service/nodes"
  11. var elect = New(hosts, root, time.Second*5)
  12. var err = elect.Init()
  13. if err != nil {
  14. log.Error("fail to init elect")
  15. return
  16. }
  17. elect.Elect()
  18. for {
  19. isMaster := <-elect.C
  20. if isMaster {
  21. log.Info("this is master, node=%s", elect.NodePath)
  22. } else {
  23. log.Info("this is follower, node=%s, master=%s", elect.NodePath, elect.MasterPath)
  24. }
  25. }
  26. }