Beispiel #1
0
func (s *S) TestClientIdAndReInit(c *C) {
	zk1, _ := s.init(c)
	clientId1 := zk1.ClientId()

	zk2, _, err := gozk.ReInit(s.zkAddr, 5e9, clientId1)
	c.Assert(err, IsNil)
	defer zk2.Close()
	clientId2 := zk2.ClientId()

	c.Assert(clientId1, Equals, clientId2)
}
Beispiel #2
0
func (s *S) TestWatchOnSessionExpiration(c *C) {
	c.Check(gozk.CountPendingWatches(), Equals, 0)

	zk, session := s.init(c)

	event := <-session
	c.Assert(event.Type, Equals, gozk.EVENT_SESSION)
	c.Assert(event.State, Equals, gozk.STATE_CONNECTED)

	c.Check(gozk.CountPendingWatches(), Equals, 1)

	stat, watch, err := zk.ExistsW("/test")
	c.Assert(err, IsNil)
	c.Assert(stat, IsNil)

	c.Check(gozk.CountPendingWatches(), Equals, 2)

	// Use expiration trick described in the FAQ.
	clientId := zk.ClientId()
	zk2, session2, err := gozk.ReInit(s.zkAddr, 5e9, clientId)

	for event := range session2 {
		c.Log("Event from overlapping session: ", event)
		if event.State == gozk.STATE_CONNECTED {
			// Wait for zk to process the connection.
			// Not reliable without this. :-(
			time.Sleep(1e9)
			zk2.Close()
		}
	}
	for event := range session {
		c.Log("Event from primary session: ", event)
		if event.State == gozk.STATE_EXPIRED_SESSION {
			break
		}
	}

	select {
	case event := <-watch:
		c.Assert(event.State, Equals, gozk.STATE_EXPIRED_SESSION)
	case <-time.After(3e9):
		c.Fatal("Watch event didn't fire")
	}

	event = <-watch
	c.Assert(event.Type, Equals, gozk.EVENT_CLOSED)
	c.Assert(event.State, Equals, gozk.STATE_CLOSED)

	c.Check(gozk.CountPendingWatches(), Equals, 1)
}