From 7bf50549c768c768df3950157afb1cdb9ccf7bb6 Mon Sep 17 00:00:00 2001 From: Qingyang Hu Date: Fri, 11 Aug 2023 08:49:13 -0400 Subject: [PATCH] update prose test --- mongo/search_index_prose_test.go | 180 +++++++++++++++++++++---------- 1 file changed, 121 insertions(+), 59 deletions(-) diff --git a/mongo/search_index_prose_test.go b/mongo/search_index_prose_test.go index 1b8731f599..0b1b3288cb 100644 --- a/mongo/search_index_prose_test.go +++ b/mongo/search_index_prose_test.go @@ -16,6 +16,7 @@ import ( "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/internal/assert" + "go.mongodb.org/mongo-driver/internal/require" "go.mongodb.org/mongo-driver/internal/uuid" "go.mongodb.org/mongo-driver/mongo/options" ) @@ -29,24 +30,6 @@ func newTestCollection(t *testing.T, client *Client) *Collection { return client.Database("test").Collection(collName) } -func getDocument(t *testing.T, ctx context.Context, view SearchIndexView, index string) bson.Raw { - t.Helper() - - for { - cursor, err := view.List(ctx, &index, nil) - assert.NoError(t, err, "failed to list") - - if !cursor.Next(ctx) { - return nil - } - if cursor.Current.Lookup("queryable").Boolean() { - return cursor.Current - } - t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String()) - time.Sleep(5 * time.Second) - } -} - func TestSearchIndexProse(t *testing.T) { const timeout = 5 * time.Minute @@ -66,10 +49,13 @@ func TestSearchIndexProse(t *testing.T) { ctx := context.Background() collection := newTestCollection(t, client) - defer collection.Drop(ctx) + defer func() { + err = collection.Drop(ctx) + assert.NoError(t, err, "failed to drop collection") + }() _, err = collection.InsertOne(ctx, bson.D{}) - assert.NoError(t, err, "failed to insert") + require.NoError(t, err, "failed to insert") view := collection.SearchIndexes() @@ -80,14 +66,28 @@ func TestSearchIndexProse(t *testing.T) { Name: &searchName, } index, err := view.CreateOne(ctx, model) - assert.NoError(t, err, "failed to create index") - assert.Equal(t, searchName, index, "unmatched name") + require.NoError(t, err, "failed to create index") + require.Equal(t, searchName, index, "unmatched name") - doc := getDocument(t, ctx, view, searchName) - assert.NotNil(t, doc, "got empty document") + var doc bson.Raw + for doc == nil { + cursor, err := view.List(ctx, &index, nil) + require.NoError(t, err, "failed to list") + + if !cursor.Next(ctx) { + break + } + if cursor.Current.Lookup("queryable").Boolean() { + doc = cursor.Current + } else { + t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String()) + time.Sleep(5 * time.Second) + } + } + require.NotNil(t, doc, "got empty document") assert.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name") expected, err := bson.Marshal(definition) - assert.NoError(t, err, "failed to marshal definition") + require.NoError(t, err, "failed to marshal definition") actual := doc.Lookup("latestDefinition").Value assert.Equal(t, expected, actual, "unmatched definition") }) @@ -96,10 +96,13 @@ func TestSearchIndexProse(t *testing.T) { ctx := context.Background() collection := newTestCollection(t, client) - defer collection.Drop(ctx) + defer func() { + err = collection.Drop(ctx) + assert.NoError(t, err, "failed to drop collection") + }() _, err = collection.InsertOne(ctx, bson.D{}) - assert.NoError(t, err, "failed to insert") + require.NoError(t, err, "failed to insert") view := collection.SearchIndexes() @@ -117,32 +120,50 @@ func TestSearchIndexProse(t *testing.T) { }, } indexes, err := view.CreateMany(ctx, models) - assert.NoError(t, err, "failed to create index") - assert.Equal(t, len(indexes), 2, "expected 2 indexes") - assert.Contains(t, indexes, searchName0) - assert.Contains(t, indexes, searchName1) + require.NoError(t, err, "failed to create index") + require.Equal(t, len(indexes), 2, "expected 2 indexes") + require.Contains(t, indexes, searchName0) + require.Contains(t, indexes, searchName1) + + getDocument := func(index string) bson.Raw { + t.Helper() + + for { + cursor, err := view.List(ctx, &index, nil) + require.NoError(t, err, "failed to list") + + if !cursor.Next(ctx) { + return nil + } + if cursor.Current.Lookup("queryable").Boolean() { + return cursor.Current + } + t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String()) + time.Sleep(5 * time.Second) + } + } var wg sync.WaitGroup wg.Add(2) go func() { defer wg.Done() - doc := getDocument(t, ctx, view, searchName0) - assert.NotNil(t, doc, "got empty document") + doc := getDocument(searchName0) + require.NotNil(t, doc, "got empty document") assert.Equal(t, searchName0, doc.Lookup("name").StringValue(), "unmatched name") expected, err := bson.Marshal(definition) - assert.NoError(t, err, "failed to marshal definition") + require.NoError(t, err, "failed to marshal definition") actual := doc.Lookup("latestDefinition").Value assert.Equal(t, expected, actual, "unmatched definition") }() go func() { defer wg.Done() - doc := getDocument(t, ctx, view, searchName1) - assert.NotNil(t, doc, "got empty document") + doc := getDocument(searchName1) + require.NotNil(t, doc, "got empty document") assert.Equal(t, searchName1, doc.Lookup("name").StringValue(), "unmatched name") expected, err := bson.Marshal(definition) - assert.NoError(t, err, "failed to marshal definition") + require.NoError(t, err, "failed to marshal definition") actual := doc.Lookup("latestDefinition").Value assert.Equal(t, expected, actual, "unmatched definition") }() @@ -153,10 +174,13 @@ func TestSearchIndexProse(t *testing.T) { ctx := context.Background() collection := newTestCollection(t, client) - defer collection.Drop(ctx) + defer func() { + err = collection.Drop(ctx) + assert.NoError(t, err, "failed to drop collection") + }() _, err = collection.InsertOne(ctx, bson.D{}) - assert.NoError(t, err, "failed to insert") + require.NoError(t, err, "failed to insert") view := collection.SearchIndexes() @@ -167,18 +191,32 @@ func TestSearchIndexProse(t *testing.T) { Name: &searchName, } index, err := view.CreateOne(ctx, model) - assert.NoError(t, err, "failed to create index") - assert.Equal(t, searchName, index, "unmatched name") + require.NoError(t, err, "failed to create index") + require.Equal(t, searchName, index, "unmatched name") - doc := getDocument(t, ctx, view, searchName) - assert.NotNil(t, doc, "got empty document") - assert.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name") + var doc bson.Raw + for doc == nil { + cursor, err := view.List(ctx, &index, nil) + require.NoError(t, err, "failed to list") + + if !cursor.Next(ctx) { + break + } + if cursor.Current.Lookup("queryable").Boolean() { + doc = cursor.Current + } else { + t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String()) + time.Sleep(5 * time.Second) + } + } + require.NotNil(t, doc, "got empty document") + require.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name") err = view.DropOne(ctx, searchName) - assert.NoError(t, err, "failed to drop index") + require.NoError(t, err, "failed to drop index") for { cursor, err := view.List(ctx, &index, nil) - assert.NoError(t, err, "failed to list") + require.NoError(t, err, "failed to list") if !cursor.Next(ctx) { break @@ -192,10 +230,13 @@ func TestSearchIndexProse(t *testing.T) { ctx := context.Background() collection := newTestCollection(t, client) - defer collection.Drop(ctx) + defer func() { + err = collection.Drop(ctx) + assert.NoError(t, err, "failed to drop collection") + }() _, err = collection.InsertOne(ctx, bson.D{}) - assert.NoError(t, err, "failed to insert") + require.NoError(t, err, "failed to insert") view := collection.SearchIndexes() @@ -206,22 +247,40 @@ func TestSearchIndexProse(t *testing.T) { Name: &searchName, } index, err := view.CreateOne(ctx, model) - assert.NoError(t, err, "failed to create index") - assert.Equal(t, searchName, index, "unmatched name") + require.NoError(t, err, "failed to create index") + require.Equal(t, searchName, index, "unmatched name") + + getDocument := func(index string) bson.Raw { + t.Helper() + + for { + cursor, err := view.List(ctx, &index, nil) + require.NoError(t, err, "failed to list") + + if !cursor.Next(ctx) { + return nil + } + if cursor.Current.Lookup("queryable").Boolean() { + return cursor.Current + } + t.Logf("cursor: %s, sleep 5 seconds...", cursor.Current.String()) + time.Sleep(5 * time.Second) + } + } - doc := getDocument(t, ctx, view, searchName) - assert.NotNil(t, doc, "got empty document") - assert.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name") + doc := getDocument(searchName) + require.NotNil(t, doc, "got empty document") + require.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name") definition = bson.D{{"mappings", bson.D{{"dynamic", true}}}} err = view.UpdateOne(ctx, searchName, definition) - assert.NoError(t, err, "failed to drop index") - doc = getDocument(t, ctx, view, searchName) - assert.NotNil(t, doc, "got empty document") + require.NoError(t, err, "failed to drop index") + doc = getDocument(searchName) + require.NotNil(t, doc, "got empty document") assert.Equal(t, searchName, doc.Lookup("name").StringValue(), "unmatched name") assert.Equal(t, "READY", doc.Lookup("status").StringValue(), "unexpected status") expected, err := bson.Marshal(definition) - assert.NoError(t, err, "failed to marshal definition") + require.NoError(t, err, "failed to marshal definition") actual := doc.Lookup("latestDefinition").Value assert.Equal(t, expected, actual, "unmatched definition") }) @@ -230,9 +289,12 @@ func TestSearchIndexProse(t *testing.T) { ctx := context.Background() collection := newTestCollection(t, client) - defer collection.Drop(ctx) + defer func() { + err = collection.Drop(ctx) + assert.NoError(t, err, "failed to drop collection") + }() err = collection.SearchIndexes().DropOne(ctx, "foo") - assert.NoError(t, err) + require.NoError(t, err) }) }