// cases to test: // child server, stopped normally; reattach, start // non-direct child server, killed abnormally; reattach, start (->error), remove pid.txt; start // non-direct child server, still running; reattach, start (->error), stop, start // child server, still running; reattach, start (-> error) // child server, still running; reattach, stop, start. // non-direct child server, still running; reattach, stop, start. func (s *S) TestAttachServer(c *C) { // Create a cookie so that we know we are reattaching to the same instance. conn, _ := s.init(c) _, err := conn.Create("/testAttachCookie", "testAttachCookie", 0, zk.WorldACL(zk.PERM_ALL)) c.Assert(err, IsNil) s.checkCookie(c) s.zkServer.Stop() s.zkServer = nil s.testAttachServer(c, (*S).startServer) s.testAttachServer(c, (*S).startServerIndirect) s.testAttachServerAbnormalTerminate(c, (*S).startServer) s.testAttachServerAbnormalTerminate(c, (*S).startServerIndirect) srv, err := zk.AttachServer(s.zkTestRoot) c.Assert(err, IsNil) s.zkServer = srv err = s.zkServer.Start() c.Assert(err, IsNil) conn, _ = s.init(c) err = conn.Delete("/testAttachCookie", -1) c.Assert(err, IsNil) }
func (s *S) testAttachServerAbnormalTerminate(c *C, start func(*S, *C, bool)) { start(s, c, true) // try attaching to it and starting - it should fail, because pid.txt // won't have been removed. srv, err := zk.AttachServer(s.zkTestRoot) c.Assert(err, IsNil) err = srv.Start() c.Assert(err, NotNil) // stopping it should bring things back to normal. err = srv.Stop() c.Assert(err, IsNil) err = srv.Start() c.Assert(err, IsNil) s.checkCookie(c) err = srv.Stop() c.Assert(err, IsNil) }
// startServer starts a ZooKeeper server, and terminates it abnormally // if abort is true. func startServer(runDir string, abort bool) error { srv, err := zk.AttachServer(runDir) if err != nil { return fmt.Errorf("cannot attach to server at %q: %v", runDir, err) } if err := srv.Start(); err != nil { return fmt.Errorf("cannot start server: %v", err) } if abort { // Give it time to start up, then kill the server process abnormally, // leaving the pid.txt file behind. time.Sleep(0.5e9) p, err := srv.Process() if err != nil { return fmt.Errorf("cannot get server process: %v", err) } defer p.Release() if err := p.Kill(); err != nil { return fmt.Errorf("cannot kill server process: %v", err) } } return nil }
func (s *S) testAttachServer(c *C, start func(*S, *C, bool)) { start(s, c, false) s.checkCookie(c) // try attaching to it while it is still running - it should fail. srv, err := zk.AttachServer(s.zkTestRoot) c.Assert(err, IsNil) err = srv.Start() c.Assert(err, NotNil) // stop it and then start it again - it should succeed. err = srv.Stop() c.Assert(err, IsNil) err = srv.Start() c.Assert(err, IsNil) s.checkCookie(c) err = srv.Stop() c.Assert(err, IsNil) }