Skip to content

Commit

Permalink
Merge pull request #53 from InVisionApp/dselans/test-mongo-update
Browse files Browse the repository at this point in the history
mongo updated to accept dialtimeout; test fix
  • Loading branch information
dselans authored Oct 12, 2018
2 parents a5df264 + f8dcc9c commit 6232e21
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
29 changes: 20 additions & 9 deletions checkers/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package checkers
import (
"fmt"
"github.com/globalsign/mgo"
"time"
)

const (
DefaultDialTimeout = 10 * time.Second
)

// MongoConfig is used for configuring the go-mongo check.
Expand All @@ -13,13 +18,16 @@ import (
//
// "Ping" is optional; Ping runs a trivial ping command just to get in touch with the server.
//
// "DialTimeout" is optional; default @ 10s; determines the max time we'll wait to reach a server.
//
// Note: At least _one_ check method must be set/enabled; you can also enable
// _all_ of the check methods (ie. perform a ping, or check particular collection for existense).
type MongoConfig struct {
Auth *MongoAuthConfig
Collection string
DB string
Ping bool
Auth *MongoAuthConfig
Collection string
DB string
Ping bool
DialTimeout time.Duration
}

// MongoAuthConfig, used to setup connection params for go-mongo check
Expand All @@ -41,7 +49,7 @@ func NewMongo(cfg *MongoConfig) (*Mongo, error) {
return nil, fmt.Errorf("unable to validate mongodb config: %v", err)
}

session, err := mgo.Dial(cfg.Auth.Url)
session, err := mgo.DialWithTimeout(cfg.Auth.Url, cfg.DialTimeout)
if err != nil {
return nil, err
}
Expand All @@ -51,7 +59,7 @@ func NewMongo(cfg *MongoConfig) (*Mongo, error) {
}

return &Mongo{
Config: cfg,
Config: cfg,
Session: session,
}, nil
}
Expand Down Expand Up @@ -98,14 +106,17 @@ func validateMongoConfig(cfg *MongoConfig) error {
return fmt.Errorf("Url string must be set in auth config")
}

if _, err := mgo.ParseURL(cfg.Auth.Url); err != nil {
return fmt.Errorf("Unable to parse URL: %v", err)
}

if !cfg.Ping && cfg.Collection == "" {
return fmt.Errorf("At minimum, either cfg.Ping or cfg.Collection")
}

if _, err := mgo.ParseURL(cfg.Auth.Url); err != nil {
return fmt.Errorf("Unable to parse URL: %v", err)
if cfg.DialTimeout <= 0 {
cfg.DialTimeout = DefaultDialTimeout
}

return nil
}

4 changes: 3 additions & 1 deletion checkers/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
. "github.com/onsi/gomega"
"github.com/zaffka/mongodb-boltdb-mock/db"
"testing"
"time"
)

func TestNewMongo(t *testing.T) {
Expand Down Expand Up @@ -33,6 +34,7 @@ func TestNewMongo(t *testing.T) {
Auth: &MongoAuthConfig{
Url: "foobar:42848",
},
DialTimeout: 20 * time.Millisecond,
}

r, err := NewMongo(cfg)
Expand Down Expand Up @@ -83,7 +85,7 @@ func TestValidateMongoConfig(t *testing.T) {
t.Run("Should error if url has wrong format", func(t *testing.T) {
cfg := &MongoConfig{
Auth: &MongoAuthConfig{
Url: "wrong\\localhost:6379",
Url: "localhost:40001?foo=1&bar=2",
},
}

Expand Down

0 comments on commit 6232e21

Please sign in to comment.