|
1 | 1 | package dbos |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
| 6 | + "log/slog" |
5 | 7 | "testing" |
6 | 8 | "time" |
7 | 9 |
|
| 10 | + "github.com/jackc/pgx/v5/pgxpool" |
8 | 11 | "github.com/stretchr/testify/assert" |
9 | 12 | "github.com/stretchr/testify/require" |
10 | 13 | ) |
@@ -58,6 +61,60 @@ func TestConfig(t *testing.T) { |
58 | 61 | assert.Equal(t, expectedMsg, dbosErr.Message) |
59 | 62 | }) |
60 | 63 |
|
| 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, err := pgxpool.ParseConfig(databaseURL) |
| 76 | + require.NoError(t, err) |
| 77 | + |
| 78 | + poolConfig.MaxConns = 10 |
| 79 | + poolConfig.MinConns = 5 |
| 80 | + poolConfig.MaxConnLifetime = 2 * time.Hour |
| 81 | + poolConfig.MaxConnIdleTime = time.Minute * 2 |
| 82 | + |
| 83 | + poolConfig.ConnConfig.ConnectTimeout = 10 * time.Second |
| 84 | + |
| 85 | + pool, err := pgxpool.NewWithConfig(context.Background(), poolConfig) |
| 86 | + require.NoError(t, err) |
| 87 | + |
| 88 | + config := Config{ |
| 89 | + DatabaseURL: databaseURL, |
| 90 | + AppName: "test-custom-pool", |
| 91 | + Logger: slogLogger, |
| 92 | + SystemDBPool: 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 | + defer dbosCtx.Shutdown(10 * time.Second) |
| 101 | + require.True(t, ok) |
| 102 | + |
| 103 | + sysDB, ok := dbosCtx.systemDB.(*sysDB) |
| 104 | + require.True(t, ok) |
| 105 | + assert.Same(t, pool, sysDB.pool, "The pool in dbosContext should be the same as the custom pool provided") |
| 106 | + |
| 107 | + stats := sysDB.pool.Stat() |
| 108 | + assert.Equal(t, int32(10), stats.MaxConns(), "MaxConns should match custom pool config") |
| 109 | + |
| 110 | + sysdbConfig := sysDB.pool.Config() |
| 111 | + assert.Equal(t, int32(10), sysdbConfig.MaxConns) |
| 112 | + assert.Equal(t, int32(5), sysdbConfig.MinConns) |
| 113 | + assert.Equal(t, 2*time.Hour, sysdbConfig.MaxConnLifetime) |
| 114 | + assert.Equal(t, 2*time.Minute, sysdbConfig.MaxConnIdleTime) |
| 115 | + assert.Equal(t, 10*time.Second, sysdbConfig.ConnConfig.ConnectTimeout) |
| 116 | + }) |
| 117 | + |
61 | 118 | t.Run("FailsWithoutDatabaseURL", func(t *testing.T) { |
62 | 119 | config := Config{ |
63 | 120 | AppName: "test-app", |
|
0 commit comments