@@ -31,7 +31,7 @@ func TestSearchIndexProse(t *testing.T) {
3131
3232 const timeout = 5 * time .Minute
3333
34- uri := os .Getenv ("TEST_INDEX_URI " )
34+ uri := os .Getenv ("SEARCH_INDEX_URI " )
3535 if uri == "" {
3636 t .Skip ("skipping" )
3737 }
@@ -57,20 +57,23 @@ func TestSearchIndexProse(t *testing.T) {
5757 require .NoError (mt , err , "failed to create index" )
5858 require .Equal (mt , searchName , index , "unmatched name" )
5959
60+ awaitCtx , cancel := context .WithTimeout (context .Background (), 1 * time .Minute )
61+ defer cancel ()
62+
6063 var doc bson.Raw
6164 for doc == nil {
62- cursor , err := view .List (ctx , opts )
65+ cursor , err := view .List (awaitCtx , opts )
6366 require .NoError (mt , err , "failed to list" )
6467
65- if ! cursor .Next (ctx ) {
68+ if ! cursor .Next (awaitCtx ) {
6669 break
6770 }
6871 name := cursor .Current .Lookup ("name" ).StringValue ()
6972 queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
7073 if name == searchName && queryable {
7174 doc = cursor .Current
7275 } else {
73- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
76+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
7477 time .Sleep (5 * time .Second )
7578 }
7679 }
@@ -110,7 +113,7 @@ func TestSearchIndexProse(t *testing.T) {
110113 require .Contains (mt , indexes , * args .Name )
111114 }
112115
113- getDocument := func (opts * options.SearchIndexesOptionsBuilder ) bson.Raw {
116+ getDocument := func (ctx context. Context , opts * options.SearchIndexesOptionsBuilder ) bson.Raw {
114117 for {
115118 cursor , err := view .List (ctx , opts )
116119 require .NoError (mt , err , "failed to list" )
@@ -127,7 +130,7 @@ func TestSearchIndexProse(t *testing.T) {
127130 if name == * args .Name && queryable {
128131 return cursor .Current
129132 }
130- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
133+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
131134 time .Sleep (5 * time .Second )
132135 }
133136 }
@@ -138,7 +141,10 @@ func TestSearchIndexProse(t *testing.T) {
138141 go func (opts * options.SearchIndexesOptionsBuilder ) {
139142 defer wg .Done ()
140143
141- doc := getDocument (opts )
144+ ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Minute )
145+ defer cancel ()
146+
147+ doc := getDocument (ctx , opts )
142148 require .NotNil (mt , doc , "got empty document" )
143149
144150 args , err := mongoutil.NewOptions [options.SearchIndexesOptions ](opts )
@@ -173,35 +179,39 @@ func TestSearchIndexProse(t *testing.T) {
173179 require .NoError (mt , err , "failed to create index" )
174180 require .Equal (mt , searchName , index , "unmatched name" )
175181
182+ createOneCtx , createOneCancel := context .WithTimeout (context .Background (), 1 * time .Minute )
183+ defer createOneCancel ()
176184 var doc bson.Raw
177185 for doc == nil {
178- cursor , err := view .List (ctx , opts )
186+ cursor , err := view .List (createOneCtx , opts )
179187 require .NoError (mt , err , "failed to list" )
180188
181- if ! cursor .Next (ctx ) {
189+ if ! cursor .Next (createOneCtx ) {
182190 break
183191 }
184192 name := cursor .Current .Lookup ("name" ).StringValue ()
185193 queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
186194 if name == searchName && queryable {
187195 doc = cursor .Current
188196 } else {
189- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
197+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
190198 time .Sleep (5 * time .Second )
191199 }
192200 }
193201 require .NotNil (mt , doc , "got empty document" )
194202
195203 err = view .DropOne (ctx , searchName )
196204 require .NoError (mt , err , "failed to drop index" )
205+ dropOneCtx , dropOneCancel := context .WithTimeout (context .Background (), 1 * time .Minute )
206+ defer dropOneCancel ()
197207 for {
198- cursor , err := view .List (ctx , opts )
208+ cursor , err := view .List (dropOneCtx , opts )
199209 require .NoError (mt , err , "failed to list" )
200210
201- if ! cursor .Next (ctx ) {
211+ if ! cursor .Next (dropOneCtx ) {
202212 break
203213 }
204- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
214+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
205215 time .Sleep (5 * time .Second )
206216 }
207217 })
@@ -224,20 +234,22 @@ func TestSearchIndexProse(t *testing.T) {
224234 require .NoError (mt , err , "failed to create index" )
225235 require .Equal (mt , searchName , index , "unmatched name" )
226236
237+ createOneCtx , createOneCancel := context .WithTimeout (context .Background (), 1 * time .Minute )
238+ defer createOneCancel ()
227239 var doc bson.Raw
228240 for doc == nil {
229- cursor , err := view .List (ctx , opts )
241+ cursor , err := view .List (createOneCtx , opts )
230242 require .NoError (mt , err , "failed to list" )
231243
232- if ! cursor .Next (ctx ) {
244+ if ! cursor .Next (createOneCtx ) {
233245 break
234246 }
235247 name := cursor .Current .Lookup ("name" ).StringValue ()
236248 queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
237249 if name == searchName && queryable {
238250 doc = cursor .Current
239251 } else {
240- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
252+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
241253 time .Sleep (5 * time .Second )
242254 }
243255 }
@@ -248,11 +260,13 @@ func TestSearchIndexProse(t *testing.T) {
248260 require .NoError (mt , err , "failed to marshal definition" )
249261 err = view .UpdateOne (ctx , searchName , definition )
250262 require .NoError (mt , err , "failed to update index" )
263+ updateOneCtx , updateOneCancel := context .WithTimeout (context .Background (), 1 * time .Minute )
264+ defer updateOneCancel ()
251265 for doc == nil {
252- cursor , err := view .List (ctx , opts )
266+ cursor , err := view .List (updateOneCtx , opts )
253267 require .NoError (mt , err , "failed to list" )
254268
255- if ! cursor .Next (ctx ) {
269+ if ! cursor .Next (updateOneCtx ) {
256270 break
257271 }
258272 name := cursor .Current .Lookup ("name" ).StringValue ()
@@ -262,7 +276,7 @@ func TestSearchIndexProse(t *testing.T) {
262276 if name == searchName && queryable && status == "READY" && bytes .Equal (latestDefinition , expected ) {
263277 doc = cursor .Current
264278 } else {
265- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
279+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
266280 time .Sleep (5 * time .Second )
267281 }
268282 }
@@ -302,20 +316,22 @@ func TestSearchIndexProse(t *testing.T) {
302316 })
303317 require .NoError (mt , err , "failed to create index" )
304318 require .Equal (mt , searchName , index , "unmatched name" )
319+ awaitCtx , cancel := context .WithTimeout (context .Background (), 1 * time .Minute )
320+ defer cancel ()
305321 var doc bson.Raw
306322 for doc == nil {
307- cursor , err := view .List (ctx , opts )
323+ cursor , err := view .List (awaitCtx , opts )
308324 require .NoError (mt , err , "failed to list" )
309325
310- if ! cursor .Next (ctx ) {
326+ if ! cursor .Next (awaitCtx ) {
311327 break
312328 }
313329 name := cursor .Current .Lookup ("name" ).StringValue ()
314330 queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
315331 if name == searchName && queryable {
316332 doc = cursor .Current
317333 } else {
318- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
334+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
319335 time .Sleep (5 * time .Second )
320336 }
321337 }
@@ -348,12 +364,14 @@ func TestSearchIndexProse(t *testing.T) {
348364 })
349365 require .NoError (mt , err , "failed to create index" )
350366 require .Equal (mt , indexName , index , "unmatched name" )
367+ implicitCtx , implicitCancel := context .WithTimeout (context .Background (), 1 * time .Minute )
368+ defer implicitCancel ()
351369 var doc bson.Raw
352370 for doc == nil {
353- cursor , err := view .List (ctx , opts )
371+ cursor , err := view .List (implicitCtx , opts )
354372 require .NoError (mt , err , "failed to list" )
355373
356- if ! cursor .Next (ctx ) {
374+ if ! cursor .Next (implicitCtx ) {
357375 break
358376 }
359377 name := cursor .Current .Lookup ("name" ).StringValue ()
@@ -363,7 +381,7 @@ func TestSearchIndexProse(t *testing.T) {
363381 doc = cursor .Current
364382 assert .Equal (mt , indexType , "search" )
365383 } else {
366- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
384+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
367385 time .Sleep (5 * time .Second )
368386 }
369387 }
@@ -376,12 +394,14 @@ func TestSearchIndexProse(t *testing.T) {
376394 })
377395 require .NoError (mt , err , "failed to create index" )
378396 require .Equal (mt , indexName , index , "unmatched name" )
397+ explicitCtx , explicitCancel := context .WithTimeout (context .Background (), 1 * time .Minute )
398+ defer explicitCancel ()
379399 doc = nil
380400 for doc == nil {
381- cursor , err := view .List (ctx , opts )
401+ cursor , err := view .List (explicitCtx , opts )
382402 require .NoError (mt , err , "failed to list" )
383403
384- if ! cursor .Next (ctx ) {
404+ if ! cursor .Next (explicitCtx ) {
385405 break
386406 }
387407 name := cursor .Current .Lookup ("name" ).StringValue ()
@@ -391,7 +411,7 @@ func TestSearchIndexProse(t *testing.T) {
391411 doc = cursor .Current
392412 assert .Equal (mt , indexType , "search" )
393413 } else {
394- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
414+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
395415 time .Sleep (5 * time .Second )
396416 }
397417 }
@@ -417,12 +437,14 @@ func TestSearchIndexProse(t *testing.T) {
417437 })
418438 require .NoError (mt , err , "failed to create index" )
419439 require .Equal (mt , indexName , index , "unmatched name" )
440+ vectorCtx , vectorCancel := context .WithTimeout (context .Background (), 1 * time .Minute )
441+ defer vectorCancel ()
420442 doc = nil
421443 for doc == nil {
422- cursor , err := view .List (ctx , opts )
444+ cursor , err := view .List (vectorCtx , opts )
423445 require .NoError (mt , err , "failed to list" )
424446
425- if ! cursor .Next (ctx ) {
447+ if ! cursor .Next (vectorCtx ) {
426448 break
427449 }
428450 name := cursor .Current .Lookup ("name" ).StringValue ()
@@ -432,7 +454,7 @@ func TestSearchIndexProse(t *testing.T) {
432454 doc = cursor .Current
433455 assert .Equal (mt , indexType , "vectorSearch" )
434456 } else {
435- t .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
457+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
436458 time .Sleep (5 * time .Second )
437459 }
438460 }
@@ -472,4 +494,55 @@ func TestSearchIndexProse(t *testing.T) {
472494 })
473495 assert .ErrorContains (mt , err , "Attribute mappings missing" )
474496 })
497+
498+ mt .Run ("case 9: Drivers use server default for unspecified name (`default`) and type (`search`)" , func (mt * mtest.T ) {
499+ cases := []struct {
500+ name string
501+ opts * options.SearchIndexesOptionsBuilder
502+ }{
503+ {name : "empty options" , opts : options .SearchIndexes ()},
504+ {name : "nil options" , opts : nil },
505+ }
506+
507+ for _ , tc := range cases {
508+ mt .Run (tc .name , func (mt * mtest.T ) {
509+ view := mt .Coll .SearchIndexes ()
510+ definition := bson.D {
511+ {"mappings" , bson.D {
512+ {"dynamic" , true },
513+ }},
514+ }
515+
516+ indexName , err := view .CreateOne (context .Background (), mongo.SearchIndexModel {
517+ Definition : definition ,
518+ Options : tc .opts ,
519+ })
520+ require .NoError (mt , err , "failed to create index" )
521+ require .Equal (mt , "default" , indexName )
522+
523+ ctx , cancel := context .WithTimeout (context .Background (), 1 * time .Minute )
524+ defer cancel ()
525+
526+ var doc bson.Raw
527+ for doc == nil {
528+ cursor , err := view .List (ctx , tc .opts )
529+ require .NoError (mt , err , "failed to list" )
530+
531+ if ! cursor .Next (ctx ) {
532+ break
533+ }
534+ name := cursor .Current .Lookup ("name" ).StringValue ()
535+ queryable := cursor .Current .Lookup ("queryable" ).Boolean ()
536+ indexType := cursor .Current .Lookup ("type" ).StringValue ()
537+ if name == indexName && queryable {
538+ doc = cursor .Current
539+ require .Equal (mt , indexType , "search" )
540+ } else {
541+ mt .Logf ("cursor: %s, sleep 5 seconds..." , cursor .Current .String ())
542+ time .Sleep (5 * time .Second )
543+ }
544+ }
545+ })
546+ }
547+ })
475548}
0 commit comments