Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/master' into fix/shutdow…
Browse files Browse the repository at this point in the history
…n-deadlock
  • Loading branch information
yolandeleungFT committed Jun 9, 2017
2 parents 449fe18 + 966219d commit 1351784
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 5 deletions.
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/husobee/vestigo"
cli "gopkg.in/urfave/cli.v1"
"fmt"
)

func init() {
Expand Down Expand Up @@ -61,6 +62,12 @@ func main() {
EnvVar: "MONGO_DB_URL",
Usage: "The Mongo DB connection url string (comma delimited).",
},
cli.IntFlag{
Name: "mongo-node-count",
Value: 1,
EnvVar: "MONGO_NODE_COUNT",
Usage: "The number of Mongo DB instances.",
},
cli.StringFlag{
Name: "cms-notifier-url",
Value: "http://localhost:8080/__cms-notifier",
Expand Down Expand Up @@ -144,6 +151,10 @@ func main() {
app.Action = func(ctx *cli.Context) {
log.Info("Starting the Publish Carousel.")

if err := native.CheckMongoURLs(ctx.String("mongo-db"), ctx.Int("mongo-node-count")); err != nil {
panic(fmt.Sprintf("Provided MongoDB URLs are invalid: %s", err))
}

s3rw := s3.NewReadWriter(ctx.String("aws-region"), ctx.String("s3-bucket"))
stateRw := scheduler.NewS3MetadataReadWriter(s3rw)

Expand Down
28 changes: 28 additions & 0 deletions native/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (

log "github.com/Sirupsen/logrus"
mgo "gopkg.in/mgo.v2"
"errors"
"strings"
"fmt"
"net"
)

var expectedConnections = 1
Expand Down Expand Up @@ -116,6 +120,30 @@ func (tx *MongoTX) ReadNativeContent(collectionID string, uuid string) (*Content
return result, nil
}

func CheckMongoURLs(providedMongoUrls string, expectedMongoNodeCount int) error {
if providedMongoUrls == "" {
return errors.New("MongoDB urls are missing")
}

mongoUrls := strings.Split(providedMongoUrls, ",")
actualMongoNodeCount := len(mongoUrls)
if actualMongoNodeCount < expectedMongoNodeCount {
return fmt.Errorf("The list of MongoDB URLs should have %d instances, but it has %d instead. Provided MongoDB URLs are: %s", expectedMongoNodeCount, actualMongoNodeCount, providedMongoUrls)
}

for _, mongoUrl := range mongoUrls {
host, port, err := net.SplitHostPort(mongoUrl);
if err != nil {
return fmt.Errorf("Cannot split MongoDB URL: %s into host and port. Error is: %s",mongoUrl, err.Error())
}
if host == "" || port == "" {
return fmt.Errorf("One of the MongoDB URLs is incomplete: %s. It should have host and port.", mongoUrl)
}
}

return nil
}

// Ping returns a mongo ping response
func (tx *MongoTX) Ping(ctx context.Context) error {
ping := make(chan error, 1)
Expand Down
40 changes: 35 additions & 5 deletions native/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ func TestFindUUIDsDateSort(t *testing.T) {
testUUID3 := uuid.NewUUID().String()
testUUIDs := []string{testUUID1, testUUID2, testUUID3}

insertTestContent(t, db, testUUID2, time.Now().Add(-10*time.Second))
insertTestContent(t, db, testUUID2, time.Now().Add(-10 * time.Second))
insertTestContent(t, db, testUUID1, time.Now())
insertTestContent(t, db, testUUID3, time.Now().Add(-20*time.Second))
insertTestContent(t, db, testUUID3, time.Now().Add(-20 * time.Second))

iter, count, err := tx.FindUUIDs("methode", 0, 10)
assert.NoError(t, err)
Expand Down Expand Up @@ -143,8 +143,8 @@ func TestFindByTimeWindow(t *testing.T) {
testUUID2 := uuid.NewUUID().String()
t.Log("Test uuids to use", testUUID, testUUID2)

insertTestContent(t, db, testUUID, time.Now().Add(time.Second*-1))
insertTestContent(t, db, testUUID2, time.Now().Add(time.Minute*-2))
insertTestContent(t, db, testUUID, time.Now().Add(time.Second * -1))
insertTestContent(t, db, testUUID2, time.Now().Add(time.Minute * -2))

end := time.Now()
start := end.Add(time.Minute * -1)
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestReadNativeContent(t *testing.T) {
assert.NotNil(t, content)

assert.Equal(t, testUUID, content.Body["uuid"])
assert.Equal(t, "tid_"+testUUID, content.Body["publishReference"])
assert.Equal(t, "tid_" + testUUID, content.Body["publishReference"])
cleanupTestContent(t, db, testUUID)
}

Expand Down Expand Up @@ -230,3 +230,33 @@ func TestDBCloses(t *testing.T) {
db.(*MongoDB).session.Ping()
})
}

func TestCheckMongoURLsValidUrls(t *testing.T) {
err := CheckMongoURLs("valid-url.com:1234,second-valid-url.com:1234,third-valid-url.com:1234", 3)
assert.NoError(t, err)
}

func TestCheckMongoURLsMissingUrls(t *testing.T) {
err := CheckMongoURLs("", 1)
assert.Error(t, err)
}

func TestCheckMongoURLsSmallerNumberOfUrls(t *testing.T) {
err := CheckMongoURLs("valid-url.com:1234", 2)
assert.Error(t, err)
}

func TestCheckMongoURLsGreaterNumberOfUrls(t *testing.T) {
err := CheckMongoURLs("valid-url.com:1234,second-valid-url.com:1234", 1)
assert.NoError(t, err)
}

func TestCheckMongoURLsMissingPort(t *testing.T) {
err := CheckMongoURLs("valid-url.com:", 1)
assert.Error(t, err)
}

func TestCheckMongoURLsMissingHost(t *testing.T) {
err := CheckMongoURLs(":1234", 1)
assert.Error(t, err)
}

0 comments on commit 1351784

Please sign in to comment.