Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions internal/integration/unified/client_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ type clientEntity struct {
logQueue chan orderedLogMessage
}

// awaitMinimumPoolSize waits for the client's connection pool to reach the
// specified minimum size. This is a best effort operation that times out after
// some predefined amount of time to avoid blocking tests indefinitely.
func awaitMinimumPoolSize(ctx context.Context, entity *clientEntity, minPoolSize uint64) error {
// Don't spend longer than 500ms awaiting minPoolSize.
awaitCtx, cancel := context.WithTimeout(ctx, 500*time.Millisecond)
defer cancel()

ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()

for {
select {
case <-awaitCtx.Done():
return fmt.Errorf("timed out waiting for client to reach minPoolSize")
case <-ticker.C:
if uint64(entity.eventsCount[connectionReadyEvent]) >= minPoolSize {
return nil
}
}
}
}

func newClientEntity(ctx context.Context, em *EntityMap, entityOptions *entityOptions) (*clientEntity, error) {
// The "configureFailPoint" command should always be ignored.
ignoredCommands := map[string]struct{}{
Expand Down Expand Up @@ -203,6 +226,12 @@ func newClientEntity(ctx context.Context, em *EntityMap, entityOptions *entityOp
return nil, fmt.Errorf("error creating mongo.Client: %w", err)
}

if entityOptions.AwaitMinPoolSize && clientOpts.MinPoolSize != nil && *clientOpts.MinPoolSize > 0 {
if err := awaitMinimumPoolSize(ctx, entity, *clientOpts.MinPoolSize); err != nil {
return nil, err
}
}

entity.Client = client
Comment on lines +229 to 235
Copy link
Preview

Copilot AI Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The awaitMinimumPoolSize function is called with entity before entity.Client is assigned (line 235). This means the entity doesn't have a valid client when the function tries to check connection events, which could cause the function to behave incorrectly.

Suggested change
if entityOptions.AwaitMinPoolSize && clientOpts.MinPoolSize != nil && *clientOpts.MinPoolSize > 0 {
if err := awaitMinimumPoolSize(ctx, entity, *clientOpts.MinPoolSize); err != nil {
return nil, err
}
}
entity.Client = client
entity.Client = client
if entityOptions.AwaitMinPoolSize && clientOpts.MinPoolSize != nil && *clientOpts.MinPoolSize > 0 {
if err := awaitMinimumPoolSize(ctx, entity, *clientOpts.MinPoolSize); err != nil {
return nil, err
}
}

Copilot uses AI. Check for mistakes.

Copy link
Member Author

@prestonvasquez prestonvasquez Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't matter since the pool event monitor was defined before client construction.

return entity, nil
}
Expand Down
6 changes: 6 additions & 0 deletions internal/integration/unified/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ type entityOptions struct {
DatabaseID string `bson:"database"`

ClientEncryptionOpts *clientEncryptionOpts `bson:"clientEncryptionOpts"`

// If true, the unified spec runner must wait for the connection pool to be
// populated for all servers according to the minPoolSize option. If false,
// not specified, or if minPoolSize equals 0, there is no need to wait for any
// specific pool state.
AwaitMinPoolSize bool `bson:"awaitMinPoolSize"`
}

func (eo *entityOptions) setHeartbeatFrequencyMS(freq time.Duration) {
Expand Down
2 changes: 1 addition & 1 deletion internal/integration/unified/schema_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

var (
supportedSchemaVersions = map[int]string{
1: "1.22",
1: "1.26",
}
)

Expand Down
Loading