Skip to content

Commit

Permalink
Unparallelise tests that lead to data races.
Browse files Browse the repository at this point in the history
  • Loading branch information
ro-tex committed Mar 16, 2022
1 parent f0eaaac commit 26a2332
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 51 deletions.
33 changes: 16 additions & 17 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,29 @@ func NewCustomDB(ctx context.Context, dbName string, creds DBCredentials, logger
if err != nil {
return nil, errors.AddContext(err, "failed to connect to DB")
}
database := c.Database(dbName)
db := c.Database(dbName)
if logger == nil {
logger = &logrus.Logger{}
}
err = ensureDBSchema(ctx, database, Schema, logger)
err = ensureDBSchema(ctx, db, Schema, logger)
if err != nil {
return nil, err
}
db := &DB{
staticDB: database,
staticUsers: database.Collection(collUsers),
staticSkylinks: database.Collection(collSkylinks),
staticUploads: database.Collection(collUploads),
staticDownloads: database.Collection(collDownloads),
staticRegistryReads: database.Collection(collRegistryReads),
staticRegistryWrites: database.Collection(collRegistryWrites),
staticEmails: database.Collection(collEmails),
staticChallenges: database.Collection(collChallenges),
staticUnconfirmedUserUpdates: database.Collection(collUnconfirmedUserUpdates),
staticConfiguration: database.Collection(collConfiguration),
staticAPIKeys: database.Collection(collAPIKeys),
return &DB{
staticDB: db,
staticUsers: db.Collection(collUsers),
staticSkylinks: db.Collection(collSkylinks),
staticUploads: db.Collection(collUploads),
staticDownloads: db.Collection(collDownloads),
staticRegistryReads: db.Collection(collRegistryReads),
staticRegistryWrites: db.Collection(collRegistryWrites),
staticEmails: db.Collection(collEmails),
staticChallenges: db.Collection(collChallenges),
staticUnconfirmedUserUpdates: db.Collection(collUnconfirmedUserUpdates),
staticConfiguration: db.Collection(collConfiguration),
staticAPIKeys: db.Collection(collAPIKeys),
staticLogger: logger,
}
return db, nil
}, nil
}

// Disconnect closes the connection to the database in an orderly fashion.
Expand Down
2 changes: 0 additions & 2 deletions test/database/apikeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

// TestAPIKeys ensures the DB operations with API keys work as expected.
func TestAPIKeys(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down
4 changes: 0 additions & 4 deletions test/database/challenge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (

// TestValidateChallengeResponse is a unit test using a database.
func TestValidateChallengeResponse(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -128,8 +126,6 @@ func TestValidateChallengeResponse(t *testing.T) {
// TestUnconfirmedUserUpdate ensures the entire flow for unconfirmed user
// updates works as expected.
func TestUnconfirmedUserUpdate(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down
2 changes: 0 additions & 2 deletions test/database/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
// TestConfiguration ensures we can correctly read and write from/to the
// configuration DB table.
func TestConfiguration(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down
2 changes: 0 additions & 2 deletions test/database/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
// TestUploadsByUser ensures UploadsByUser returns the correct uploads,
// in the correct order, with the correct sized and so on.
func TestUploadsByUser(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down
24 changes: 0 additions & 24 deletions test/database/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
// TestUserByEmail ensures UserByEmail works as expected.
// This method also tests UserCreate.
func TestUserByEmail(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -64,8 +62,6 @@ func TestUserByEmail(t *testing.T) {

// TestUserByID ensures UserByID works as expected.
func TestUserByID(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -105,8 +101,6 @@ func TestUserByID(t *testing.T) {
// TestUserByPubKey makes sure UserByPubKey functions correctly, both with a
// single and multiple pubkeys attached to a user.
func TestUserByPubKey(t *testing.T) {
t.Parallel()

ctx := context.Background()
name := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, name, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -158,8 +152,6 @@ func TestUserByPubKey(t *testing.T) {
// TestUserByStripeID ensures UserByStripeID works as expected.
// This method also tests UserCreate and UserSetStripeID.
func TestUserByStripeID(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -204,8 +196,6 @@ func TestUserByStripeID(t *testing.T) {
// TestUserBySub ensures UserBySub works as expected.
// This method also tests UserCreate.
func TestUserBySub(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -248,8 +238,6 @@ func TestUserBySub(t *testing.T) {
// TestUserConfirmEmail ensures that email confirmation works as expected,
// including resecting the expiration of tokens.
func TestUserConfirmEmail(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -288,8 +276,6 @@ func TestUserConfirmEmail(t *testing.T) {

// TestUserCreate ensures UserCreate works as expected.
func TestUserCreate(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -339,8 +325,6 @@ func TestUserCreate(t *testing.T) {

// TestUserDelete ensures UserDelete works as expected.
func TestUserDelete(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -379,8 +363,6 @@ func TestUserDelete(t *testing.T) {

// TestUserSave ensures that UserSave works as expected.
func TestUserSave(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -427,8 +409,6 @@ func TestUserSave(t *testing.T) {

// TestUserSetStripeID ensures that UserSetStripeID works as expected.
func TestUserSetStripeID(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -460,8 +440,6 @@ func TestUserSetStripeID(t *testing.T) {

// TestUserSetTier ensures that UserSetTier works as expected.
func TestUserSetTier(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down Expand Up @@ -491,8 +469,6 @@ func TestUserSetTier(t *testing.T) {

// TestUserStats ensures we report accurate statistics for users.
func TestUserStats(t *testing.T) {
t.Parallel()

ctx := context.Background()
dbName := test.DBNameForTest(t.Name())
db, err := database.NewCustomDB(ctx, dbName, test.DBTestCredentials(), nil)
Expand Down

0 comments on commit 26a2332

Please sign in to comment.