Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: test on a non-primary database #529

Merged
merged 1 commit into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/cli/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestUpdateUsers(t *testing.T) {
t.Fatal(err)
}

database := redis.NewDatabase(logger, conf.Redis.GetSettings(), redis.Cli)
database := redis.NewTestDatabase(logger)
conf.Cleanup.Whitelist = []string{"Nikolay", ""}

users := []string{"Aleksey", "Arkadiy", "Emil"}
Expand Down
19 changes: 14 additions & 5 deletions database/redis/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ type DBSource string

// All types of database instances users
const (
API DBSource = "API"
Checker DBSource = "Checker"
Filter DBSource = "Filter"
Notifier DBSource = "Notifier"
Cli DBSource = "Cli"
API DBSource = "API"
Checker DBSource = "Checker"
Filter DBSource = "Filter"
Notifier DBSource = "Notifier"
Cli DBSource = "Cli"
testSource DBSource = "test"
)

// Test data for configuration
const testDB = 1

// DbConnector contains redis pool
type DbConnector struct {
pool *redis.Pool
Expand Down Expand Up @@ -108,6 +112,11 @@ func NewDatabase(logger moira.Logger, config Config, source DBSource) *DbConnect
return connector
}

// NewTestDatabase use it only for tests
func NewTestDatabase(logger moira.Logger) *DbConnector {
return NewDatabase(logger, Config{Port: "6379", Host: "0.0.0.0", DB: testDB}, testSource)
}

// AllowStale returns a database instance, that prioritizes connections to slave nodes
// Should only be used for read accesses when data actuality is not needed
func (connector *DbConnector) AllowStale() moira.Database {
Expand Down
4 changes: 2 additions & 2 deletions database/redis/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (

var config = Config{Port: "6379", Host: "0.0.0.0"}
var emptyConfig = Config{}
var testSource = DBSource("test")

// use it only for tests
// use it only for tests in redis package
func newTestDatabase(logger moira.Logger, config Config) *DbConnector {
config.DB = testDB
return NewDatabase(logger, config, testSource)
}

Expand Down
2 changes: 1 addition & 1 deletion integration_tests/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ var event = moira.NotificationEvent{
func TestNotifier(t *testing.T) {
mockCtrl = gomock.NewController(t)
defer mockCtrl.Finish()
database := redis.NewDatabase(logger, redis.Config{Port: "6379", Host: "localhost"}, redis.Notifier)
database := redis.NewTestDatabase(logger)
metricsSourceProvider := metricSource.CreateMetricSourceProvider(local.Create(database), nil)
database.SaveContact(&contact)
database.SaveSubscription(&subscription)
Expand Down