-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from mailgun/thrawn/develop
Added WrappedContext and mongoutil.Config
- Loading branch information
Showing
6 changed files
with
326 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package cancel_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mailgun/holster/v4/cancel" | ||
) | ||
|
||
func TestWrapFirst(t *testing.T) { | ||
// First context | ||
firstCtx := cancel.New(context.Background()) | ||
// Second context | ||
secondCtx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// Now if either firstCtx or secondCtx is cancelled 'ctx' should cancel | ||
ctx := firstCtx.Wrap(secondCtx) | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
<-ctx.Done() | ||
close(done) | ||
}() | ||
|
||
firstCtx.Cancel() | ||
|
||
select { | ||
case <-done: | ||
case <-time.After(time.Second): | ||
t.Fatalf("timeout waiting for context to cancel") | ||
} | ||
} | ||
|
||
func TestWrapSecond(t *testing.T) { | ||
// First context | ||
firstCtx := cancel.New(context.Background()) | ||
// Second context | ||
secondCtx, cancel := context.WithCancel(context.Background()) | ||
|
||
// Now if either firstCtx or secondCtx is cancelled 'ctx' should cancel | ||
ctx := firstCtx.Wrap(secondCtx) | ||
|
||
done := make(chan struct{}) | ||
go func() { | ||
<-ctx.Done() | ||
close(done) | ||
}() | ||
|
||
cancel() | ||
|
||
select { | ||
case <-done: | ||
case <-time.After(time.Second): | ||
t.Fatalf("timeout waiting for context to cancel") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package mongoutil | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
type Config struct { | ||
Servers []string `json:"servers"` | ||
Database string `json:"database"` | ||
URI string `json:"uri"` | ||
Options []map[string]interface{} `json:"options"` | ||
} | ||
|
||
func MongoURI() string { | ||
mongoURI := os.Getenv("MONGO_URI") | ||
if mongoURI == "" { | ||
return "mongodb://127.0.0.1:27017/mg_test" | ||
} | ||
return mongoURI | ||
} | ||
|
||
func (c Config) URIWithOptions() string { | ||
URI := c.URI | ||
|
||
// Create an URI using the Servers list and Database if provided | ||
if len(c.Servers) != 0 && c.Database != "" { | ||
URI = fmt.Sprintf("mongodb://%s/%s", strings.Join(c.Servers, ","), c.Database) | ||
} | ||
|
||
type opt struct { | ||
key string | ||
value string | ||
} | ||
adjustedURI := URI | ||
var options []opt | ||
|
||
// Parse options from the URI. | ||
qmIdx := strings.Index(URI, "?") | ||
if qmIdx > 0 { | ||
adjustedURI = URI[:qmIdx] | ||
for _, pair := range strings.Split(URI[qmIdx+1:], "&") { | ||
eqIdx := strings.Index(pair, "=") | ||
if eqIdx > 0 { | ||
options = append(options, opt{key: pair[:eqIdx], value: pair[eqIdx+1:]}) | ||
} | ||
} | ||
} | ||
|
||
// NOTE: The options are an ordered list because mongo cares | ||
// about the order of some options like replica tag order. | ||
|
||
// Override URI options with config options. | ||
for _, o := range c.Options { | ||
for optName, optVal := range o { | ||
switch optVal := optVal.(type) { | ||
case int: | ||
options = append(options, opt{key: toCamelCase(optName), value: strconv.Itoa(optVal)}) | ||
case float64: | ||
options = append(options, opt{key: toCamelCase(optName), value: strconv.Itoa(int(optVal))}) | ||
case string: | ||
options = append(options, opt{key: toCamelCase(optName), value: optVal}) | ||
} | ||
} | ||
} | ||
|
||
// Construct a URI as recognized by mgo.Dial | ||
firstOpt := true | ||
var buf bytes.Buffer | ||
buf.WriteString(adjustedURI) | ||
|
||
for i := range options { | ||
o := options[i] | ||
if firstOpt { | ||
buf.WriteRune('?') | ||
firstOpt = false | ||
} else { | ||
buf.WriteRune('&') | ||
} | ||
buf.WriteString(o.key) | ||
buf.WriteRune('=') | ||
buf.WriteString(o.value) | ||
} | ||
return buf.String() | ||
} | ||
|
||
func toCamelCase(s string) string { | ||
var buf bytes.Buffer | ||
capitalize := false | ||
for _, ch := range s { | ||
if ch == '_' { | ||
capitalize = true | ||
continue | ||
} | ||
if capitalize { | ||
capitalize = false | ||
buf.WriteRune(unicode.ToUpper(ch)) | ||
continue | ||
} | ||
buf.WriteRune(ch) | ||
} | ||
return buf.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package mongoutil_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/mailgun/holster/v4/mongoutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
func TestMongoConfig_URIWithOptions(t *testing.T) { | ||
for _, tt := range []struct { | ||
cfg mongoutil.Config | ||
name string | ||
uri string | ||
}{{ | ||
name: "Bare minimum config", | ||
cfg: mongoutil.Config{ | ||
URI: "mongodb://127.0.0.1:27017/foo", | ||
}, | ||
uri: "mongodb://127.0.0.1:27017/foo", | ||
}, { | ||
name: "URI parameters appended after URI encoded parameters", | ||
cfg: mongoutil.Config{ | ||
URI: "mongodb://127.0.0.1:27017/foo?replicaSet=bazz&blah=wow", | ||
Options: []map[string]interface{}{ | ||
{"replica_set": "bar"}, | ||
{"max_pool_size": 5}, | ||
}, | ||
}, | ||
uri: "mongodb://127.0.0.1:27017/foo?replicaSet=bazz&blah=wow&replicaSet=bar&maxPoolSize=5", | ||
}, { | ||
name: "Read preference provided", | ||
cfg: mongoutil.Config{ | ||
URI: "mongodb://127.0.0.1:27017/foo", | ||
Options: []map[string]interface{}{ | ||
{"read_preference": "secondaryPreferred"}, | ||
}, | ||
}, | ||
uri: "mongodb://127.0.0.1:27017/foo?readPreference=secondaryPreferred", | ||
}, { | ||
name: "Servers and Database provided", | ||
cfg: mongoutil.Config{ | ||
Servers: []string{ | ||
"mongodb-n01:27017", | ||
"mongodb-n02:28017", | ||
}, | ||
Database: "foo", | ||
Options: []map[string]interface{}{ | ||
{"read_preference": "secondaryPreferred"}, | ||
}, | ||
}, | ||
uri: "mongodb://mongodb-n01:27017,mongodb-n02:28017/foo?readPreference=secondaryPreferred", | ||
}} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
uri := tt.cfg.URIWithOptions() | ||
assert.Equal(t, tt.uri, uri) | ||
}) | ||
} | ||
} | ||
|
||
func TestMongoURIFromJSON(t *testing.T) { | ||
cfgJSON := []byte(`{ | ||
"uri": "mongodb://127.0.0.1:27017/foo", | ||
"options": [ | ||
{"compressors": "snappy,zlib"}, | ||
{"replica_set": "v34_queue"}, | ||
{"read_preference": "secondaryPreferred"}, | ||
{"max_pool_size": 5} | ||
] | ||
}`) | ||
var conf mongoutil.Config | ||
// When | ||
err := json.Unmarshal(cfgJSON, &conf) | ||
// Then | ||
require.NoError(t, err) | ||
require.Equal(t, | ||
"mongodb://127.0.0.1:27017/foo?compressors=snappy,zlib&replicaSet=v34_queue&"+ | ||
"readPreference=secondaryPreferred&maxPoolSize=5", conf.URIWithOptions()) | ||
} | ||
|
||
func TestMongoURIFromYAML(t *testing.T) { | ||
cfgYAML := []byte(`servers: | ||
- mongo-routes-n01-us-east-1.postgun.com:27017 | ||
- mongo-routes-n02-us-east-1.postgun.com:27017 | ||
- mongo-routes-n03-us-east-1.postgun.com:27017 | ||
database: mg_prod | ||
options: | ||
- ssl: true | ||
- tlsCertificateKeyFile: /etc/mailgun/ssl/mongo.pem | ||
- tlsCAFile: /etc/mailgun/ssl/mongo-ca.crt | ||
- replicaSet: routes | ||
- readPreferenceTags: "dc:use1" | ||
- readPreferenceTags: "dc:usw2" | ||
`) | ||
var conf mongoutil.Config | ||
// When | ||
err := yaml.Unmarshal(cfgYAML, &conf) | ||
// Then | ||
require.NoError(t, err) | ||
require.Equal(t, "mongodb://mongo-routes-n01-us-east-1.postgun.com:27017,"+ | ||
"mongo-routes-n02-us-east-1.postgun.com:27017,mongo-routes-n03-us-east-1.postgun.com:27017/mg_prod?"+ | ||
"tlsCertificateKeyFile=/etc/mailgun/ssl/mongo.pem&tlsCAFile=/etc/mailgun/ssl/mongo-ca.crt&"+ | ||
"replicaSet=routes&readPreferenceTags=dc:use1&readPreferenceTags=dc:usw2", conf.URIWithOptions()) | ||
} |