TestDatabaseInitTeardownTiming tests scenarios where InitializeDatabase is called during the completion phase of a previous async initialization. Ensures there are no cases where a watcher is added but never receives a done notification.
(t *testing.T)
| 415 | // the completion phase of a previous async initialization. Ensures there are no cases where a |
| 416 | // watcher is added but never receives a done notification. |
| 417 | func TestDatabaseInitTeardownTiming(t *testing.T) { |
| 418 | |
| 419 | RequireN1QLIndexes(t) |
| 420 | base.TestRequiresCollections(t) |
| 421 | base.SetUpTestLogging(t, base.LevelInfo, base.KeyHTTP, base.KeyConfig) |
| 422 | |
| 423 | sc, closeFn := StartBootstrapServer(t) |
| 424 | defer closeFn() |
| 425 | ctx := base.TestCtx(t) |
| 426 | |
| 427 | // Get a test bucket for bootstrap testing, and drop indexes created by bucket pool readier |
| 428 | tb := base.GetTestBucket(t) |
| 429 | defer tb.Close(ctx) |
| 430 | |
| 431 | // Drop all test indexes so we can test InitializeDatabase |
| 432 | DropAllTestIndexes(t, tb) |
| 433 | |
| 434 | // Set up collection names and ScopesConfig for testing |
| 435 | scopesConfig := GetCollectionsConfig(t, tb, 3) |
| 436 | dataStoreNames := GetDataStoreNamesFromScopesConfig(scopesConfig) |
| 437 | scopeName := dataStoreNames[0].ScopeName() |
| 438 | collection1Name := dataStoreNames[0].CollectionName() |
| 439 | collection2Name := dataStoreNames[1].CollectionName() |
| 440 | collection1and2ScopesConfig := makeScopesConfig(scopeName, []string{collection1Name, collection2Name}) |
| 441 | |
| 442 | initMgr := sc.DatabaseInitManager |
| 443 | |
| 444 | // Create collection callback that blocks and waits for test notification the first time a collection is initialized, does not block afterward. |
| 445 | var collectionCount atomic.Int64 |
| 446 | initMgr.testCollectionStatusUpdateCallback = func(_ string, _ base.ScopeAndCollectionName, status db.CollectionIndexStatus) { |
| 447 | if status != db.CollectionIndexStatusReady { |
| 448 | return |
| 449 | } |
| 450 | collectionCount.Add(1) |
| 451 | } |
| 452 | dbName := "dbName" |
| 453 | dbConfig := makeDbConfig(tb.GetName(), dbName, collection1and2ScopesConfig) |
| 454 | require.NoError(t, dbConfig.setup(ctx, dbName, sc.Config.Bootstrap, nil, nil, false)) |
| 455 | |
| 456 | wg := &sync.WaitGroup{} |
| 457 | wg.Add(1) |
| 458 | var databaseCompleteCount atomic.Int64 |
| 459 | initMgr.testDatabaseCompleteCallback = func(dbName string) { |
| 460 | // On first completion, invoke InitializeDatabase with the same collection set post-completion |
| 461 | if databaseCompleteCount.Add(1) == 1 { |
| 462 | defer wg.Done() |
| 463 | log.Printf("invoking InitializeDatabase again during teardown") |
| 464 | doneChan2, err := initMgr.InitializeDatabase(ctx, sc.Config, dbConfig.ToDatabaseConfig(), testUseLegacySyncDocsIndex) |
| 465 | require.NoError(t, err) |
| 466 | WaitForChannel(t, doneChan2, "done chan 2") |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // Start first async index creation, should block after first collection |
| 471 | doneChan1, err := initMgr.InitializeDatabase(ctx, sc.Config, dbConfig.ToDatabaseConfig(), testUseLegacySyncDocsIndex) |
| 472 | require.NoError(t, err) |
| 473 | |
| 474 | WaitForChannel(t, doneChan1, "done chan 1") |
nothing calls this directly
no test coverage detected