github.com/mwhudson/juju@v0.0.0-20160512215208-90ff01f3497f/environs/testing/polling_test.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  	stdtesting "testing"
     8  	"time"
     9  
    10  	"github.com/juju/utils"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/environs"
    14  	"github.com/juju/juju/provider/common"
    15  )
    16  
    17  func TestPackage(t *stdtesting.T) {
    18  	gc.TestingT(t)
    19  }
    20  
    21  type testingSuite struct{}
    22  
    23  var _ = gc.Suite(&testingSuite{})
    24  
    25  func (*testingSuite) TestSaveAttemptStrategiesSaves(c *gc.C) {
    26  	attempt := utils.AttemptStrategy{
    27  		Total: time.Second,
    28  		Delay: time.Millisecond,
    29  	}
    30  
    31  	snapshot := saveAttemptStrategies([]*utils.AttemptStrategy{&attempt})
    32  
    33  	c.Assert(snapshot, gc.HasLen, 1)
    34  	c.Check(snapshot[0].address, gc.Equals, &attempt)
    35  	c.Check(snapshot[0].original, gc.DeepEquals, attempt)
    36  }
    37  
    38  func (*testingSuite) TestSaveAttemptStrategiesLeavesOriginalsIntact(c *gc.C) {
    39  	original := utils.AttemptStrategy{
    40  		Total: time.Second,
    41  		Delay: time.Millisecond,
    42  	}
    43  	attempt := original
    44  
    45  	saveAttemptStrategies([]*utils.AttemptStrategy{&attempt})
    46  
    47  	c.Check(attempt, gc.DeepEquals, original)
    48  }
    49  
    50  func (*testingSuite) TestInternalPatchAttemptStrategiesPatches(c *gc.C) {
    51  	attempt := utils.AttemptStrategy{
    52  		Total: 33 * time.Millisecond,
    53  		Delay: 99 * time.Microsecond,
    54  	}
    55  	c.Assert(attempt, gc.Not(gc.DeepEquals), impatientAttempt)
    56  
    57  	internalPatchAttemptStrategies([]*utils.AttemptStrategy{&attempt})
    58  
    59  	c.Check(attempt, gc.DeepEquals, impatientAttempt)
    60  }
    61  
    62  // internalPatchAttemptStrategies returns a cleanup function that restores
    63  // the given strategies to their original configurations.  For simplicity,
    64  // these tests take this as sufficient proof that any strategy that gets
    65  // patched, also gets restored by the cleanup function.
    66  func (*testingSuite) TestInternalPatchAttemptStrategiesReturnsCleanup(c *gc.C) {
    67  	original := utils.AttemptStrategy{
    68  		Total: 22 * time.Millisecond,
    69  		Delay: 77 * time.Microsecond,
    70  	}
    71  	c.Assert(original, gc.Not(gc.DeepEquals), impatientAttempt)
    72  	attempt := original
    73  
    74  	cleanup := internalPatchAttemptStrategies([]*utils.AttemptStrategy{&attempt})
    75  	cleanup()
    76  
    77  	c.Check(attempt, gc.DeepEquals, original)
    78  }
    79  
    80  func (*testingSuite) TestPatchAttemptStrategiesPatchesEnvironsStrategies(c *gc.C) {
    81  	c.Assert(common.LongAttempt, gc.Not(gc.DeepEquals), impatientAttempt)
    82  	c.Assert(common.ShortAttempt, gc.Not(gc.DeepEquals), impatientAttempt)
    83  	c.Assert(environs.AddressesRefreshAttempt, gc.Not(gc.DeepEquals), impatientAttempt)
    84  
    85  	cleanup := PatchAttemptStrategies()
    86  	defer cleanup()
    87  
    88  	c.Check(common.LongAttempt, gc.DeepEquals, impatientAttempt)
    89  	c.Check(common.ShortAttempt, gc.DeepEquals, impatientAttempt)
    90  	c.Check(environs.AddressesRefreshAttempt, gc.DeepEquals, impatientAttempt)
    91  }
    92  
    93  func (*testingSuite) TestPatchAttemptStrategiesPatchesGivenAttempts(c *gc.C) {
    94  	attempt1 := utils.AttemptStrategy{
    95  		Total: 33 * time.Millisecond,
    96  		Delay: 99 * time.Microsecond,
    97  	}
    98  	attempt2 := utils.AttemptStrategy{
    99  		Total: 82 * time.Microsecond,
   100  		Delay: 62 * time.Nanosecond,
   101  	}
   102  
   103  	cleanup := PatchAttemptStrategies(&attempt1, &attempt2)
   104  	defer cleanup()
   105  
   106  	c.Check(attempt1, gc.DeepEquals, impatientAttempt)
   107  	c.Check(attempt2, gc.DeepEquals, impatientAttempt)
   108  }