Last active
May 9, 2022 11:57
-
-
Save ncomet/a5cf63ba305f77ec353e58d68b8f3855 to your computer and use it in GitHub Desktop.
TestMain final form
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
func TestMain(m *testing.M) { | |
var code = 1 | |
defer func() { os.Exit(code) }() | |
ctx := context.Background() | |
mongoContainer, err := setup(ctx, mongoDB) | |
if err != nil { | |
log.Printf("Unexpected error, fallback to mem repository implementations.\nerror: %s.\n", err) | |
allGames = mem.NewAllGames() | |
} else { | |
ctx, cancel := context.WithTimeout(ctx, 10*time.Second) | |
defer cancel() | |
client, err := mdriver.Connect(ctx, options.Client().ApplyURI(mongoContainer.URI)) | |
if err != nil { | |
log.Printf("Could not connect to mongodb, fallback to mem repository implementations.\nerror: %s.\n", err) | |
allGames = mem.NewAllGames() | |
} else { | |
allGames = mongo.NewAllGames(client) | |
} | |
defer func() { | |
if err = client.Disconnect(ctx); err != nil { | |
panic(err) | |
} | |
}() | |
} | |
code = m.Run() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment