give mcfg.Cfg a TestRun method which can be used during tests

This commit is contained in:
Brian Picciano 2018-02-15 22:46:32 +00:00
parent 93cfa66c4b
commit 0feba17791

View File

@ -130,9 +130,14 @@ func New() *Cfg {
} }
func (c *Cfg) populateParams(src Source) error { func (c *Cfg) populateParams(src Source) error {
pvs, err := src.Parse(c) // we allow for nil Source here for tests
if err != nil { // TODO make Source stub type which tests could use here instead
return err var pvs []ParamValue
if src != nil {
var err error
if pvs, err = src.Parse(c); err != nil {
return err
}
} }
// first dedupe the params. We use this param struct as the key by which to // first dedupe the params. We use this param struct as the key by which to
@ -190,6 +195,16 @@ func (c *Cfg) populateParams(src Source) error {
return nil return nil
} }
func (c *Cfg) runPreBlock(ctx context.Context, src Source) error {
if err := c.populateParams(src); err != nil {
return err
}
startCtx, cancel := context.WithTimeout(ctx, c.StartTimeout)
defer cancel()
return c.startHooks(startCtx)
}
// Run blocks while performing all steps of a Cfg run. The steps, in order, are; // Run blocks while performing all steps of a Cfg run. The steps, in order, are;
// * Populate all configuration parameters // * Populate all configuration parameters
// * Recursively perform Start hooks, depth first // * Recursively perform Start hooks, depth first
@ -202,14 +217,7 @@ func (c *Cfg) populateParams(src Source) error {
// proper cleanup. If you care about that sort of thing you'll need to handle it // proper cleanup. If you care about that sort of thing you'll need to handle it
// yourself. // yourself.
func (c *Cfg) Run(ctx context.Context, src Source) error { func (c *Cfg) Run(ctx context.Context, src Source) error {
if err := c.populateParams(src); err != nil { if err := c.runPreBlock(ctx, src); err != nil {
return err
}
startCtx, cancel := context.WithTimeout(ctx, c.StartTimeout)
err := c.startHooks(startCtx)
cancel()
if err != nil {
return err return err
} }
@ -220,6 +228,16 @@ func (c *Cfg) Run(ctx context.Context, src Source) error {
return c.stopHooks(stopCtx) return c.stopHooks(stopCtx)
} }
// TestRun is like Run, except it's intended to only be used during tests to
// initialize other entities which are going to actually be tested. It assumes
// all default configuration param values, and will return after the Start hook
// has completed. It will panic on any errors.
func (c *Cfg) TestRun() {
if err := c.runPreBlock(context.Background(), nil); err != nil {
panic(err)
}
}
func (c *Cfg) startHooks(ctx context.Context) error { func (c *Cfg) startHooks(ctx context.Context) error {
return c.recurseHooks(ctx, func(c *Cfg) Hook { return c.Start }) return c.recurseHooks(ctx, func(c *Cfg) Hook { return c.Start })
} }