Skip to content

Commit dce750d

Browse files
committed
chore: making unit tests to try out various things
1 parent 9713887 commit dce750d

File tree

4 files changed

+411
-3
lines changed

4 files changed

+411
-3
lines changed

coderd/database/dbtestutil/tx.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package dbtestutil
2+
3+
import (
4+
"database/sql"
5+
"sync"
6+
"testing"
7+
8+
"github.com/coder/coder/v2/coderd/database"
9+
)
10+
11+
type DBTx struct {
12+
database.Store
13+
mu sync.Mutex
14+
err error
15+
errC chan error
16+
finalErr chan error
17+
}
18+
19+
// StartTx starts a transaction and returns a DBTx object. This allows running
20+
// 2 transactions concurrently in a test more easily.
21+
func StartTx(t *testing.T, db database.Store, opts *sql.TxOptions) *DBTx {
22+
errC := make(chan error)
23+
finalErr := make(chan error)
24+
txC := make(chan database.Store)
25+
26+
go func() {
27+
t.Helper()
28+
once := sync.Once{}
29+
count := 0
30+
31+
err := db.InTx(func(store database.Store) error {
32+
// InTx can be retried
33+
once.Do(func() {
34+
txC <- store
35+
})
36+
count++
37+
if count > 1 {
38+
t.Logf("InTx called more than once: %d", count)
39+
}
40+
return <-errC
41+
}, opts)
42+
finalErr <- err
43+
}()
44+
45+
txStore := <-txC
46+
close(txC)
47+
48+
return &DBTx{Store: txStore, errC: errC, finalErr: finalErr}
49+
}
50+
51+
func (tx *DBTx) SetError(err error) {
52+
tx.mu.Lock()
53+
defer tx.mu.Unlock()
54+
tx.err = err
55+
}
56+
57+
// Done can only be called once. If you call it twice, it will panic.
58+
func (tx *DBTx) Done() error {
59+
tx.mu.Lock()
60+
defer tx.mu.Unlock()
61+
62+
tx.errC <- tx.err
63+
close(tx.errC)
64+
return <-tx.finalErr
65+
}

enterprise/coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
701701

702702
if initial, changed, enabled := featureChanged(codersdk.FeatureTemplateRBAC); shouldUpdate(initial, changed, enabled) {
703703
if enabled {
704-
committer := committer{
704+
committer := Committer{
705705
Log: api.Logger.Named("quota_committer"),
706706
Database: api.Database,
707707
}

enterprise/coderd/workspacequota.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import (
1818
"github.com/coder/coder/v2/provisionerd/proto"
1919
)
2020

21-
type committer struct {
21+
type Committer struct {
2222
Log slog.Logger
2323
Database database.Store
2424
}
2525

26-
func (c *committer) CommitQuota(
26+
func (c *Committer) CommitQuota(
2727
ctx context.Context, request *proto.CommitQuotaRequest,
2828
) (*proto.CommitQuotaResponse, error) {
2929
jobID, err := uuid.Parse(request.JobId)

0 commit comments

Comments
 (0)