Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1367)

Side by Side Diff: testing/mgo.go

Issue 6560045: testing: don't panic if mongod is missing (Closed)
Patch Set: testing: don't panic if mongod is missing Created 11 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 "net" 8 "net"
9 "os" 9 "os"
10 "os/exec" 10 "os/exec"
11 "strconv" 11 "strconv"
12 stdtesting "testing" 12 stdtesting "testing"
13 "time" 13 "time"
14 ) 14 )
15 15
16 // MgoAddr holds the address of the shared MongoDB server set up by 16 // MgoAddr holds the address of the shared MongoDB server set up by
17 // StartMgoServer. 17 // StartMgoServer.
18 var MgoAddr string 18 var MgoAddr string
19 19
20 // MgoSuite is a suite that deletes all content from the shared MongoDB 20 // MgoSuite is a suite that deletes all content from the shared MongoDB
21 // server at the end of every test and supplies a connection to the shared 21 // server at the end of every test and supplies a connection to the shared
22 // MongoDB server. 22 // MongoDB server.
23 type MgoSuite struct { 23 type MgoSuite struct {
24 Session *mgo.Session 24 Session *mgo.Session
25 } 25 }
26 26
27 // StartMgoServer starts a MongoDB server in a temporary directory. 27 // StartMgoServer starts a MongoDB server in a temporary directory.
28 // It panics if it encounters an error. 28 // It panics if it encounters an error.
29 func StartMgoServer() (server *exec.Cmd, dbdir string) { 29 func StartMgoServer(t *stdtesting.T) (server *exec.Cmd, dbdir string) {
niemeyer 2012/09/25 14:11:56 Just return an error instead: func StartMgoServer
dfc 2012/09/26 01:27:30 Done.
30 dbdir, err := ioutil.TempDir("", "test-mgo") 30 dbdir, err := ioutil.TempDir("", "test-mgo")
31 if err != nil { 31 if err != nil {
32 » » panic(fmt.Errorf("cannot create temporary directory: %v", err)) 32 » » t.Fatalf("cannot create temporary directory: %v", err)
33 } 33 }
34 mgoport := strconv.Itoa(FindTCPPort()) 34 mgoport := strconv.Itoa(FindTCPPort())
35 mgoargs := []string{ 35 mgoargs := []string{
36 "--dbpath", dbdir, 36 "--dbpath", dbdir,
37 "--bind_ip", "localhost", 37 "--bind_ip", "localhost",
38 "--port", mgoport, 38 "--port", mgoport,
39 "--nssize", "1", 39 "--nssize", "1",
40 "--noprealloc", 40 "--noprealloc",
41 "--smallfiles", 41 "--smallfiles",
42 "--nojournal", 42 "--nojournal",
43 } 43 }
44 server = exec.Command("mongod", mgoargs...) 44 server = exec.Command("mongod", mgoargs...)
45 err = server.Start() 45 err = server.Start()
46 if err != nil { 46 if err != nil {
47 os.RemoveAll(dbdir) 47 os.RemoveAll(dbdir)
48 » » panic(fmt.Errorf("cannot start MongoDB server: %v", err)) 48 » » t.Fatalf("cannot start MongoDB server: %v", err)
49 } 49 }
50 MgoAddr = "localhost:" + mgoport 50 MgoAddr = "localhost:" + mgoport
51 return server, dbdir 51 return server, dbdir
52 } 52 }
53 53
54 func MgoDestroy(server *exec.Cmd, dbdir string) { 54 func MgoDestroy(server *exec.Cmd, dbdir string) {
55 server.Process.Kill() 55 server.Process.Kill()
56 server.Process.Wait() 56 server.Process.Wait()
57 os.RemoveAll(dbdir) 57 os.RemoveAll(dbdir)
58 } 58 }
59 59
60 // MgoTestPackage should be called to register the tests for any package that 60 // MgoTestPackage should be called to register the tests for any package that
61 // requires a MongoDB server. 61 // requires a MongoDB server.
62 func MgoTestPackage(t *stdtesting.T) { 62 func MgoTestPackage(t *stdtesting.T) {
63 » server, dbdir := StartMgoServer() 63 » server, dbdir := StartMgoServer(t)
niemeyer 2012/09/25 14:11:56 if err != nil { t.Fatal(err) }
dfc 2012/09/26 01:27:30 Done.
64 defer MgoDestroy(server, dbdir) 64 defer MgoDestroy(server, dbdir)
65 TestingT(t) 65 TestingT(t)
66 } 66 }
67 67
68 func (s *MgoSuite) SetUpSuite(c *C) { 68 func (s *MgoSuite) SetUpSuite(c *C) {
69 if MgoAddr == "" { 69 if MgoAddr == "" {
70 panic("MgoSuite tests must be run with MgoTestPackage") 70 panic("MgoSuite tests must be run with MgoTestPackage")
71 } 71 }
72 mgo.SetStats(true) 72 mgo.SetStats(true)
73 } 73 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 // We hope that the probability is small enough during 130 // We hope that the probability is small enough during
131 // testing to be negligible. 131 // testing to be negligible.
132 func FindTCPPort() int { 132 func FindTCPPort() int {
133 l, err := net.Listen("tcp", "127.0.0.1:0") 133 l, err := net.Listen("tcp", "127.0.0.1:0")
134 if err != nil { 134 if err != nil {
135 panic(err) 135 panic(err)
136 } 136 }
137 l.Close() 137 l.Close()
138 return l.Addr().(*net.TCPAddr).Port 138 return l.Addr().(*net.TCPAddr).Port
139 } 139 }
OLDNEW
« no previous file with comments | « [revision details] ('k') | testing/mgo_test.go » ('j') | testing/mgo_test.go » ('J')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b