~ubuntu-branches/ubuntu/saucy/juju-core/saucy-updates

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/environs/polling_test.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-09-03 14:22:22 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20130903142222-9mes2r8wqr0bs7lp
Tags: 1.13.3-0ubuntu1
New upstream point release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2013 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package environs_test
5
 
 
6
 
import (
7
 
        "errors"
8
 
        "time"
9
 
 
10
 
        gc "launchpad.net/gocheck"
11
 
 
12
 
        "launchpad.net/juju-core/environs"
13
 
        "launchpad.net/juju-core/instance"
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 = environs.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
 
        environs.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
 
        environs.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 environs.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 := environs.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 := environs.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 := environs.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
 
}