Conversation
Summary of ChangesHello @rahulpinto19, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the reliability and error handling of PostgreSQL-based integration tests. By modifying the core table setup utility to propagate errors and updating all relevant test suites to properly handle these errors, the changes ensure that test failures due to setup issues are caught promptly and that database resources are cleaned up effectively, leading to more stable and predictable test runs. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the test setup for Postgres tables to improve error handling, which is a good direction. However, the implementation introduces a few issues. There's a critical bug in tests/common.go where pool.Query is used instead of pool.Exec for DDL/DML, which can lead to connection leaks. Additionally, there's a critical bug in tests/alloydbpg/alloydb_pg_integration_test.go that results in a resource leak because a teardown function isn't called on success. Other test files have functionally correct but verbose and redundant error handling logic that can be simplified for better readability and maintainability. My review includes suggestions to fix these issues.
There was a problem hiding this comment.
The teardown function teardownTable1 is only deferred in the error case. If SetupPostgresSQLTable succeeds, the teardown function is never called, leading to a resource leak where the test table is not dropped. This can cause issues in subsequent test runs.
The defer statement should be placed to ensure cleanup happens in both success and partial failure scenarios.
teardownTable1, err := tests.SetupPostgresSQLTable(t, ctx, pool, createParamTableStmt, insertParamTableStmt, tableNameParam, paramTestParams)
if teardownTable1 != nil {
defer teardownTable1(t)
}
if err != nil {
t.Fatalf("Setup failed: %v", err)
}
tests/common.go
Outdated
There was a problem hiding this comment.
Using pool.Query for CREATE and INSERT statements is incorrect and can lead to connection leaks. pool.Query is for queries that return rows (like SELECT), and the returned pgx.Rows object must be closed to release the connection back to the pool. For statements that don't return rows, pool.Exec should be used.
This applies to both the CREATE TABLE statement on line 624 and the INSERT statement on line 630.
// Create table
_, err = pool.Exec(ctx, createStatement)
if err != nil {
return nil, fmt.Errorf("unable to create test table %s: %w", tableName, err)
}
// Insert test data
_, err = pool.Exec(ctx, insertStatement, params...)There was a problem hiding this comment.
The error handling pattern here can be simplified. The teardownTable2 function is deferred in both the error case and the success case, which is redundant. A cleaner pattern is to defer the teardown if it exists, and then check for the error. This avoids code duplication and is easier to read.
teardownTable2, err := tests.SetupPostgresSQLTable(t, ctx, pool, createAuthTableStmt, insertAuthTableStmt, tableNameAuth, authTestParams)
if teardownTable2 != nil {
defer teardownTable2(t)
}
if err != nil {
t.Fatalf("Setup failed: %v", err)
}There was a problem hiding this comment.
The error handling pattern here can be simplified. The teardownTable1 function is deferred in both the error case and the success case, which is redundant. A cleaner pattern is to defer the teardown if it exists, and then check for the error. This avoids code duplication and is easier to read.
teardownTable1, err := tests.SetupPostgresSQLTable(t, ctx, pool, createParamTableStmt, insertParamTableStmt, tableNameParam, paramTestParams)
if teardownTable1 != nil {
defer teardownTable1(t)
}
if err != nil {
t.Fatalf("Setup failed: %v", err)
}There was a problem hiding this comment.
The error handling pattern here can be simplified. The teardownTable2 function is deferred in both the error case and the success case, which is redundant. A cleaner pattern is to defer the teardown if it exists, and then check for the error. This avoids code duplication and is easier to read.
teardownTable2, err := tests.SetupPostgresSQLTable(t, ctx, pool, createAuthTableStmt, insertAuthTableStmt, tableNameAuth, authTestParams)
if teardownTable2 != nil {
defer teardownTable2(t)
}
if err != nil {
t.Fatalf("Setup failed: %v", err)
}There was a problem hiding this comment.
The error handling pattern here can be simplified. The teardownTable1 function is deferred in both the error case and the success case, which is redundant. A cleaner pattern is to defer the teardown if it exists, and then check for the error. This avoids code duplication and is easier to read.
Also, the commented-out code on line 106 should be removed.
teardownTable1, err := tests.SetupPostgresSQLTable(t, ctx, pool, createParamTableStmt, insertParamTableStmt, tableNameParam, paramTestParams)
if teardownTable1 != nil {
defer teardownTable1(t)
}
if err != nil {
t.Fatalf("Setup failed: %v", err)
}There was a problem hiding this comment.
The error handling pattern here can be simplified. The teardownTable2 function is deferred in both the error case and the success case, which is redundant. A cleaner pattern is to defer the teardown if it exists, and then check for the error. This avoids code duplication and is easier to read.
teardownTable2, err := tests.SetupPostgresSQLTable(t, ctx, pool, createAuthTableStmt, insertAuthTableStmt, tableNameAuth, authTestParams)
if teardownTable2 != nil {
defer teardownTable2(t)
}
if err != nil {
t.Fatalf("Setup failed: %v", err)
}
issue: The Toolbox integration tests are not cleaning up test databases correctly, causing resource leaks
Steps Taken
Explicit Error Returns: SetupPostgresSQLTable now returns an error, allowing tests to fail immediately on setup issues.
Resilient Teardown: Cleanup logic now uses DROP TABLE IF EXISTS to prevent crashes during teardown.
Test Suite Updates: All relevant integration tests updated to handle setup errors and ensure proper resource cleanup.