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