Skip to content

Commit

Permalink
test: config tests and mongodb connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasoares committed Jun 26, 2023
1 parent bed45a7 commit 3e885d5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/config/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var CacheType = Create(&ViperConfigKey{
var CacheUri = Create(&ViperConfigKey{
Key: "cache.uri",
Aliases: []string{"redis.uri"},
Default: nil,
})

var CacheConnectionRetryEnabled = Create(&ViperConfigKey{
Expand Down
2 changes: 1 addition & 1 deletion internal/config/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var StorageType = Create(&ViperConfigKey{
var StorageUri = Create(&ViperConfigKey{
Key: "storage.uri",
Aliases: []string{"mongo.uri", "mongodb.uri"},
Default: "",
Default: nil,
})

var StorageConnectionRetryEnabled = Create(&ViperConfigKey{
Expand Down
18 changes: 18 additions & 0 deletions internal/config/viper_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (config *ViperConfigKey) Get() string {
}
}

if val, ok := config.GetDefault().(string); ok {
return val
}

Check warning on line 49 in internal/config/viper_config.go

View check run for this annotation

Codecov / codecov/patch

internal/config/viper_config.go#L48-L49

Added lines #L48 - L49 were not covered by tests

return ""
}

Expand All @@ -59,6 +63,12 @@ func (config *ViperConfigKey) GetDuration() time.Duration {
}
}

if val, ok := config.GetDefault().(string); ok {
duration, _ := time.ParseDuration(val)

return duration
}

Check warning on line 70 in internal/config/viper_config.go

View check run for this annotation

Codecov / codecov/patch

internal/config/viper_config.go#L67-L70

Added lines #L67 - L70 were not covered by tests

return 0
}

Expand All @@ -74,6 +84,10 @@ func (config *ViperConfigKey) GetBool() bool {
}
}

if val, ok := config.GetDefault().(bool); ok {
return val
}

Check warning on line 89 in internal/config/viper_config.go

View check run for this annotation

Codecov / codecov/patch

internal/config/viper_config.go#L88-L89

Added lines #L88 - L89 were not covered by tests

return false
}

Expand All @@ -89,5 +103,9 @@ func (config *ViperConfigKey) GetInt() int {
}
}

if val, ok := config.GetDefault().(int); ok {
return val
}

Check warning on line 108 in internal/config/viper_config.go

View check run for this annotation

Codecov / codecov/patch

internal/config/viper_config.go#L107-L108

Added lines #L107 - L108 were not covered by tests

return 0
}
6 changes: 6 additions & 0 deletions internal/messagepool/storage/mongo_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ func waitForClient(ctx context.Context, opts *options.ClientOptions) (*mongo.Cli
var client *mongo.Client

for i := 1; i <= config.StorageConnectionRetryAttempts.GetInt(); i++ {
var cancelFunc context.CancelFunc = func() {}
if opts.ConnectTimeout != nil {
ctx, cancelFunc = context.WithTimeout(ctx, *opts.ConnectTimeout)
}
defer cancelFunc()

client, err = createClient(ctx, opts)

if err == nil || !config.StorageConnectionRetryEnabled.GetBool() {
Expand Down
3 changes: 1 addition & 2 deletions internal/messagepool/storage/mongo_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func TestNewStorageWithoutServerShouldErrorIntegration(t *testing.T) {

defer viper.Reset()
config.StorageConnectionRetryEnabled.Set(false)
config.MongoAddresses.Set("localhost:41343")
config.MongoDatabase.Set("unit_test")
config.StorageUri.Set("mongodb://localhost:41343/unit_test?connectTimeoutMS=200&socketTimeoutMS=200")

_, err := NewMongoStorage(context.Background())

Expand Down

0 comments on commit 3e885d5

Please sign in to comment.