Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(cli): show queue position during workspace builds#12606

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

Merged
dannykopping merged 6 commits intocoder:mainfromdannykopping:dk/cliq
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from1 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
PrevPrevious commit
NextNext commit
Adding tests
Signed-off-by: Danny Kopping <danny@coder.com>
  • Loading branch information
@dannykopping
dannykopping committedMar 15, 2024
commit686c2b73b516cae4f2d0b03389be5dede0ee013a
66 changes: 66 additions & 0 deletionscli/cliui/provisionerjob_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,10 @@ package cliui_test

import (
"context"
"fmt"
"io"
"os"
"regexp"
"runtime"
"sync"
"testing"
Expand DownExpand Up@@ -79,6 +81,70 @@ func TestProvisionerJob(t *testing.T) {
test.PTY.ExpectMatch("Something")
})

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

stage := cliui.ProvisioningStateQueued

tests := []struct {
name string
queuePos int
expected string
}{
{
name: "first",
queuePos: 0,
expected: fmt.Sprintf("%s$", stage),
},
{
name: "next",
queuePos: 1,
expected: fmt.Sprintf(`%s %s$`, stage, regexp.QuoteMeta("(next)")),
},
{
name: "other",
queuePos: 4,
expected: fmt.Sprintf(`%s %s$`, stage, regexp.QuoteMeta("(position: 4)")),
},
}

for _, tc := range tests {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

test := newProvisionerJob(t)
test.JobMutex.Lock()
test.Job.QueuePosition = tc.queuePos
test.Job.QueueSize = tc.queuePos
test.JobMutex.Unlock()

go func() {
<-test.Next
test.JobMutex.Lock()
test.Job.Status = codersdk.ProvisionerJobRunning
now := dbtime.Now()
test.Job.StartedAt = &now
test.JobMutex.Unlock()
<-test.Next
test.JobMutex.Lock()
test.Job.Status = codersdk.ProvisionerJobSucceeded
now = dbtime.Now()
test.Job.CompletedAt = &now
close(test.Logs)
test.JobMutex.Unlock()
}()
test.PTY.ExpectRegexMatch(tc.expected)
test.Next <- struct{}{}
test.PTY.ExpectMatch(cliui.ProvisioningStateQueued) // step completed
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
test.Next <- struct{}{}
test.PTY.ExpectMatch(cliui.ProvisioningStateRunning)
})
}
})

// This cannot be ran in parallel because it uses a signal.
// nolint:paralleltest
t.Run("Cancel", func(t *testing.T) {
Expand Down
25 changes: 23 additions & 2 deletionspty/ptytest/ptytest.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ import (
"context"
"fmt"
"io"
"regexp"
"runtime"
"strings"
"sync"
Expand DownExpand Up@@ -145,16 +146,36 @@ type outExpecter struct {
}

func (e *outExpecter) ExpectMatch(str string) string {
return e.expectMatchContext(str, e.ExpectMatchContext)
}

func (e *outExpecter) ExpectRegexMatch(str string) string {
return e.expectMatchContext(str, e.ExpectRegexMatchContext)
}

func (e *outExpecter) expectMatchContext(str string, impl func(ctx context.Context, str string) string) string {
e.t.Helper()

timeout, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
defer cancel()

returne.ExpectMatchContext(timeout, str)
returnimpl(timeout, str)
}

// TODO(mafredri): Rename this to ExpectMatch when refactoring.
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@mafredri not really sure what this TODO is about since both this and theExpectMatch functions have lots of usage. Could you elaborate please?

Copy link
Member

@mafredrimafredriMar 15, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Essentially the non-context variant should be marked deprecated. All usage switched to context. Then delete the non-context one and rename this to not have context in the name.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

OK, is it because we need to be able to control the deadline?
I think I'll do that in a separate PR.

func (e *outExpecter) ExpectMatchContext(ctx context.Context, str string) string {
return e.expectMatcher(ctx, str, func(src, pattern string) bool {
return strings.Contains(src, pattern)
})
}

func (e *outExpecter) ExpectRegexMatchContext(ctx context.Context, str string) string {
return e.expectMatcher(ctx, str, func(src, pattern string) bool {
return regexp.MustCompile(pattern).MatchString(src)
})
}

func (e *outExpecter) expectMatcher(ctx context.Context, str string, matchFn func(src, pattern string) bool) string {
e.t.Helper()

var buffer bytes.Buffer
Expand All@@ -168,7 +189,7 @@ func (e *outExpecter) ExpectMatchContext(ctx context.Context, str string) string
if err != nil {
return err
}
ifstrings.Contains(buffer.String(), str) {
ifmatchFn(buffer.String(), str) {
return nil
}
}
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp