github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/provider/common/polling_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common_test 5 6 import ( 7 "errors" 8 "time" 9 10 gc "launchpad.net/gocheck" 11 12 "launchpad.net/juju-core/instance" 13 "launchpad.net/juju-core/provider/common" 14 "launchpad.net/juju-core/utils" 15 ) 16 17 type pollingSuite struct { 18 originalLongAttempt utils.AttemptStrategy 19 } 20 21 var _ = gc.Suite(&pollingSuite{}) 22 23 func (s *pollingSuite) SetUpSuite(c *gc.C) { 24 s.originalLongAttempt = common.LongAttempt 25 // The implementation of AttemptStrategy does not yield at all for a 26 // delay that's already expired. So while this setting must be short 27 // to avoid blocking tests, it must also allow enough time to convince 28 // AttemptStrategy to sleep. Otherwise a polling loop would just run 29 // uninterrupted and a concurrent goroutine that it was waiting for 30 // might never actually get to do its work. 31 common.LongAttempt = utils.AttemptStrategy{ 32 Total: 10 * time.Millisecond, 33 Delay: 1 * time.Millisecond, 34 } 35 } 36 37 func (s *pollingSuite) TearDownSuite(c *gc.C) { 38 common.LongAttempt = s.originalLongAttempt 39 } 40 41 // dnsNameFakeInstance is a fake environs.Instance implementation where 42 // DNSName returns whatever you tell it to, and WaitDNSName delegates to the 43 // shared WaitDNSName implementation. All the other methods are empty stubs. 44 type dnsNameFakeInstance struct { 45 // embed a nil Instance to panic on unimplemented method 46 instance.Instance 47 name string 48 err error 49 } 50 51 var _ instance.Instance = (*dnsNameFakeInstance)(nil) 52 53 func (inst *dnsNameFakeInstance) DNSName() (string, error) { 54 return inst.name, inst.err 55 } 56 57 func (inst *dnsNameFakeInstance) WaitDNSName() (string, error) { 58 return common.WaitDNSName(inst) 59 } 60 61 func (*dnsNameFakeInstance) Id() instance.Id { return "" } 62 63 func (pollingSuite) TestWaitDNSNameReturnsDNSNameIfAvailable(c *gc.C) { 64 inst := dnsNameFakeInstance{name: "anansi"} 65 name, err := common.WaitDNSName(&inst) 66 c.Assert(err, gc.IsNil) 67 c.Check(name, gc.Equals, "anansi") 68 } 69 70 func (pollingSuite) TestWaitDNSNamePollsOnErrNoDNSName(c *gc.C) { 71 inst := dnsNameFakeInstance{err: instance.ErrNoDNSName} 72 _, err := common.WaitDNSName(&inst) 73 c.Assert(err, gc.NotNil) 74 c.Check(err, gc.ErrorMatches, ".*timed out trying to get DNS address.*") 75 } 76 77 func (pollingSuite) TestWaitDNSNamePropagatesFailure(c *gc.C) { 78 failure := errors.New("deliberate failure") 79 inst := dnsNameFakeInstance{err: failure} 80 _, err := common.WaitDNSName(&inst) 81 c.Assert(err, gc.NotNil) 82 c.Check(err, gc.Equals, failure) 83 } 84 85 func (pollingSuite) TestInstanceWaitDNSDelegatesToSharedWaitDNS(c *gc.C) { 86 inst := dnsNameFakeInstance{name: "anansi"} 87 name, err := inst.WaitDNSName() 88 c.Assert(err, gc.IsNil) 89 c.Check(name, gc.Equals, "anansi") 90 }