Skip to content

Commit e5e3c64

Browse files
committed
Fix: Adding test for testing newsysdb with custom pool
1 parent 8b06027 commit e5e3c64

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

dbos/dbos.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/google/uuid"
24+
"github.com/jackc/pgx/v5/pgxpool"
2425
"github.com/robfig/cron/v3"
2526
)
2627

@@ -42,6 +43,7 @@ type Config struct {
4243
ApplicationVersion string // Application version (optional, overridden by DBOS__APPVERSION env var)
4344
ExecutorID string // Executor ID (optional, overridden by DBOS__VMID env var)
4445
Context context.Context // User Context
46+
Pool *pgxpool.Pool // Custom Pool
4547
}
4648

4749
// processConfig enforces mandatory fields and applies defaults.
@@ -67,6 +69,7 @@ func processConfig(inputConfig *Config) (*Config, error) {
6769
ConductorAPIKey: inputConfig.ConductorAPIKey,
6870
ApplicationVersion: inputConfig.ApplicationVersion,
6971
ExecutorID: inputConfig.ExecutorID,
72+
Pool: inputConfig.Pool,
7073
}
7174

7275
// Load defaults
@@ -327,7 +330,7 @@ func NewDBOSContext(ctx context.Context, inputConfig Config) (DBOSContext, error
327330

328331
newSystemDatabaseInputs := newSystemDatabaseInput{
329332
databaseURL: config.DatabaseURL,
330-
custom_pool: nil,
333+
custom_pool: config.Pool,
331334
logger: initExecutor.logger,
332335
}
333336

dbos/dbos_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package dbos
22

33
import (
4+
"bytes"
45
"context"
6+
"log/slog"
57
"testing"
68
"time"
79

10+
"github.com/jackc/pgx/v5/pgxpool"
811
"github.com/stretchr/testify/assert"
912
"github.com/stretchr/testify/require"
1013
)
@@ -58,6 +61,55 @@ func TestConfig(t *testing.T) {
5861
assert.Equal(t, expectedMsg, dbosErr.Message)
5962
})
6063

64+
t.Run("NewSystemDatabaseWithCustomPool", func(t *testing.T) {
65+
66+
// Logger
67+
var buf bytes.Buffer
68+
slogLogger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{
69+
Level: slog.LevelDebug,
70+
}))
71+
72+
slogLogger = slogLogger.With("service", "dbos-test", "environment", "test")
73+
74+
// Custom Pool
75+
poolConfig, _ := pgxpool.ParseConfig(databaseURL)
76+
77+
poolConfig.MaxConns = 10
78+
poolConfig.MinConns = 5
79+
poolConfig.MaxConnLifetime = 2 * time.Hour
80+
poolConfig.MaxConnIdleTime = time.Minute * 2
81+
82+
poolConfig.ConnConfig.ConnectTimeout = 10 * time.Second
83+
84+
pool, err := pgxpool.NewWithConfig(context.Background(), poolConfig)
85+
require.NoError(t, err)
86+
defer pool.Close()
87+
88+
config := Config{
89+
DatabaseURL: databaseURL,
90+
AppName: "test-custom-pool",
91+
Logger: slogLogger,
92+
Pool: pool,
93+
}
94+
95+
customdbosContext, err := NewDBOSContext(context.Background(), config)
96+
require.NoError(t, err)
97+
require.NotNil(t, customdbosContext)
98+
99+
dbosCtx, ok := customdbosContext.(*dbosContext)
100+
require.True(t, ok)
101+
sysDB, ok := dbosCtx.systemDB.(*sysDB)
102+
require.True(t, ok)
103+
assert.Same(t, pool, sysDB.pool, "The pool in dbosContext should be the same as the custom pool provided")
104+
105+
stats := sysDB.pool.Stat()
106+
assert.Equal(t, int32(10), stats.MaxConns(), "MaxConns should match custom pool config")
107+
108+
customdbosContext.Shutdown(1 * time.Minute)
109+
err = pool.Ping(context.Background())
110+
assert.NoError(t, err, "Custom pool should not be closed by Shutdown and should still be usable")
111+
})
112+
61113
t.Run("FailsWithoutDatabaseURL", func(t *testing.T) {
62114
config := Config{
63115
AppName: "test-app",

0 commit comments

Comments
 (0)