1
// WARNING: This package was replaced by mgo.v2/dbtest.
17
// WARNING: This package was replaced by mgo.v2/dbtest.
18
type TestServer struct {
27
// WARNING: This package was replaced by mgo.v2/dbtest.
28
func (ts *TestServer) SetPath(dbpath string) {
32
func (ts *TestServer) start() {
34
panic("TestServer already started")
37
panic("TestServer.SetPath must be called before using the server")
40
l, err := net.Listen("tcp", "127.0.0.1:0")
42
panic("unable to listen on a local address: " + err.Error())
44
addr := l.Addr().(*net.TCPAddr)
46
ts.host = addr.String()
49
"--dbpath", ts.dbpath,
50
"--bind_ip", "127.0.0.1",
51
"--port", strconv.Itoa(addr.Port),
58
ts.server = exec.Command("mongod", args...)
59
ts.server.Stdout = &ts.output
60
ts.server.Stderr = &ts.output
61
err = ts.server.Start()
65
ts.tomb.Go(ts.monitor)
69
func (ts *TestServer) monitor() error {
70
ts.server.Process.Wait()
72
// Present some debugging information.
73
fmt.Fprintf(os.Stderr, "---- mongod process died unexpectedly:\n")
74
fmt.Fprintf(os.Stderr, "%s", ts.output.Bytes())
75
fmt.Fprintf(os.Stderr, "---- mongod processes running right now:\n")
76
cmd := exec.Command("/bin/sh", "-c", "ps auxw | grep mongod")
77
cmd.Stdout = os.Stderr
78
cmd.Stderr = os.Stderr
80
fmt.Fprintf(os.Stderr, "----------------------------------------\n")
82
panic("mongod process died unexpectedly")
87
// WARNING: This package was replaced by mgo.v2/dbtest.
88
func (ts *TestServer) Stop() {
89
if ts.session != nil {
91
if ts.session != nil {
98
ts.server.Process.Kill()
100
case <-ts.tomb.Dead():
101
case <-time.After(5 * time.Second):
102
panic("timeout waiting for mongod process to die")
108
// WARNING: This package was replaced by mgo.v2/dbtest.
109
func (ts *TestServer) Session() *mgo.Session {
110
if ts.server == nil {
113
if ts.session == nil {
116
ts.session, err = mgo.Dial(ts.host + "/test")
121
return ts.session.Copy()
124
// WARNING: This package was replaced by mgo.v2/dbtest.
125
func (ts *TestServer) checkSessions() {
126
if check := os.Getenv("CHECK_SESSIONS"); check == "0" || ts.server == nil || ts.session == nil {
131
for i := 0; i < 100; i++ {
132
stats := mgo.GetStats()
133
if stats.SocketsInUse == 0 && stats.SocketsAlive == 0 {
136
time.Sleep(100 * time.Millisecond)
138
panic("There are mgo sessions still alive.")
141
// WARNING: This package was replaced by mgo.v2/dbtest.
142
func (ts *TestServer) Wipe() {
143
if ts.server == nil || ts.session == nil {
147
sessionUnset := ts.session == nil
148
session := ts.Session()
149
defer session.Close()
154
names, err := session.DatabaseNames()
158
for _, name := range names {
160
case "admin", "local", "config":
162
err = session.DB(name).DropDatabase()