github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/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/clock" 11 "github.com/juju/retry" 12 "github.com/juju/utils/v3" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/environs" 16 "github.com/juju/juju/provider/common" 17 ) 18 19 func TestPackage(t *stdtesting.T) { 20 gc.TestingT(t) 21 } 22 23 type testingSuite struct{} 24 25 var _ = gc.Suite(&testingSuite{}) 26 27 func (*testingSuite) TestSaveAttemptStrategiesSaves(c *gc.C) { 28 // TODO(katco): 2016-08-09: lp:1611427 29 attempt := utils.AttemptStrategy{ 30 Total: time.Second, 31 Delay: time.Millisecond, 32 } 33 34 snapshot := saveAttemptStrategies([]*utils.AttemptStrategy{&attempt}) 35 36 c.Assert(snapshot, gc.HasLen, 1) 37 c.Check(snapshot[0].address, gc.Equals, &attempt) 38 c.Check(snapshot[0].original, gc.DeepEquals, attempt) 39 } 40 41 func (*testingSuite) TestSaveAttemptStrategiesLeavesOriginalsIntact(c *gc.C) { 42 // TODO(katco): 2016-08-09: lp:1611427 43 original := utils.AttemptStrategy{ 44 Total: time.Second, 45 Delay: time.Millisecond, 46 } 47 attempt := original 48 49 saveAttemptStrategies([]*utils.AttemptStrategy{&attempt}) 50 51 c.Check(attempt, gc.DeepEquals, original) 52 } 53 54 func (*testingSuite) TestInternalPatchAttemptStrategiesPatches(c *gc.C) { 55 // TODO(katco): 2016-08-09: lp:1611427 56 attempt := utils.AttemptStrategy{ 57 Total: 33 * time.Millisecond, 58 Delay: 99 * time.Microsecond, 59 } 60 c.Assert(attempt, gc.Not(gc.DeepEquals), impatientAttempt) 61 62 internalPatchAttemptStrategies([]*utils.AttemptStrategy{&attempt}) 63 64 c.Check(attempt, gc.DeepEquals, impatientAttempt) 65 } 66 67 // internalPatchAttemptStrategies returns a cleanup function that restores 68 // the given strategies to their original configurations. For simplicity, 69 // these tests take this as sufficient proof that any strategy that gets 70 // patched, also gets restored by the cleanup function. 71 func (*testingSuite) TestInternalPatchAttemptStrategiesReturnsCleanup(c *gc.C) { 72 // TODO(katco): 2016-08-09: lp:1611427 73 original := utils.AttemptStrategy{ 74 Total: 22 * time.Millisecond, 75 Delay: 77 * time.Microsecond, 76 } 77 c.Assert(original, gc.Not(gc.DeepEquals), impatientAttempt) 78 attempt := original 79 80 cleanup := internalPatchAttemptStrategies([]*utils.AttemptStrategy{&attempt}) 81 cleanup() 82 83 c.Check(attempt, gc.DeepEquals, original) 84 } 85 86 func (*testingSuite) TestPatchAttemptStrategiesPatchesEnvironsStrategies(c *gc.C) { 87 c.Assert(common.LongAttempt, gc.Not(gc.DeepEquals), impatientAttempt) 88 c.Assert(common.ShortAttempt, gc.Not(gc.DeepEquals), impatientAttempt) 89 c.Assert(environs.AddressesRefreshAttempt, gc.Not(gc.DeepEquals), impatientAttempt) 90 91 cleanup := PatchAttemptStrategies() 92 defer cleanup() 93 94 c.Check(common.LongAttempt, gc.DeepEquals, impatientAttempt) 95 c.Check(common.ShortAttempt, gc.DeepEquals, impatientAttempt) 96 c.Check(environs.AddressesRefreshAttempt, gc.DeepEquals, impatientAttempt) 97 } 98 99 func (*testingSuite) TestPatchAttemptStrategiesPatchesGivenAttempts(c *gc.C) { 100 // TODO(katco): 2016-08-09: lp:1611427 101 attempt1 := utils.AttemptStrategy{ 102 Total: 33 * time.Millisecond, 103 Delay: 99 * time.Microsecond, 104 } 105 attempt2 := utils.AttemptStrategy{ 106 Total: 82 * time.Microsecond, 107 Delay: 62 * time.Nanosecond, 108 } 109 110 cleanup := PatchAttemptStrategies(&attempt1, &attempt2) 111 defer cleanup() 112 113 c.Check(attempt1, gc.DeepEquals, impatientAttempt) 114 c.Check(attempt2, gc.DeepEquals, impatientAttempt) 115 } 116 117 // TODO(jack-w-shaw): 2022-01-21: Implementing funcs for both 'AttemptStrategy' 118 // patching and 'RetryStrategy' patching whilst lp:1611427 is in progress 119 // 120 // Remove AttemptStrategy patching when they are no longer in use i.e. when 121 // lp issue is resolved 122 123 func (*testingSuite) TestSaveRetrytrategiesSaves(c *gc.C) { 124 retryStrategy := retry.CallArgs{ 125 Clock: clock.WallClock, 126 MaxDuration: time.Second, 127 Delay: time.Millisecond, 128 } 129 130 snapshot := saveRetryStrategies([]*retry.CallArgs{&retryStrategy}) 131 132 c.Assert(snapshot, gc.HasLen, 1) 133 c.Check(snapshot[0].address, gc.Equals, &retryStrategy) 134 c.Check(snapshot[0].original, gc.DeepEquals, retryStrategy) 135 } 136 137 func (*testingSuite) TestSaveRetryStrategiesLeavesOriginalsIntact(c *gc.C) { 138 original := retry.CallArgs{ 139 Clock: clock.WallClock, 140 MaxDuration: time.Second, 141 Delay: time.Millisecond, 142 } 143 retryStrategy := original 144 145 saveRetryStrategies([]*retry.CallArgs{&retryStrategy}) 146 147 c.Check(retryStrategy, gc.DeepEquals, original) 148 } 149 150 func (*testingSuite) TestInternalPatchRetryStrategiesPatches(c *gc.C) { 151 retryStrategy := retry.CallArgs{ 152 Clock: clock.WallClock, 153 MaxDuration: 33 * time.Millisecond, 154 Delay: 99 * time.Microsecond, 155 } 156 c.Assert(retryStrategy, gc.Not(gc.DeepEquals), impatientRetryStrategy) 157 158 internalPatchRetryStrategies([]*retry.CallArgs{&retryStrategy}) 159 160 c.Check(retryStrategy, gc.DeepEquals, impatientRetryStrategy) 161 } 162 163 // internalPatchAttemptStrategies returns a cleanup function that restores 164 // the given strategies to their original configurations. For simplicity, 165 // these tests take this as sufficient proof that any strategy that gets 166 // patched, also gets restored by the cleanup function. 167 func (*testingSuite) TestInternalPatchRetryStrategiesReturnsCleanup(c *gc.C) { 168 original := retry.CallArgs{ 169 Clock: clock.WallClock, 170 MaxDuration: 22 * time.Millisecond, 171 Delay: 77 * time.Microsecond, 172 } 173 c.Assert(original, gc.Not(gc.DeepEquals), impatientRetryStrategy) 174 retryStrategy := original 175 176 cleanup := internalPatchRetryStrategies([]*retry.CallArgs{&retryStrategy}) 177 cleanup() 178 179 c.Check(retryStrategy, gc.DeepEquals, original) 180 } 181 182 func (*testingSuite) TestPatchRetryStrategiesPatchesGivenRetries(c *gc.C) { 183 retryStrategy1 := retry.CallArgs{ 184 Clock: clock.WallClock, 185 MaxDuration: 33 * time.Millisecond, 186 Delay: 99 * time.Microsecond, 187 } 188 retryStrategy2 := retry.CallArgs{ 189 Clock: clock.WallClock, 190 MaxDuration: 82 * time.Microsecond, 191 Delay: 62 * time.Nanosecond, 192 } 193 194 cleanup := PatchRetryStrategies(&retryStrategy1, &retryStrategy2) 195 defer cleanup() 196 197 c.Check(retryStrategy1, gc.DeepEquals, impatientRetryStrategy) 198 c.Check(retryStrategy2, gc.DeepEquals, impatientRetryStrategy) 199 }