func (c *ConfigClientTestSuite) TestNewWatchFailed() { // new etcd client with port forwarding port := 23123 root := "/watch" stop, err := portforward.PortForward(fmt.Sprintf("localhost:%d", port), fmt.Sprintf("localhost:%d", c.srvPort)) assert.NoError(c.T(), err) etcdCli := etcd.NewClient([]string{fmt.Sprintf("http://localhost:%d", port)}) assert.NotNil(c.T(), etcdCli) _, err = etcdCli.SetDir(root, 0) assert.NoError(c.T(), err) ch := make(chan struct{}, 1) fatalfFn = func(s string, iface ...interface{}) { ch <- struct{}{} logrus.Infof("Called fatalf:%s", fmt.Sprintf(s, iface...)) } etcdCli.Set(fmt.Sprintf(infoPrefix, root), "{}", 0) cli, err := NewClient(etcdCli, root) assert.NoError(c.T(), err) // sleep for 1 sec and disconnect etcd time.Sleep(time.Second) close(stop) select { case <-ch: case <-time.After(10 * time.Second): c.T().Fail() } // close client and wait stopping all routines absolutely cli.Stop() time.Sleep(5 * time.Second) fatalfFn = logrus.Fatalf }
func portForwarder(from, to string) func() (chan struct{}, error) { return func() (chan struct{}, error) { return portforward.PortForward( from, to, ) } }
func (s *ephemeralSuite) TestWatchDisconnect() { if s.testZK { fmt.Println("this is skipped in zk due to samuel library issue.") return } testDir := "/watch-disconnect" // Make a node assert.NoError(s.T(), s.ephemeral.AddDir(context.Background(), testDir)) // Configure port forwarding to test network disconnection portForward := 4444 stop, err := pf.PortForward(fmt.Sprintf(":%d", portForward), fmt.Sprintf(":%d", s.port)) assert.NoError(s.T(), err, "no error") // Make a new storer to be disconnected later. eph2, err := s.newEphemeral(portForward) assert.NoError(s.T(), err) // List with watch. It should return current children children := eph2.List(context.Background(), testDir, true) c := <-children assert.NoError(s.T(), c.Err) assert.Equal(s.T(), []string{}, c.Children) // make sure that etcd watch has started time.Sleep(3 * time.Second) // Now disconnect close(stop) // list channel should get error and close. select { case c := <-children: assert.Error(s.T(), c.Err) assert.Equal(s.T(), []string(nil), c.Children) case <-time.After(5 * time.Second): assert.Fail(s.T(), "should receive") } select { case _, ok := <-children: assert.False(s.T(), ok) case <-time.After(5 * time.Second): assert.Fail(s.T(), "should receive close") } }