コード例 #1
0
ファイル: zk_test.go プロジェクト: koofr/gozk
func (s *S) TestClientIdAndReInit(c *C) {
	zk1, _ := s.init(c)
	clientId1 := zk1.ClientId()

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

	c.Assert(clientId1, DeepEquals, clientId2)
}
コード例 #2
0
ファイル: zk_test.go プロジェクト: koofr/gozk
func (s *S) TestWatchOnSessionExpiration(c *C) {
	c.Check(zk.CountPendingWatches(), Equals, 0)

	conn, session := s.init(c)

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

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

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

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

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

	for event := range session2 {
		c.Log("Event from overlapping session: ", event)
		if event.State == zk.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 == zk.STATE_EXPIRED_SESSION {
			break
		}
	}

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

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

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