Merge lp:~axwalk/juju-core/testing-mgo-addrinuse into lp:~go-bot/juju-core/trunk
Proposed by
Andrew Wilkins
Status: | Merged |
---|---|
Approved by: | Andrew Wilkins |
Approved revision: | no longer in the source branch. |
Merged at revision: | 2729 |
Proposed branch: | lp:~axwalk/juju-core/testing-mgo-addrinuse |
Merge into: | lp:~go-bot/juju-core/trunk |
Diff against target: |
86 lines (+38/-13) 1 file modified
testing/mgo.go (+38/-13) |
To merge this branch: | bzr merge lp:~axwalk/juju-core/testing-mgo-addrinuse |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Juju Engineering | Pending | ||
Review via email: mp+219456@code.launchpad.net |
Commit message
testing: reattempt MgoInstance.run if addr in use
Mongod does not support listening on port 0 to
acquire an ephemeral port, so our tests must
choose a port which may then be allocated to
another process. This CL adds code to reattempt
the starting of mongod if the port is in use,
up to a maximum of 5 attempts.
Description of the change
testing: reattempt MgoInstance.run if addr in use
Mongod does not support listening on port 0 to
acquire an ephemeral port, so our tests must
choose a port which may then be allocated to
another process. This CL adds code to reattempt
the starting of mongod if the port is in use,
up to a maximum of 5 attempts.
To post a comment you must log in.
Reviewers: mp+219456_ code.launchpad. net,
Message:
Please take a look.
Description:
testing: reattempt MgoInstance.run if addr in use
Mongod does not support listening on port 0 to
acquire an ephemeral port, so our tests must
choose a port which may then be allocated to
another process. This CL adds code to reattempt
the starting of mongod if the port is in use,
up to a maximum of 5 attempts.
https:/ /code.launchpad .net/~axwalk/ juju-core/ testing- mgo-addrinuse/ +merge/ 219456
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/94410046/
Affected files (+39, -13 lines):
A [revision details]
M testing/mgo.go
Index: [revision details] 20140513233806- k5sipwwqoqnt8gz r
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-
+New revision: <email address hidden>
Index: testing/mgo.go nnectionsRe = regexp. MustCompile( ".*initandliste n.*waiting
=== modified file 'testing/mgo.go'
--- testing/mgo.go 2014-05-12 07:59:38 +0000
+++ testing/mgo.go 2014-05-14 05:26:43 +0000
@@ -38,6 +38,11 @@
waitingForCo
for connections.*")
)
+const ( ttempts = 5 "localhost: %d", inst.port) inst.dir) Warningf( "failed to start mongo: %v", err) Debugf( "started mongod pid %d in %s on port %d", Process. Pid, dbdir, inst.port) ttempts times, ttempts; i++ { "localhost: %d", inst.port) eError: Debugf( "failed to start mongo: %v, trying another port", err) Debugf( "started mongod pid %d in %s on port %d", Process. Pid, dbdir, inst.port) inst.dir) Warningf( "failed to start mongo: %v", err) ng(prefix, io.TeeReader(out, &buf), ctionsRe) { Contains( buf.String( ), "addr already in use") { eError{ err}
+ // maximum number of times to attempt starting mongod
+ maxStartMongodA
+)
+
type MgoInstance struct {
// addr holds the address of the MongoDB server
addr string
@@ -107,18 +112,29 @@
if err != nil {
return fmt.Errorf("cannot write cert/key PEM: %v", err)
}
- inst.port = FindTCPPort()
- inst.addr = fmt.Sprintf(
- inst.dir = dbdir
- inst.ssl = ssl
- if err := inst.run(); err != nil {
- inst.addr = ""
- inst.port = 0
- os.RemoveAll(
- inst.dir = ""
- logger.
- } else {
- logger.
inst.server.
+
+ // Attempt to start mongo up to maxStartMongodA
+ // as the port we choose may be taken from us in the mean time.
+ for i := 0; i < maxStartMongodA
+ inst.port = FindTCPPort()
+ inst.addr = fmt.Sprintf(
+ inst.dir = dbdir
+ inst.ssl = ssl
+ err = inst.run()
+ switch err.(type) {
+ case addrAlreadyInUs
+ logger.
+ continue
+ case nil:
+ logger.
inst.server.
+ default:
+ inst.addr = ""
+ inst.port = 0
+ os.RemoveAll(
+ inst.dir = ""
+ logger.
+ }
+ break
}
return err
}
@@ -186,7 +202,11 @@
if readUntilMatchi
waitingForConne
listening <- nil
} else {
- listening <- fmt.Errorf("mongod failed to listen on port %v", mgoport)
+ err := fmt.Errorf("mongod failed to listen on port %v", mgoport)
+ if strings.
+ err = addrAlreadyInUs
+ }
+ listening <- err
}
// Capture the last 20 lines of output from mongod, to log
/...