Skip to content

chore: Reduce test times #3856

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore: Reduce test times
  • Loading branch information
kylecarbs committed Sep 3, 2022
commit a9576f8c35ab4551d811fdca3cc58809f9239f47
18 changes: 9 additions & 9 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,11 @@ func AwaitTemplateVersionJob(t *testing.T, client *codersdk.Client, version uuid

t.Logf("waiting for template version job %s", version)
var templateVersion codersdk.TemplateVersion
require.True(t, testutil.EventuallyShort(t, func(ctx context.Context) bool {
require.Eventually(t, func() bool {
var err error
templateVersion, err = client.TemplateVersion(ctx, version)
templateVersion, err = client.TemplateVersion(context.Background(), version)
return assert.NoError(t, err) && templateVersion.Job.CompletedAt != nil
}))
}, testutil.WaitShort, testutil.IntervalFast)
return templateVersion
}

Expand All @@ -469,10 +469,10 @@ func AwaitWorkspaceBuildJob(t *testing.T, client *codersdk.Client, build uuid.UU

t.Logf("waiting for workspace build job %s", build)
var workspaceBuild codersdk.WorkspaceBuild
require.True(t, testutil.EventuallyShort(t, func(ctx context.Context) bool {
workspaceBuild, err := client.WorkspaceBuild(ctx, build)
require.Eventually(t, func() bool {
workspaceBuild, err := client.WorkspaceBuild(context.Background(), build)
return assert.NoError(t, err) && workspaceBuild.Job.CompletedAt != nil
}))
}, testutil.WaitShort, testutil.IntervalFast)
return workspaceBuild
}

Expand All @@ -482,9 +482,9 @@ func AwaitWorkspaceAgents(t *testing.T, client *codersdk.Client, build uuid.UUID

t.Logf("waiting for workspace agents (build %s)", build)
var resources []codersdk.WorkspaceResource
require.True(t, testutil.EventuallyLong(t, func(ctx context.Context) bool {
require.Eventually(t, func() bool {
var err error
resources, err = client.WorkspaceResourcesByBuild(ctx, build)
resources, err = client.WorkspaceResourcesByBuild(context.Background(), build)
if !assert.NoError(t, err) {
return false
}
Expand All @@ -497,7 +497,7 @@ func AwaitWorkspaceAgents(t *testing.T, client *codersdk.Client, build uuid.UUID
}
}
return true
}))
}, testutil.WaitLong, testutil.IntervalFast)
return resources
}

Expand Down
38 changes: 7 additions & 31 deletions coderd/templateversions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,32 +809,24 @@ func TestTemplateVersionDryRun(t *testing.T) {
func TestPaginatedTemplateVersions(t *testing.T) {
t.Parallel()

client := coderdtest.New(t, &coderdtest.Options{APIRateLimit: -1, IncludeProvisionerD: true})
// Prepare database.
client := coderdtest.New(t, &coderdtest.Options{APIRateLimit: -1})
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

// This test takes longer than a long time.
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong*2)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

// Populate database with template versions.
total := 9
eg, egCtx := errgroup.WithContext(ctx)
templateVersionIDs := make([]uuid.UUID, total)
data, err := echo.Tar(nil)
require.NoError(t, err)
file, err := client.Upload(egCtx, codersdk.ContentTypeTar, data)
require.NoError(t, err)
for i := 0; i < total; i++ {
i := i
eg.Go(func() error {
data, err := echo.Tar(nil)
if err != nil {
return err
}
file, err := client.Upload(egCtx, codersdk.ContentTypeTar, data)
if err != nil {
return err
}
templateVersion, err := client.CreateTemplateVersion(egCtx, user.OrganizationID, codersdk.CreateTemplateVersionRequest{
TemplateID: template.ID,
StorageSource: file.Hash,
Expand All @@ -844,29 +836,13 @@ func TestPaginatedTemplateVersions(t *testing.T) {
if err != nil {
return err
}

templateVersionIDs[i] = templateVersion.ID

return nil
})
}
err := eg.Wait()
err = eg.Wait()
require.NoError(t, err, "create templates failed")

for i := 0; i < len(templateVersionIDs); i++ {
// We don't use coderdtest.AwaitTemplateVersionJob here because
// we can't control the timeouts, the concurrent creations take
// a while.
templateVersion, err := client.TemplateVersion(ctx, templateVersionIDs[i])
if err == nil && templateVersion.Job.CompletedAt != nil {
continue
}
require.NotErrorIs(t, err, context.DeadlineExceeded, "template version %d not created in time", i)
// Retry.
time.Sleep(testutil.IntervalMedium)
i--
}

templateVersions, err := client.TemplateVersionsByTemplate(ctx,
codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
Expand Down
24 changes: 0 additions & 24 deletions testutil/eventually.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,3 @@ func Eventually(ctx context.Context, t testing.TB, condition func(context.Contex
}
}
}

// EventuallyShort is a convenience function that runs Eventually with
// IntervalFast and times out after WaitShort.
func EventuallyShort(t testing.TB, condition func(context.Context) bool) bool {
ctx, cancel := context.WithTimeout(context.Background(), WaitShort)
defer cancel()
return Eventually(ctx, t, condition, IntervalFast)
}

// EventuallyMedium is a convenience function that runs Eventually with
// IntervalMedium and times out after WaitMedium.
func EventuallyMedium(t testing.TB, condition func(context.Context) bool) bool {
ctx, cancel := context.WithTimeout(context.Background(), WaitMedium)
defer cancel()
return Eventually(ctx, t, condition, IntervalMedium)
}

// EventuallyLong is a convenience function that runs Eventually with
// IntervalSlow and times out after WaitLong.
func EventuallyLong(t testing.TB, condition func(context.Context) bool) bool {
ctx, cancel := context.WithTimeout(context.Background(), WaitLong)
defer cancel()
return Eventually(ctx, t, condition, IntervalSlow)
}
27 changes: 0 additions & 27 deletions testutil/eventually_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,6 @@ func TestEventually(t *testing.T) {
testutil.Eventually(ctx, t, condition, testutil.IntervalFast)
})

t.Run("Timeout", func(t *testing.T) {
t.Parallel()
condition := func(_ context.Context) bool {
return false
}
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()
mockT := new(testing.T)
testutil.Eventually(ctx, mockT, condition, testutil.IntervalFast)
assert.True(t, mockT.Failed())
})

t.Run("Panic", func(t *testing.T) {
t.Parallel()

Expand All @@ -52,19 +40,4 @@ func TestEventually(t *testing.T) {
}
assert.Panics(t, panicky)
})

t.Run("Short", func(t *testing.T) {
t.Parallel()
testutil.EventuallyShort(t, func(_ context.Context) bool { return true })
})

t.Run("Medium", func(t *testing.T) {
t.Parallel()
testutil.EventuallyMedium(t, func(_ context.Context) bool { return true })
})

t.Run("Long", func(t *testing.T) {
t.Parallel()
testutil.EventuallyLong(t, func(_ context.Context) bool { return true })
})
}