Skip to content

Commit bebe4f0

Browse files
chore: sort inserted users on dbmem (coder#15483)
1 parent 97b3bbf commit bebe4f0

File tree

1 file changed

+3
-15
lines changed

1 file changed

+3
-15
lines changed

coderd/database/dbmem/dbmem.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -7714,21 +7714,6 @@ func (q *FakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
77147714
return database.User{}, err
77157715
}
77167716

7717-
// There is a common bug when using dbmem that 2 inserted users have the
7718-
// same created_at time. This causes user order to not be deterministic,
7719-
// which breaks some unit tests.
7720-
// To fix this, we make sure that the created_at time is always greater
7721-
// than the last user's created_at time.
7722-
allUsers, _ := q.GetUsers(context.Background(), database.GetUsersParams{})
7723-
if len(allUsers) > 0 {
7724-
lastUser := allUsers[len(allUsers)-1]
7725-
if arg.CreatedAt.Before(lastUser.CreatedAt) ||
7726-
arg.CreatedAt.Equal(lastUser.CreatedAt) {
7727-
// 1 ms is a good enough buffer.
7728-
arg.CreatedAt = lastUser.CreatedAt.Add(time.Millisecond)
7729-
}
7730-
}
7731-
77327717
q.mutex.Lock()
77337718
defer q.mutex.Unlock()
77347719

@@ -7756,6 +7741,9 @@ func (q *FakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
77567741
LoginType: arg.LoginType,
77577742
}
77587743
q.users = append(q.users, user)
7744+
sort.Slice(q.users, func(i, j int) bool {
7745+
return q.users[i].CreatedAt.Before(q.users[j].CreatedAt)
7746+
})
77597747
return user, nil
77607748
}
77617749

0 commit comments

Comments
 (0)