func (*APIOpenerSuite) TestTimoutClosesAPIOnTimeout(c *gc.C) { var controllerName, accountName, modelName string finished := make(chan struct{}) mockConn := &mockConnection{closed: make(chan struct{})} open := func(_ jujuclient.ClientStore, controllerNameArg, accountNameArg, modelNameArg string) (api.Connection, error) { <-finished controllerName = controllerNameArg accountName = accountNameArg modelName = modelNameArg return mockConn, nil } // have the mock clock only wait a microsecond clock := &mockClock{wait: time.Microsecond} // but tell it to wait five seconds opener := modelcmd.NewTimeoutOpener(modelcmd.OpenFunc(open), clock, 5*time.Second) conn, err := opener.Open(nil, "a-name", "b-name", "c-name") c.Assert(errors.Cause(err), gc.Equals, modelcmd.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(controllerName, gc.Equals, "a-name") c.Assert(accountName, gc.Equals, "b-name") c.Assert(modelName, gc.Equals, "c-name") }
// wrapKillCommand provides the common wrapping used by tests and // the default NewKillCommand above. func wrapKillCommand(kill *killCommand, apiOpen modelcmd.APIOpener, clock clock.Clock) cmd.Command { if apiOpen == nil { apiOpen = modelcmd.OpenFunc(kill.JujuCommandBase.NewAPIRoot) } openStrategy := modelcmd.NewTimeoutOpener(apiOpen, clock, 10*time.Second) return modelcmd.WrapController( kill, modelcmd.WrapControllerSkipControllerFlags, modelcmd.WrapControllerSkipDefaultController, modelcmd.WrapControllerAPIOpener(openStrategy), ) }
// 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 := modelcmd.NewTimeoutOpener(fn, clock, 10*time.Second) return modelcmd.Wrap( kill, modelcmd.ModelSkipFlags, modelcmd.ModelSkipDefault, modelcmd.EnvAPIOpener(openStrategy), ) }
func (*APIOpenerSuite) TestTimoutErrors(c *gc.C) { var name string open := func(connectionName string) (api.Connection, error) { name = connectionName return nil, errors.New("boom") } opener := modelcmd.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") }
func (*APIOpenerSuite) TestTimoutSuccess(c *gc.C) { var name string open := func(connectionName string) (api.Connection, error) { name = connectionName return &mockConnection{}, nil } opener := modelcmd.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") }
func (*APIOpenerSuite) TestTimoutErrors(c *gc.C) { var controllerName, modelName string open := func(_ jujuclient.ClientStore, controllerNameArg, modelNameArg string) (api.Connection, error) { controllerName = controllerNameArg modelName = modelNameArg return nil, errors.New("boom") } opener := modelcmd.NewTimeoutOpener(modelcmd.OpenFunc(open), clock.WallClock, 10*time.Second) conn, err := opener.Open(nil, "a-name", "b-name") c.Assert(err, gc.ErrorMatches, "boom") c.Assert(conn, gc.IsNil) c.Assert(controllerName, gc.Equals, "a-name") c.Assert(modelName, gc.Equals, "b-name") }
func (*APIOpenerSuite) TestTimoutSuccess(c *gc.C) { var controllerName, accountName, modelName string open := func(_ jujuclient.ClientStore, controllerNameArg, accountNameArg, modelNameArg string) (api.Connection, error) { controllerName = controllerNameArg accountName = accountNameArg modelName = modelNameArg return &mockConnection{}, nil } opener := modelcmd.NewTimeoutOpener(modelcmd.OpenFunc(open), clock.WallClock, 10*time.Second) conn, err := opener.Open(nil, "a-name", "b-name", "c-name") c.Assert(err, jc.ErrorIsNil) c.Assert(conn, gc.NotNil) c.Assert(controllerName, gc.Equals, "a-name") c.Assert(accountName, gc.Equals, "b-name") c.Assert(modelName, gc.Equals, "c-name") }