Skip to content

Commit 4f7e32e

Browse files
committed
fix: Speed up TestPaginatedUsers by creating users concurrently
1 parent c352a07 commit 4f7e32e

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

coderd/users_test.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/google/uuid"
1313
"github.com/stretchr/testify/require"
14+
"golang.org/x/sync/errgroup"
1415

1516
"github.com/coder/coder/coderd/coderdtest"
1617
"github.com/coder/coder/coderd/rbac"
@@ -1102,38 +1103,48 @@ func TestPaginatedUsers(t *testing.T) {
11021103
require.NoError(t, err)
11031104
orgID := me.OrganizationIDs[0]
11041105

1105-
allUsers := make([]codersdk.User, 0)
1106-
allUsers = append(allUsers, me)
1107-
specialUsers := make([]codersdk.User, 0)
1108-
11091106
// When 100 users exist
11101107
total := 100
1108+
allUsers := make([]codersdk.User, total+1)
1109+
allUsers[0] = me
1110+
specialUsers := make([]codersdk.User, total/2)
1111+
1112+
eg, egCtx := errgroup.WithContext(ctx)
11111113
// Create users
11121114
for i := 0; i < total; i++ {
1113-
email := fmt.Sprintf("%d@coder.com", i)
1114-
username := fmt.Sprintf("user%d", i)
1115-
if i%2 == 0 {
1116-
email = fmt.Sprintf("%d@gmail.com", i)
1117-
username = fmt.Sprintf("specialuser%d", i)
1118-
}
1119-
// One side effect of having to use the api vs the db calls directly, is you cannot
1120-
// mock time. Ideally I could pass in mocked times and space these users out.
1121-
//
1122-
// But this also serves as a good test. Postgres has microsecond precision on its timestamps.
1123-
// If 2 users share the same created_at, that could cause an issue if you are strictly paginating via
1124-
// timestamps. The pagination goes by timestamps and uuids.
1125-
newUser, err := client.CreateUser(ctx, codersdk.CreateUserRequest{
1126-
Email: email,
1127-
Username: username,
1128-
Password: "password",
1129-
OrganizationID: orgID,
1115+
i := i
1116+
eg.Go(func() error {
1117+
email := fmt.Sprintf("%d@coder.com", i)
1118+
username := fmt.Sprintf("user%d", i)
1119+
if i%2 == 0 {
1120+
email = fmt.Sprintf("%d@gmail.com", i)
1121+
username = fmt.Sprintf("specialuser%d", i)
1122+
}
1123+
// One side effect of having to use the api vs the db calls directly, is you cannot
1124+
// mock time. Ideally I could pass in mocked times and space these users out.
1125+
//
1126+
// But this also serves as a good test. Postgres has microsecond precision on its timestamps.
1127+
// If 2 users share the same created_at, that could cause an issue if you are strictly paginating via
1128+
// timestamps. The pagination goes by timestamps and uuids.
1129+
newUser, err := client.CreateUser(egCtx, codersdk.CreateUserRequest{
1130+
Email: email,
1131+
Username: username,
1132+
Password: "password",
1133+
OrganizationID: orgID,
1134+
})
1135+
if err != nil {
1136+
return err
1137+
}
1138+
allUsers[i+1] = newUser
1139+
if i%2 == 0 {
1140+
specialUsers[i/2] = newUser
1141+
}
1142+
1143+
return nil
11301144
})
1131-
require.NoError(t, err)
1132-
allUsers = append(allUsers, newUser)
1133-
if i%2 == 0 {
1134-
specialUsers = append(specialUsers, newUser)
1135-
}
11361145
}
1146+
err = eg.Wait()
1147+
require.NoError(t, err, "create users failed")
11371148

11381149
// Sorting the users will sort by (created_at, uuid). This is to handle
11391150
// the off case that created_at is identical for 2 users.

0 commit comments

Comments
 (0)