Skip to content

Commit d9ebe5d

Browse files
authored
fix: improve list locks integration test for postgres (#2279)
## Description Fix: Fixed the flakiness in the list locks integration test for postgres ``` go test -tags=integration tests/postgres/postgres_integration_test.go ok command-line-arguments 14.392s ``` ## PR Checklist - [X] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [X] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [X] Ensure the tests and linter pass - [X] Code coverage does not decrease (if any source code was changed) - [X] Appropriate docs were updated (if necessary) - [X] Make sure to add `!` if this involve a breaking change 🛠️ Fixes #1738
1 parent a11b5c5 commit d9ebe5d

1 file changed

Lines changed: 44 additions & 29 deletions

File tree

tests/tool.go

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/google/uuid"
3636
"github.com/googleapis/genai-toolbox/internal/server/mcp/jsonrpc"
3737
"github.com/googleapis/genai-toolbox/internal/sources"
38+
"github.com/jackc/pgx/v5"
3839
"github.com/jackc/pgx/v5/pgxpool"
3940
)
4041

@@ -3724,26 +3725,43 @@ func RunMSSQLListTablesTest(t *testing.T, tableNameParam, tableNameAuth string)
37243725
}
37253726
}
37263727

3728+
func CreateAndLockPostgresTable(t *testing.T, ctx context.Context, pool *pgxpool.Pool, tableName string) func() {
3729+
_, err := pool.Exec(ctx, fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (id INT PRIMARY KEY)", pgx.Identifier{tableName}.Sanitize()))
3730+
if err != nil {
3731+
t.Fatalf("unable to create table: %s", err)
3732+
}
3733+
3734+
tx, err := pool.BeginTx(ctx, pgx.TxOptions{})
3735+
if err != nil {
3736+
t.Fatalf("unable to create transaction: %s", err)
3737+
}
3738+
if _, err := tx.Exec(ctx, fmt.Sprintf("LOCK TABLE %s IN ACCESS EXCLUSIVE MODE", pgx.Identifier{tableName}.Sanitize())); err != nil {
3739+
t.Fatalf("unable to acquire lock: %s", err)
3740+
}
3741+
3742+
return func() {
3743+
if err := tx.Rollback(ctx); err != nil {
3744+
t.Fatalf("unable to rollback transaction: %s", err)
3745+
}
3746+
if _, err := pool.Exec(ctx, fmt.Sprintf("DROP TABLE IF EXISTS %s", pgx.Identifier{tableName}.Sanitize())); err != nil {
3747+
t.Fatalf("unable to drop table: %s", err)
3748+
}
3749+
}
3750+
}
3751+
37273752
// RunPostgresListLocksTest runs tests for the postgres list-locks tool
37283753
func RunPostgresListLocksTest(t *testing.T, ctx context.Context, pool *pgxpool.Pool) {
3754+
3755+
// Create and lock a test table
3756+
cleanup := CreateAndLockPostgresTable(t, ctx, pool, "test_postgres_list_locks_table")
3757+
defer cleanup()
3758+
37293759
type lockDetails struct {
3730-
Pid any `json:"pid"`
3731-
Usename string `json:"usename"`
3732-
Database string `json:"database"`
3733-
RelName string `json:"relname"`
3734-
LockType string `json:"locktype"`
3735-
Mode string `json:"mode"`
3736-
Granted bool `json:"granted"`
3737-
FastPath bool `json:"fastpath"`
3738-
VirtualXid any `json:"virtualxid"`
3739-
TransactionId any `json:"transactionid"`
3740-
ClassId any `json:"classid"`
3741-
ObjId any `json:"objid"`
3742-
ObjSubId any `json:"objsubid"`
3743-
PageNumber any `json:"page"`
3744-
TupleNumber any `json:"tuple"`
3745-
VirtualBlock any `json:"virtualblock"`
3746-
BlockNumber any `json:"blockno"`
3760+
Pid any `json:"pid"`
3761+
Usename string `json:"usename"`
3762+
Query string `json:"query"`
3763+
TrxID string `json:"trxid"`
3764+
Locks string `json:"locks"`
37473765
}
37483766

37493767
invokeTcs := []struct {
@@ -3752,12 +3770,12 @@ func RunPostgresListLocksTest(t *testing.T, ctx context.Context, pool *pgxpool.P
37523770
wantStatusCode int
37533771
expectResults bool
37543772
}{
3755-
// {
3756-
// name: "invoke list_locks with no arguments",
3757-
// requestBody: bytes.NewBuffer([]byte(`{}`)),
3758-
// wantStatusCode: http.StatusOK,
3759-
// expectResults: false, // locks may or may not exist
3760-
// },
3773+
{
3774+
name: "invoke list_locks with no arguments",
3775+
requestBody: bytes.NewBuffer([]byte(`{}`)),
3776+
wantStatusCode: http.StatusOK,
3777+
expectResults: true,
3778+
},
37613779
}
37623780
for _, tc := range invokeTcs {
37633781
t.Run(tc.name, func(t *testing.T) {
@@ -3788,12 +3806,9 @@ func RunPostgresListLocksTest(t *testing.T, ctx context.Context, pool *pgxpool.P
37883806
t.Fatalf("failed to unmarshal result: %v, result string: %s", err, resultString)
37893807
}
37903808
}
3791-
3792-
// Verify that if results exist, they have the expected structure
3793-
for _, lock := range got {
3794-
if lock.LockType == "" {
3795-
t.Errorf("lock type should not be empty")
3796-
}
3809+
// Verify that we got results if expected
3810+
if tc.expectResults && len(got) == 0 {
3811+
t.Errorf("expected results but got none")
37973812
}
37983813
})
37993814
}

0 commit comments

Comments
 (0)