github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/environs/testing/polling.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing
     5  
     6  import (
     7  	"github.com/juju/utils"
     8  
     9  	"github.com/juju/juju/environs"
    10  	"github.com/juju/juju/provider/common"
    11  )
    12  
    13  // impatientAttempt is an extremely short polling time suitable for tests.
    14  // It polls at least once, never delays, and times out very quickly.
    15  var impatientAttempt = utils.AttemptStrategy{}
    16  
    17  // savedAttemptStrategy holds the state needed to restore an AttemptStrategy's
    18  // original setting.
    19  type savedAttemptStrategy struct {
    20  	address  *utils.AttemptStrategy
    21  	original utils.AttemptStrategy
    22  }
    23  
    24  // saveAttemptStrategies captures the information required to restore the
    25  // given AttemptStrategy objects.
    26  func saveAttemptStrategies(strategies []*utils.AttemptStrategy) []savedAttemptStrategy {
    27  	savedStrategies := make([]savedAttemptStrategy, len(strategies))
    28  	for index, strategy := range strategies {
    29  		savedStrategies[index] = savedAttemptStrategy{
    30  			address:  strategy,
    31  			original: *strategy,
    32  		}
    33  	}
    34  	return savedStrategies
    35  }
    36  
    37  // restore returns a saved strategy to its original configuration.
    38  func (saved savedAttemptStrategy) restore() {
    39  	*saved.address = saved.original
    40  }
    41  
    42  // restoreAttemptStrategies restores multiple saved AttemptStrategies.
    43  func restoreAttemptStrategies(strategies []savedAttemptStrategy) {
    44  	for _, saved := range strategies {
    45  		saved.restore()
    46  	}
    47  }
    48  
    49  // internalPatchAttemptStrategies sets the given AttemptStrategy objects
    50  // to the impatientAttempt configuration, and returns a function that restores
    51  // the original configurations.
    52  func internalPatchAttemptStrategies(strategies []*utils.AttemptStrategy) func() {
    53  	snapshot := saveAttemptStrategies(strategies)
    54  	for _, strategy := range strategies {
    55  		*strategy = impatientAttempt
    56  	}
    57  	return func() { restoreAttemptStrategies(snapshot) }
    58  }
    59  
    60  // TODO: Everything up to this point is generic.  Move it to utils?
    61  
    62  // PatchAttemptStrategies patches environs' global polling strategy, plus any
    63  // otther AttemptStrategy objects whose addresses you pass, to very short
    64  // polling and timeout times so that tests can run fast.
    65  // It returns a cleanup function that restores the original settings.  You must
    66  // call this afterwards.
    67  func PatchAttemptStrategies(strategies ...*utils.AttemptStrategy) func() {
    68  	// The one irregularity here is that LongAttempt goes on the list of
    69  	// strategies that need patching.  To keep testing simple, we treat
    70  	// the given attempts and LongAttempt as a single slice from here on.
    71  	combinedStrategies := append(
    72  		strategies,
    73  		&common.LongAttempt,
    74  		&common.ShortAttempt,
    75  		&environs.AddressesRefreshAttempt,
    76  	)
    77  	return internalPatchAttemptStrategies(combinedStrategies)
    78  }