| OLD | NEW |
| 1 package testing | 1 package testing |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "fmt" | 4 "fmt" |
| 5 "io/ioutil" | 5 "io/ioutil" |
| 6 "labix.org/v2/mgo" | 6 "labix.org/v2/mgo" |
| 7 . "launchpad.net/gocheck" | 7 . "launchpad.net/gocheck" |
| 8 "launchpad.net/juju-core/log" | 8 "launchpad.net/juju-core/log" |
| 9 "launchpad.net/juju-core/trivial" |
| 9 "net" | 10 "net" |
| 10 "os" | 11 "os" |
| 11 "os/exec" | 12 "os/exec" |
| 12 "strconv" | 13 "strconv" |
| 13 stdtesting "testing" | 14 stdtesting "testing" |
| 14 "time" | 15 "time" |
| 15 ) | 16 ) |
| 16 | 17 |
| 17 var ( | 18 var ( |
| 18 // MgoAddr holds the address of the shared MongoDB server set up by | 19 // MgoAddr holds the address of the shared MongoDB server set up by |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 84 |
| 84 func (s *MgoSuite) SetUpSuite(c *C) { | 85 func (s *MgoSuite) SetUpSuite(c *C) { |
| 85 if MgoAddr == "" { | 86 if MgoAddr == "" { |
| 86 panic("MgoSuite tests must be run with MgoTestPackage") | 87 panic("MgoSuite tests must be run with MgoTestPackage") |
| 87 } | 88 } |
| 88 mgo.SetStats(true) | 89 mgo.SetStats(true) |
| 89 } | 90 } |
| 90 | 91 |
| 91 func (s *MgoSuite) TearDownSuite(c *C) {} | 92 func (s *MgoSuite) TearDownSuite(c *C) {} |
| 92 | 93 |
| 94 var mgoDialAttempt = trivial.AttemptStrategy{ |
| 95 Total: 500 * time.Millisecond, |
| 96 Delay: 100 * time.Millisecond, |
| 97 } |
| 98 |
| 93 // MgoDial returns a new connection to the shared MongoDB server. | 99 // MgoDial returns a new connection to the shared MongoDB server. |
| 94 func MgoDial() *mgo.Session { | 100 func MgoDial() *mgo.Session { |
| 95 » session, err := mgo.Dial(MgoAddr) | 101 » var err error |
| 96 » if err != nil { | 102 » for a := mgoDialAttempt.Start(); a.Next(); { |
| 97 » » panic(err) | 103 » » var session *mgo.Session |
| 104 » » session, err = mgo.Dial(MgoAddr) |
| 105 » » if err == nil { |
| 106 » » » return session |
| 107 » » } |
| 98 } | 108 } |
| 99 » return session | 109 » panic(err) |
| 100 } | 110 } |
| 101 | 111 |
| 102 func (s *MgoSuite) SetUpTest(c *C) { | 112 func (s *MgoSuite) SetUpTest(c *C) { |
| 103 mgo.ResetStats() | 113 mgo.ResetStats() |
| 104 s.Session = MgoDial() | 114 s.Session = MgoDial() |
| 105 } | 115 } |
| 106 | 116 |
| 107 // MgoReset deletes all content from the shared MongoDB server. | 117 // MgoReset deletes all content from the shared MongoDB server. |
| 108 func MgoReset() { | 118 func MgoReset() { |
| 109 session := MgoDial() | 119 session := MgoDial() |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // We hope that the probability is small enough during | 176 // We hope that the probability is small enough during |
| 167 // testing to be negligible. | 177 // testing to be negligible. |
| 168 func FindTCPPort() int { | 178 func FindTCPPort() int { |
| 169 l, err := net.Listen("tcp", "127.0.0.1:0") | 179 l, err := net.Listen("tcp", "127.0.0.1:0") |
| 170 if err != nil { | 180 if err != nil { |
| 171 panic(err) | 181 panic(err) |
| 172 } | 182 } |
| 173 l.Close() | 183 l.Close() |
| 174 return l.Addr().(*net.TCPAddr).Port | 184 return l.Addr().(*net.TCPAddr).Port |
| 175 } | 185 } |
| OLD | NEW |