Skip to content

Instantly share code, notes, and snippets.

@ncomet
Last active May 9, 2022 11:57
Show Gist options
  • Save ncomet/a5cf63ba305f77ec353e58d68b8f3855 to your computer and use it in GitHub Desktop.
Save ncomet/a5cf63ba305f77ec353e58d68b8f3855 to your computer and use it in GitHub Desktop.
TestMain final form
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