- Notifications
You must be signed in to change notification settings - Fork1k
chore: fix concurrentCommitQuota
transactions for unrelated users/orgs#15261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
dce750d
4fd9760
6215603
9ccd0b2
784a98f
5f021ad
426eeb6
b69dd0f
ec8f799
f613f31
746203d
c0d05db
245f6df
422e694
685b21e
33dbd92
a99882a
6ce8bfe
3a9270d
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package dbfake | ||
import ( | ||
"testing" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbauthz" | ||
"github.com/coder/coder/v2/coderd/database/dbgen" | ||
"github.com/coder/coder/v2/coderd/database/dbtime" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
type OrganizationBuilder struct { | ||
t *testing.T | ||
db database.Store | ||
seed database.Organization | ||
allUsersAllowance int32 | ||
members []uuid.UUID | ||
groups map[database.Group][]uuid.UUID | ||
} | ||
func Organization(t *testing.T, db database.Store) OrganizationBuilder { | ||
return OrganizationBuilder{ | ||
t: t, | ||
db: db, | ||
members: []uuid.UUID{}, | ||
groups: make(map[database.Group][]uuid.UUID), | ||
} | ||
} | ||
type OrganizationResponse struct { | ||
Org database.Organization | ||
AllUsersGroup database.Group | ||
Members []database.OrganizationMember | ||
Groups []database.Group | ||
} | ||
func (b OrganizationBuilder) EveryoneAllowance(allowance int) OrganizationBuilder { | ||
//nolint: revive // returns modified struct | ||
b.allUsersAllowance = int32(allowance) | ||
return b | ||
} | ||
func (b OrganizationBuilder) Seed(seed database.Organization) OrganizationBuilder { | ||
//nolint: revive // returns modified struct | ||
b.seed = seed | ||
return b | ||
} | ||
func (b OrganizationBuilder) Members(users ...database.User) OrganizationBuilder { | ||
for _, u := range users { | ||
//nolint: revive // returns modified struct | ||
b.members = append(b.members, u.ID) | ||
} | ||
return b | ||
} | ||
func (b OrganizationBuilder) Group(seed database.Group, members ...database.User) OrganizationBuilder { | ||
//nolint: revive // returns modified struct | ||
b.groups[seed] = []uuid.UUID{} | ||
for _, u := range members { | ||
//nolint: revive // returns modified struct | ||
b.groups[seed] = append(b.groups[seed], u.ID) | ||
} | ||
return b | ||
} | ||
func (b OrganizationBuilder) Do() OrganizationResponse { | ||
org := dbgen.Organization(b.t, b.db, b.seed) | ||
ctx := testutil.Context(b.t, testutil.WaitShort) | ||
//nolint:gocritic // builder code needs perms | ||
ctx = dbauthz.AsSystemRestricted(ctx) | ||
everyone, err := b.db.InsertAllUsersGroup(ctx, org.ID) | ||
require.NoError(b.t, err) | ||
if b.allUsersAllowance > 0 { | ||
everyone, err = b.db.UpdateGroupByID(ctx, database.UpdateGroupByIDParams{ | ||
Name: everyone.Name, | ||
DisplayName: everyone.DisplayName, | ||
AvatarURL: everyone.AvatarURL, | ||
QuotaAllowance: b.allUsersAllowance, | ||
ID: everyone.ID, | ||
}) | ||
require.NoError(b.t, err) | ||
} | ||
members := make([]database.OrganizationMember, 0) | ||
if len(b.members) > 0 { | ||
for _, u := range b.members { | ||
newMem := dbgen.OrganizationMember(b.t, b.db, database.OrganizationMember{ | ||
UserID: u, | ||
OrganizationID: org.ID, | ||
CreatedAt: dbtime.Now(), | ||
UpdatedAt: dbtime.Now(), | ||
Roles: nil, | ||
}) | ||
members = append(members, newMem) | ||
} | ||
} | ||
groups := make([]database.Group, 0) | ||
if len(b.groups) > 0 { | ||
for g, users := range b.groups { | ||
g.OrganizationID = org.ID | ||
group := dbgen.Group(b.t, b.db, g) | ||
groups = append(groups, group) | ||
for _, u := range users { | ||
dbgen.GroupMember(b.t, b.db, database.GroupMemberTable{ | ||
UserID: u, | ||
GroupID: group.ID, | ||
}) | ||
} | ||
} | ||
} | ||
return OrganizationResponse{ | ||
Org: org, | ||
AllUsersGroup: everyone, | ||
Members: members, | ||
Groups: groups, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package dbtestutil | ||
import ( | ||
"sync" | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/v2/coderd/database" | ||
) | ||
type DBTx struct { | ||
database.Store | ||
mu sync.Mutex | ||
done chan error | ||
finalErr chan error | ||
} | ||
// StartTx starts a transaction and returns a DBTx object. This allows running | ||
// 2 transactions concurrently in a test more easily. | ||
// Example: | ||
// | ||
//a := StartTx(t, db, opts) | ||
//b := StartTx(t, db, opts) | ||
// | ||
//a.GetUsers(...) | ||
//b.GetUsers(...) | ||
// | ||
//require.NoError(t, a.Done() | ||
func StartTx(t *testing.T, db database.Store, opts *database.TxOptions) *DBTx { | ||
done := make(chan error) | ||
finalErr := make(chan error) | ||
txC := make(chan database.Store) | ||
go func() { | ||
t.Helper() | ||
once := sync.Once{} | ||
count := 0 | ||
err := db.InTx(func(store database.Store) error { | ||
// InTx can be retried | ||
once.Do(func() { | ||
txC <- store | ||
}) | ||
count++ | ||
if count > 1 { | ||
// If you recursively call InTx, then don't use this. | ||
t.Logf("InTx called more than once: %d", count) | ||
assert.NoError(t, xerrors.New("InTx called more than once, this is not allowed with the StartTx helper")) | ||
} | ||
<-done | ||
// Just return nil. The caller should be checking their own errors. | ||
return nil | ||
}, opts) | ||
finalErr <- err | ||
}() | ||
txStore := <-txC | ||
close(txC) | ||
return &DBTx{Store: txStore, done: done, finalErr: finalErr} | ||
} | ||
// Done can only be called once. If you call it twice, it will panic. | ||
func (tx *DBTx) Done() error { | ||
tx.mu.Lock() | ||
defer tx.mu.Unlock() | ||
close(tx.done) | ||
return <-tx.finalErr | ||
} |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.