77package integration
88
99import (
10+ "bytes"
1011 "context"
1112 "os"
1213 "sync"
@@ -20,6 +21,8 @@ import (
2021 "go.mongodb.org/mongo-driver/mongo"
2122 "go.mongodb.org/mongo-driver/mongo/integration/mtest"
2223 "go.mongodb.org/mongo-driver/mongo/options"
24+ "go.mongodb.org/mongo-driver/mongo/readconcern"
25+ "go.mongodb.org/mongo-driver/mongo/writeconcern"
2326)
2427
2528func TestSearchIndexProse (t * testing.T ) {
@@ -61,15 +64,16 @@ func TestSearchIndexProse(t *testing.T) {
6164 if ! cursor .Next (ctx ) {
6265 break
6366 }
64- if cursor .Current .Lookup ("queryable" ).Boolean () {
67+ name := cursor .Current .Lookup ("name" ).StringValue ()
68+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
69+ if name == searchName && queryable {
6570 doc = cursor .Current
6671 } else {
6772 t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
6873 time .Sleep (5 * time .Second )
6974 }
7075 }
7176 require .NotNil (mt , doc , "got empty document" )
72- assert .Equal (mt , searchName , doc .Lookup ("name" ).StringValue (), "unmatched name" )
7377 expected , err := bson .Marshal (definition )
7478 require .NoError (mt , err , "failed to marshal definition" )
7579 actual := doc .Lookup ("latestDefinition" ).Value
@@ -110,7 +114,9 @@ func TestSearchIndexProse(t *testing.T) {
110114 if ! cursor .Next (ctx ) {
111115 return nil
112116 }
113- if cursor .Current .Lookup ("queryable" ).Boolean () {
117+ name := cursor .Current .Lookup ("name" ).StringValue ()
118+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
119+ if name == * opts .Name && queryable {
114120 return cursor .Current
115121 }
116122 t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
@@ -126,7 +132,6 @@ func TestSearchIndexProse(t *testing.T) {
126132
127133 doc := getDocument (opts )
128134 require .NotNil (mt , doc , "got empty document" )
129- assert .Equal (mt , * opts .Name , doc .Lookup ("name" ).StringValue (), "unmatched name" )
130135 expected , err := bson .Marshal (definition )
131136 require .NoError (mt , err , "failed to marshal definition" )
132137 actual := doc .Lookup ("latestDefinition" ).Value
@@ -162,15 +167,16 @@ func TestSearchIndexProse(t *testing.T) {
162167 if ! cursor .Next (ctx ) {
163168 break
164169 }
165- if cursor .Current .Lookup ("queryable" ).Boolean () {
170+ name := cursor .Current .Lookup ("name" ).StringValue ()
171+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
172+ if name == searchName && queryable {
166173 doc = cursor .Current
167174 } else {
168175 t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
169176 time .Sleep (5 * time .Second )
170177 }
171178 }
172179 require .NotNil (mt , doc , "got empty document" )
173- require .Equal (mt , searchName , doc .Lookup ("name" ).StringValue (), "unmatched name" )
174180
175181 err = view .DropOne (ctx , searchName )
176182 require .NoError (mt , err , "failed to drop index" )
@@ -204,37 +210,49 @@ func TestSearchIndexProse(t *testing.T) {
204210 require .NoError (mt , err , "failed to create index" )
205211 require .Equal (mt , searchName , index , "unmatched name" )
206212
207- getDocument := func () bson.Raw {
208- for {
209- cursor , err := view .List (ctx , opts )
210- require .NoError (mt , err , "failed to list" )
213+ var doc bson.Raw
214+ for doc == nil {
215+ cursor , err := view .List (ctx , opts )
216+ require .NoError (mt , err , "failed to list" )
211217
212- if ! cursor .Next (ctx ) {
213- return nil
214- }
215- if cursor .Current .Lookup ("queryable" ).Boolean () {
216- return cursor .Current
217- }
218+ if ! cursor .Next (ctx ) {
219+ break
220+ }
221+ name := cursor .Current .Lookup ("name" ).StringValue ()
222+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
223+ if name == searchName && queryable {
224+ doc = cursor .Current
225+ } else {
218226 t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
219227 time .Sleep (5 * time .Second )
220228 }
221229 }
222-
223- doc := getDocument ()
224230 require .NotNil (mt , doc , "got empty document" )
225- require .Equal (mt , searchName , doc .Lookup ("name" ).StringValue (), "unmatched name" )
226231
227232 definition = bson.D {{"mappings" , bson.D {{"dynamic" , true }}}}
228- err = view .UpdateOne (ctx , searchName , definition )
229- require .NoError (mt , err , "failed to drop index" )
230- doc = getDocument ()
231- require .NotNil (mt , doc , "got empty document" )
232- assert .Equal (mt , searchName , doc .Lookup ("name" ).StringValue (), "unmatched name" )
233- assert .Equal (mt , "READY" , doc .Lookup ("status" ).StringValue (), "unexpected status" )
234233 expected , err := bson .Marshal (definition )
235234 require .NoError (mt , err , "failed to marshal definition" )
236- actual := doc .Lookup ("latestDefinition" ).Value
237- assert .Equal (mt , expected , actual , "unmatched definition" )
235+ err = view .UpdateOne (ctx , searchName , definition )
236+ require .NoError (mt , err , "failed to update index" )
237+ for doc == nil {
238+ cursor , err := view .List (ctx , opts )
239+ require .NoError (mt , err , "failed to list" )
240+
241+ if ! cursor .Next (ctx ) {
242+ break
243+ }
244+ name := cursor .Current .Lookup ("name" ).StringValue ()
245+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
246+ status := cursor .Current .Lookup ("status" ).StringValue ()
247+ latestDefinition := doc .Lookup ("latestDefinition" ).Value
248+ if name == searchName && queryable && status == "READY" && bytes .Equal (latestDefinition , expected ) {
249+ doc = cursor .Current
250+ } else {
251+ t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
252+ time .Sleep (5 * time .Second )
253+ }
254+ }
255+ require .NotNil (mt , doc , "got empty document" )
238256 })
239257
240258 mt .Run ("case 5: dropSearchIndex suppresses namespace not found errors" , func (mt * mtest.T ) {
@@ -250,4 +268,47 @@ func TestSearchIndexProse(t *testing.T) {
250268 err = collection .SearchIndexes ().DropOne (ctx , "foo" )
251269 require .NoError (mt , err )
252270 })
271+
272+ mt .RunOpts ("case 6: Driver can successfully create and list search indexes with non-default readConcern and writeConcern" ,
273+ mtest .NewOptions ().CollectionOptions (options .Collection ().SetWriteConcern (writeconcern .New (writeconcern .W (1 ))).SetReadConcern (readconcern .Majority ())),
274+ func (mt * mtest.T ) {
275+ ctx := context .Background ()
276+
277+ _ , err := mt .Coll .InsertOne (ctx , bson.D {})
278+ require .NoError (mt , err , "failed to insert" )
279+
280+ view := mt .Coll .SearchIndexes ()
281+
282+ definition := bson.D {{"mappings" , bson.D {{"dynamic" , false }}}}
283+ const searchName = "test-search-index-case6"
284+ opts := options .SearchIndexes ().SetName (searchName )
285+ index , err := view .CreateOne (ctx , mongo.SearchIndexModel {
286+ Definition : definition ,
287+ Options : opts ,
288+ })
289+ require .NoError (mt , err , "failed to create index" )
290+ require .Equal (mt , searchName , index , "unmatched name" )
291+ var doc bson.Raw
292+ for doc == nil {
293+ cursor , err := view .List (ctx , opts )
294+ require .NoError (mt , err , "failed to list" )
295+
296+ if ! cursor .Next (ctx ) {
297+ break
298+ }
299+ name := cursor .Current .Lookup ("name" ).StringValue ()
300+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
301+ if name == searchName && queryable {
302+ doc = cursor .Current
303+ } else {
304+ t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
305+ time .Sleep (5 * time .Second )
306+ }
307+ }
308+ require .NotNil (mt , doc , "got empty document" )
309+ expected , err := bson .Marshal (definition )
310+ require .NoError (mt , err , "failed to marshal definition" )
311+ actual := doc .Lookup ("latestDefinition" ).Value
312+ assert .Equal (mt , expected , actual , "unmatched definition" )
313+ })
253314}
0 commit comments