示例#1
0
func (*APIOpenerSuite) TestTimoutClosesAPIOnTimeout(c *gc.C) {
	var name string
	finished := make(chan struct{})
	mockConn := &mockConnection{closed: make(chan struct{})}
	open := func(connectionName string) (api.Connection, error) {
		<-finished
		name = connectionName
		return mockConn, nil
	}
	// have the mock clock only wait a microsecond
	clock := &mockClock{wait: time.Microsecond}
	// but tell it to wait five seconds
	opener := envcmd.NewTimeoutOpener(open, clock, 5*time.Second)
	conn, err := opener.Open("a-name")
	c.Assert(errors.Cause(err), gc.Equals, envcmd.ErrConnTimedOut)
	c.Assert(conn, gc.IsNil)
	// check it was told to wait for 5 seconds
	c.Assert(clock.duration, gc.Equals, 5*time.Second)
	// tell the open func to continue now we have timed out
	close(finished)
	// wait until the connection has been closed
	select {
	case <-mockConn.closed:
		// continue
	case <-time.After(5 * time.Second):
		c.Error("API connection was not closed.")
	}
	c.Assert(name, gc.Equals, "a-name")
}
示例#2
0
文件: kill.go 项目: imoapps/juju
// wrapKillCommand provides the common wrapping used by tests and
// the default NewKillCommand above.
func wrapKillCommand(kill *killCommand, fn func(string) (api.Connection, error), clock clock.Clock) cmd.Command {
	if fn == nil {
		fn = kill.JujuCommandBase.NewAPIRoot
	}
	openStrategy := envcmd.NewTimeoutOpener(fn, clock, 10*time.Second)
	return envcmd.Wrap(
		kill,
		envcmd.EnvSkipFlags,
		envcmd.EnvSkipDefault,
		envcmd.EnvAPIOpener(openStrategy),
	)
}
示例#3
0
func (*APIOpenerSuite) TestTimoutErrors(c *gc.C) {
	var name string
	open := func(connectionName string) (api.Connection, error) {
		name = connectionName
		return nil, errors.New("boom")
	}
	opener := envcmd.NewTimeoutOpener(open, clock.WallClock, 10*time.Second)
	conn, err := opener.Open("a-name")
	c.Assert(err, gc.ErrorMatches, "boom")
	c.Assert(conn, gc.IsNil)
	c.Assert(name, gc.Equals, "a-name")
}
示例#4
0
func (*APIOpenerSuite) TestTimoutSuccess(c *gc.C) {
	var name string
	open := func(connectionName string) (api.Connection, error) {
		name = connectionName
		return &mockConnection{}, nil
	}
	opener := envcmd.NewTimeoutOpener(open, clock.WallClock, 10*time.Second)
	conn, err := opener.Open("a-name")
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(conn, gc.NotNil)
	c.Assert(name, gc.Equals, "a-name")
}