func (s *topoTestSuite) SetUpSuite(c *C) { var conn zkhelper.Conn if len(*testZK) == 0 { //use fake zk conn = zkhelper.NewConn() } else { var err error conn, err = zkhelper.ConnectToZk(*testZK) c.Assert(err, IsNil) } s.t = NewTopo("myshard_test", conn) err := s.t.clear() c.Assert(err, IsNil) }
func newZk(cfg *Config, fsm *masterFSM) (Cluster, error) { z := new(Zk) var err error if !strings.HasPrefix(cfg.Zk.BaseDir, "/zk") { return nil, fmt.Errorf("invalid zk base dir %s, must have prefix /zk", cfg.Zk.BaseDir) } addr := strings.Join(cfg.Zk.Addr, ",") if addr == "memory" { // only for test log.Infof("only for test, use memory") z.conn = zkhelper.NewConn() } else { z.conn, err = zkhelper.ConnectToZk(addr) } if err != nil { return nil, err } z.c = cfg z.fsm = fsm z.isLeader.Set(false) z.leaderCh = make(chan bool, 1) z.actionCh = make(chan *zkAction, 10) z.quit = make(chan struct{}) if _, err = zkhelper.CreateOrUpdate(z.conn, cfg.Zk.BaseDir, "", 0, zkhelper.DefaultDirACLs(), true); err != nil { log.Errorf("create %s error: %v", cfg.Zk.BaseDir, err) return nil, err } onRetryLock := func() { z.noticeLeaderCh(false) } z.elector = createElection(z.conn, cfg.Zk.BaseDir, cfg.Addr, onRetryLock) z.checkLeader() return z, nil }