-
Notifications
You must be signed in to change notification settings - Fork 881
fix(cli): port-forward: update workspace last_used_at #12659
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
Changes from 10 commits
279f874
8f9b945
e8c842c
86704a1
c99327c
5876edd
e4e0311
958d1d1
a36aeb9
692f666
d794e00
45a0eef
8e40efd
591e1ab
0caaf3a
cc72868
f5f8d75
a2e716d
5b64f96
23ccf21
c9ac9d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ import ( | |
"github.com/coder/coder/v2/coderd/util/ptr" | ||
"github.com/coder/coder/v2/coderd/workspaceapps" | ||
"github.com/coder/coder/v2/coderd/workspaceapps/appurl" | ||
"github.com/coder/coder/v2/coderd/workspaceusage" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/codersdk/agentsdk" | ||
"github.com/coder/coder/v2/codersdk/drpc" | ||
|
@@ -146,6 +147,7 @@ type Options struct { | |
WorkspaceAppsStatsCollectorOptions workspaceapps.StatsCollectorOptions | ||
AllowWorkspaceRenames bool | ||
NewTicker func(duration time.Duration) (<-chan time.Time, func()) | ||
WorkspaceUsageTracker *workspaceusage.Tracker | ||
} | ||
|
||
// New constructs a codersdk client connected to an in-memory API instance. | ||
|
@@ -306,6 +308,36 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can | |
hangDetector.Start() | ||
t.Cleanup(hangDetector.Close) | ||
|
||
if options.WorkspaceUsageTracker == nil { | ||
// Did last_used_at not update? Scratching your noggin? Here's why. | ||
// Workspace usage tracking must be triggered manually in tests. | ||
// The vast majority of existing tests do not depend on last_used_at | ||
// and adding an extra background goroutine to all existing tests may | ||
// lead to future flakes and goleak complaints. | ||
// To do this, pass in your own WorkspaceUsageTracker like so: | ||
// | ||
// db, ps = dbtestutil.NewDB(t) | ||
// wuTick = make(chan time.Time) | ||
// wuFlush = make(chan int, 1) | ||
// wut = workspaceusage.New(db, workspaceusage.WithFlushChannel(wuFlush), workspaceusage.WithTickChannel(wuTick)) | ||
// client = coderdtest.New(t, &coderdtest.Options{ | ||
// WorkspaceUsageTracker: wut, | ||
// Database: db, | ||
// Pubsub: ps, | ||
// }) | ||
// | ||
// See TestPortForward for how this works in practice. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lol, love the personality in this comment 😄 |
||
wutFlush := make(chan int) | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
wutTick := make(chan time.Time) | ||
options.WorkspaceUsageTracker = workspaceusage.New( | ||
options.Database, | ||
workspaceusage.WithLogger(options.Logger.Named("workspace_usage_tracker")), | ||
workspaceusage.WithFlushChannel(wutFlush), | ||
workspaceusage.WithTickChannel(wutTick), | ||
) | ||
} | ||
t.Cleanup(options.WorkspaceUsageTracker.Close) | ||
|
||
var mutex sync.RWMutex | ||
var handler http.Handler | ||
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
@@ -454,6 +486,7 @@ func NewOptions(t testing.TB, options *Options) (func(http.Handler), context.Can | |
WorkspaceAppsStatsCollectorOptions: options.WorkspaceAppsStatsCollectorOptions, | ||
AllowWorkspaceRenames: options.AllowWorkspaceRenames, | ||
NewTicker: options.NewTicker, | ||
WorkspaceUsageTracker: options.WorkspaceUsageTracker, | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wat