diff --git a/storage/mongo_test.go b/storage/mongo_test.go index 8785e31..4add417 100644 --- a/storage/mongo_test.go +++ b/storage/mongo_test.go @@ -5,30 +5,35 @@ import ( "crypto/rand" "encoding/base64" "testing" + "time" ) -func testMongoNew(t *testing.T) *Mongo { +func testMongoNew(t *testing.T) (*Mongo, func()) { b := make([]byte, 5) rand.Read(b) - m, err := NewMongo(10, base64.URLEncoding.EncodeToString(b), "mongodb://localhost:27017") + ns := "gotest_" + base64.URLEncoding.EncodeToString(b) + m, err := NewMongo(10, ns, "mongodb://localhost:27017") if err != nil { t.Fatal(err) } - return m -} - -func TestMongoNew(t *testing.T) { - if err := testMongoNew(t).Close(); err != nil { - t.Fatal(err) + return m, func() { + ctx, can := context.WithTimeout(context.Background(), time.Second*30) + defer can() + m.client.Database(ns).Drop(ctx) + m.Close() } } +func TestMongoNew(t *testing.T) { + _, can := testMongoNew(t) + can() +} + func TestMongoFind(t *testing.T) { - m := testMongoNew(t) - defer m.Close() + m, can := testMongoNew(t) + defer can() c := m.Account() - defer c.Drop(context.TODO()) if _, err := c.InsertMany(context.TODO(), []interface{}{ map[string]interface{}{"_id": "1", "a": "b"}, map[string]interface{}{"_id": "2", "a": "b", "c": "d"}, @@ -65,11 +70,10 @@ func TestMongoFind(t *testing.T) { } func TestMongoUpsert(t *testing.T) { - m := testMongoNew(t) - defer m.Close() + m, can := testMongoNew(t) + defer can() c := m.Account() - defer c.Drop(context.TODO()) if _, err := c.InsertMany(context.TODO(), []interface{}{ map[string]interface{}{"_id": "1", "a": "b"}, map[string]interface{}{"_id": "2", "a": "b", "c": "d"},