8000 ci: Lock PostgreSQL database creation · coder/coder@28cee52 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28cee52

Browse files
committed
ci: Lock PostgreSQL database creation
There have been race conditions when multiple instances are created at once. This is an attempt to fix!
1 parent 5367d93 commit 28cee52

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

database/postgres/postgres.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ import (
55
"fmt"
66
"io/ioutil"
77
"os"
8+
"sync"
89
"time"
910

1011
"github.com/ory/dockertest/v3"
1112
"github.com/ory/dockertest/v3/docker"
1213
"golang.org/x/xerrors"
1314
)
1415

16+
// Locks the creation of a PostgreSQL instance to one-at-a-time.
17+
// We experienced race conditions when creating multiple instances
18+
// at a time. Somehow they were sharing data...
19+
//
20+
// This may be an upstream issue with "dockertest".
21+
var createMutex sync.Mutex
22+
1523
// Open creates a new PostgreSQL server using a Docker container.
1624
func Open() (string, func(), error) {
25+
createMutex.Lock()
26+
defer createMutex.Unlock()
27+
1728
pool, err := dockertest.NewPool("")
1829
if err != nil {
1930
return "", nil, xerrors.Errorf("create pool: %w", err)
@@ -50,6 +61,7 @@ func Open() (string, func(), error) {
5061
return "", nil, xerrors.Errorf("could not start resource: %w", err)
5162
}
5263
hostAndPort := resource.GetHostPort("5432/tcp")
64+
fmt.Printf("HOST AND PORT %s\n", hostAndPort)
5365
dbURL := fmt.Sprintf("postgres://postgres:postgres@%s/postgres?sslmode=disable", hostAndPort)
5466

5567
// Docker should hard-kill the container after 120 seconds.

0 commit comments

Comments
 (0)
0