Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1bfa7d4

Browse files
authored
chore: add postgres template caching for tests (#15336)
This PR is the first in a series aimed at closing[#15109](#15109).### Changes- **Template Database Creation:** `dbtestutil.Open` now has the ability to create a template database ifnone is provided via `DB_FROM`. The template database’s name is derivedfrom a hash of the migration files, ensuring that it can be reusedacross tests and is automatically updated whenever migrations change.- **Optimized Database Handling:** Previously, `dbtestutil.Open` would spin up a new container for eachtest when `DB_FROM` was unset. Now, it first checks for an activePostgreSQL instance on `localhost:5432`. If none is found, it creates asingle container that remains available for subsequent tests,eliminating repeated container startups.These changes address the long individual test times (10+ seconds)reported by some users, likely due to the time Docker took to start andcomplete migrations.
1 parent1c29944 commit1bfa7d4

File tree

15 files changed

+629
-144
lines changed

15 files changed

+629
-144
lines changed

‎Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ sqlc-vet: test-postgres-docker
765765
test-postgres: test-postgres-docker
766766
# The postgres test is prone to failure, so we limit parallelism for
767767
# more consistent execution.
768-
$(GIT_FLAGS) DB=ciDB_FROM=$(shell go run scripts/migrate-ci/main.go)gotestsum\
768+
$(GIT_FLAGS) DB=ci gotestsum\
769769
--junitfile="gotests.xml"\
770770
--jsonfile="gotests.json"\
771771
--packages="./..." --\

‎cli/resetpassword_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ func TestResetPassword(t *testing.T) {
3232
constnewPassword="MyNewPassword!"
3333

3434
// start postgres and coder server processes
35-
connectionURL,closeFunc,err:=dbtestutil.Open()
35+
connectionURL,err:=dbtestutil.Open(t)
3636
require.NoError(t,err)
37-
defercloseFunc()
3837
ctx,cancelFunc:=context.WithCancel(context.Background())
3938
serverDone:=make(chanstruct{})
4039
serverinv,cfg:=clitest.New(t,

‎cli/server_createadminuser_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ func TestServerCreateAdminUser(t *testing.T) {
8585
// Skip on non-Linux because it spawns a PostgreSQL instance.
8686
t.SkipNow()
8787
}
88-
connectionURL,closeFunc,err:=dbtestutil.Open()
88+
connectionURL,err:=dbtestutil.Open(t)
8989
require.NoError(t,err)
90-
defercloseFunc()
9190

9291
sqlDB,err:=sql.Open("postgres",connectionURL)
9392
require.NoError(t,err)
@@ -151,9 +150,8 @@ func TestServerCreateAdminUser(t *testing.T) {
151150
// Skip on non-Linux because it spawns a PostgreSQL instance.
152151
t.SkipNow()
153152
}
154-
connectionURL,closeFunc,err:=dbtestutil.Open()
153+
connectionURL,err:=dbtestutil.Open(t)
155154
require.NoError(t,err)
156-
defercloseFunc()
157155

158156
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitMedium)
159157
defercancel()
@@ -185,9 +183,8 @@ func TestServerCreateAdminUser(t *testing.T) {
185183
// Skip on non-Linux because it spawns a PostgreSQL instance.
186184
t.SkipNow()
187185
}
188-
connectionURL,closeFunc,err:=dbtestutil.Open()
186+
connectionURL,err:=dbtestutil.Open(t)
189187
require.NoError(t,err)
190-
defercloseFunc()
191188

192189
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitMedium)
193190
defercancel()
@@ -225,9 +222,8 @@ func TestServerCreateAdminUser(t *testing.T) {
225222
// Skip on non-Linux because it spawns a PostgreSQL instance.
226223
t.SkipNow()
227224
}
228-
connectionURL,closeFunc,err:=dbtestutil.Open()
225+
connectionURL,err:=dbtestutil.Open(t)
229226
require.NoError(t,err)
230-
defercloseFunc()
231227
ctx,cancelFunc:=context.WithCancel(context.Background())
232228
defercancelFunc()
233229

‎cli/server_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,9 +1598,8 @@ func TestServer_Production(t *testing.T) {
15981598
// Skip on non-Linux because it spawns a PostgreSQL instance.
15991599
t.SkipNow()
16001600
}
1601-
connectionURL,closeFunc,err:=dbtestutil.Open()
1601+
connectionURL,err:=dbtestutil.Open(t)
16021602
require.NoError(t,err)
1603-
defercloseFunc()
16041603

16051604
// Postgres + race detector + CI = slow.
16061605
ctx,cancelFunc:=context.WithTimeout(context.Background(),testutil.WaitSuperLong*3)
@@ -1803,9 +1802,8 @@ func TestConnectToPostgres(t *testing.T) {
18031802

18041803
log:=slogtest.Make(t,nil)
18051804

1806-
dbURL,closeFunc,err:=dbtestutil.Open()
1805+
dbURL,err:=dbtestutil.Open(t)
18071806
require.NoError(t,err)
1808-
t.Cleanup(closeFunc)
18091807

18101808
sqlDB,err:=cli.ConnectToPostgres(ctx,log,"postgres",dbURL)
18111809
require.NoError(t,err)

‎coderd/database/db_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ func TestNestedInTx(t *testing.T) {
8787
functestSQLDB(t testing.TB)*sql.DB {
8888
t.Helper()
8989

90-
connection,closeFn,err:=dbtestutil.Open()
90+
connection,err:=dbtestutil.Open(t)
9191
require.NoError(t,err)
92-
t.Cleanup(closeFn)
9392

9493
db,err:=sql.Open("postgres",connection)
9594
require.NoError(t,err)

‎coderd/database/dbtestutil/db.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,17 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
9595
opt(&o)
9696
}
9797

98-
db:=dbmem.New()
99-
ps:=pubsub.NewInMemory()
98+
vardbdatabase.Store
99+
varps pubsub.Pubsub
100100
ifWillUsePostgres() {
101101
connectionURL:=os.Getenv("CODER_PG_CONNECTION_URL")
102102
ifconnectionURL==""&&o.url!="" {
103103
connectionURL=o.url
104104
}
105105
ifconnectionURL=="" {
106-
var (
107-
errerror
108-
closePgfunc()
109-
)
110-
connectionURL,closePg,err=Open()
106+
varerrerror
107+
connectionURL,err=Open(t)
111108
require.NoError(t,err)
112-
t.Cleanup(closePg)
113109
}
114110

115111
ifo.fixedTimezone=="" {
@@ -143,6 +139,9 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
143139
t.Cleanup(func() {
144140
_=ps.Close()
145141
})
142+
}else {
143+
db=dbmem.New()
144+
ps=pubsub.NewInMemory()
146145
}
147146

148147
returndb,ps

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp