Skip to content

Commit 50d8151

Browse files
authored
ci: Run tests using PostgreSQL database and mock (#49)
* ci: Run tests using PostgreSQL database and mock This allows us to use the mock database for quick iterative testing, and have confidence from CI using a real PostgreSQL database. PostgreSQL tests are only ran on Linux. They are *really* slow on MacOS and Windows runners, and don't provide much additional confidence. * Only run PostgreSQL tests once for speed * Fix race condition of log after close Not all resources were cleaned up immediately after a peer connection was closed. DataChannels could have a goroutine exit after Close() prior to this. * Fix comment
1 parent dfddaf1 commit 50d8151

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

.github/workflows/coder.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,19 @@ jobs:
149149
150150
- run: go install gotest.tools/gotestsum@latest
151151

152-
- run:
152+
- name: Test with Mock Database
153+
run:
153154
gotestsum --jsonfile="gotests.json" --packages="./..." --
154155
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
155156
-count=3 -race -parallel=2
156157

158+
- name: Test with PostgreSQL Database
159+
if: runner.os == 'Linux'
160+
run:
161+
DB=true gotestsum --jsonfile="gotests.json" --packages="./..." --
162+
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
163+
-count=1 -race -parallel=2
164+
157165
- uses: codecov/codecov-action@v2
158166
with:
159167
token: ${{ secrets.CODECOV_TOKEN }}

coderd/coderdtest/coderdtest.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ package coderdtest
22

33
import (
44
"context"
5+
"database/sql"
56
"net/http/httptest"
67
"net/url"
8+
"os"
79
"testing"
810

911
"github.com/stretchr/testify/require"
1012

1113
"cdr.dev/slog/sloggers/slogtest"
1214
"github.com/coder/coder/coderd"
1315
"github.com/coder/coder/codersdk"
16+
"github.com/coder/coder/database"
1417
"github.com/coder/coder/database/databasefake"
18+
"github.com/coder/coder/database/postgres"
1519
)
1620

1721
// Server represents a test instance of coderd.
@@ -27,6 +31,20 @@ type Server struct {
2731
func New(t *testing.T) Server {
2832
// This can be hotswapped for a live database instance.
2933
db := databasefake.New()
34+
if os.Getenv("DB") != "" {
35+
connectionURL, close, err := postgres.Open()
36+
require.NoError(t, err)
37+
t.Cleanup(close)
38+
sqlDB, err := sql.Open("postgres", connectionURL)
39+
require.NoError(t, err)
40+
t.Cleanup(func() {
41+
_ = sqlDB.Close()
42+
})
43+
err = database.Migrate(sqlDB)
44+
require.NoError(t, err)
45+
db = database.New(sqlDB)
46+
}
47+
3048
handler := coderd.New(&coderd.Options{
3149
Logger: slogtest.Make(t, nil),
3250
Database: db,

database/query.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ INSERT INTO
8686
email,
8787
name,
8888
login_type,
89+
revoked,
8990
hashed_password,
9091
created_at,
9192
updated_at,
9293
username
9394
)
9495
VALUES
95-
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
96+
($1, $2, $3, $4, false, $5, $6, $7, $8) RETURNING *;
9697

9798
-- name: UpdateAPIKeyByID :exec
9899
UPDATE

database/query.sql.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

peer/channel.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func (c *Channel) init() {
135135

136136
c.conn.dcDisconnectListeners.Add(1)
137137
c.conn.dcFailedListeners.Add(1)
138+
c.conn.dcClosedWaitGroup.Add(1)
138139
go func() {
139140
var err error
140141
// A DataChannel can disconnect multiple times, so this needs to loop.
@@ -274,6 +275,7 @@ func (c *Channel) closeWithError(err error) error {
274275
close(c.sendMore)
275276
c.conn.dcDisconnectListeners.Sub(1)
276277
c.conn.dcFailedListeners.Sub(1)
278+
c.conn.dcClosedWaitGroup.Done()
277279

278280
if c.rwc != nil {
279281
_ = c.rwc.Close()

peer/conn.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ type Conn struct {
113113
dcDisconnectListeners atomic.Uint32
114114
dcFailedChannel chan struct{}
115115
dcFailedListeners atomic.Uint32
116+
dcClosedWaitGroup sync.WaitGroup
116117

117118
localCandidateChannel chan webrtc.ICECandidateInit
118119
localSessionDescriptionChannel chan webrtc.SessionDescription
@@ -125,11 +126,10 @@ type Conn struct {
125126
pingEchoChan *Channel
126127
pingEchoOnce sync.Once
127128
pingEchoError error
128-
129-
pingMutex sync.Mutex
130-
pingOnce sync.Once
131-
pingChan *Channel
132-
pingError error
129+
pingMutex sync.Mutex
130+
pingOnce sync.Once
131+
pingChan *Channel
132+
pingError error
133133
}
134134

135135
func (c *Conn) init() error {
@@ -502,5 +502,10 @@ func (c *Conn) CloseWithError(err error) error {
502502
// this call will return an error that isn't typed. We don't check the error because
503503
// closing an already closed connection isn't an issue for us.
504504
_ = c.rtc.Close()
505+
506+
// Waits for all DataChannels to exit before officially labeling as closed.
507+
// All logging, goroutines, and async functionality is cleaned up after this.
508+
c.dcClosedWaitGroup.Wait()
509+
505510
return err
506511
}

0 commit comments

Comments
 (0)