Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rollback db timeout #2177

Merged
merged 1 commit into from
Aug 22, 2024
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
8 changes: 1 addition & 7 deletions node/pkg/db/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"
"sync"
"time"

errorSentinel "bisonai.com/miko/node/pkg/error"
"bisonai.com/miko/node/pkg/secrets"
Expand All @@ -23,8 +22,6 @@ var (
poolErr error
)

const DefaultDBTimeout = 60 * time.Second

func GetPool(ctx context.Context) (*pgxpool.Pool, error) {
return getPool(ctx, &initPgxOnce)
}
Expand Down Expand Up @@ -103,10 +100,7 @@ func QueryRows[T any](ctx context.Context, queryString string, args map[string]a
}

func query(ctx context.Context, pool *pgxpool.Pool, query string, args map[string]any) (pgx.Rows, error) {
ctxWithTimeout, cancel := context.WithTimeout(ctx, DefaultDBTimeout)
defer cancel()

return pool.Query(ctxWithTimeout, query, pgx.NamedArgs(args))
return pool.Query(ctx, query, pgx.NamedArgs(args))
}

func queryRow[T any](ctx context.Context, pool *pgxpool.Pool, queryString string, args map[string]any) (T, error) {
Expand Down
63 changes: 0 additions & 63 deletions node/pkg/db/pgsql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package db

import (
"context"
"errors"
"os"
"reflect"
"testing"
)
Expand Down Expand Up @@ -502,64 +500,3 @@ func TestBulkSelect(t *testing.T) {
})

}

func TestQueryTimeout(t *testing.T) {
t.Skip("")
// Setting up the context with a short timeout
ctx := context.Background()

pool, err := GetPool(ctx)
if err != nil {
t.Fatalf("GetPool failed: %v", err)
}

// Create a temporary table (optional, depending on your test needs)
_, err = pool.Exec(ctx, `CREATE TEMPORARY TABLE test_timeout (id SERIAL PRIMARY KEY, name TEXT)`)
if err != nil {
t.Fatalf("Failed to create temporary table: %v", err)
}

// Simulate a long-running query using pg_sleep (2 seconds)
_, err = QueryRow[struct {
Name string `db:"name"`
}](ctx, `SELECT pg_sleep(16)`, nil)

// Check for context.DeadlineExceeded error
if err == nil {
t.Fatalf("Expected timeout error but got none")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Expected context.DeadlineExceeded error, but got: %v", err)
}
}

func TestQueryTimeoutTransient(t *testing.T) {
t.Skip("")
ctx := context.Background()

dbUrl := os.Getenv("DATABASE_URL")
pool, err := GetTransientPool(ctx, dbUrl)
if err != nil {
t.Fatalf("GetPool failed: %v", err)
}
defer pool.Close()

// Create a temporary table (optional, depending on your test needs)
_, err = pool.Exec(ctx, `CREATE TEMPORARY TABLE test_timeout (id SERIAL PRIMARY KEY, name TEXT)`)
if err != nil {
t.Fatalf("Failed to create temporary table: %v", err)
}

// Simulate a long-running query using pg_sleep (2 seconds)
_, err = QueryRow[struct {
Name string `db:"name"`
}](ctx, `SELECT pg_sleep(16)`, nil)

// Check for context.DeadlineExceeded error
if err == nil {
t.Fatalf("Expected timeout error but got none")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Expected context.DeadlineExceeded error, but got: %v", err)
}
}
Loading