Repository navigation
fix(cli)!: protect client Logger and refactor cli scaletest tests - #8317
Conversation
| t.Skipf("This test is flakey. See https://github.com/coder/coder/issues/4942") | ||
| t.Parallel() | ||
|
|
||
| // This test does a create-workspaces scale test with --no-cleanup, checks |
There was a problem hiding this comment.
review: already tested in scaletest/createworkspaces/run_test.go. No point duplicating.
| // Logger is optionally provided to log requests. | ||
| // logger is optionally provided to log requests. | ||
| // Method, URL, and response code will be logged by default. | ||
| Logger slog.Logger | ||
| logger slog.Logger | ||
|
|
||
| // LogBodies can be enabled to print request and response bodies to the logger. | ||
| LogBodies bool | ||
| // logBodies can be enabled to print request and response bodies to the logger. | ||
| logBodies bool |
There was a problem hiding this comment.
review: these fields were being concurrently accessed in a number of places. Protecting them with a mutex for the moment. This treatment could theoretically be extended to other fields, but some fields (like URL) are very frequently accessed, so such a change has a large blast radius.
There was a problem hiding this comment.
This treatment could theoretically be extended to other fields, but some fields (like URL) are very frequently accessed, so such a change has a large blast radius.
I think the URL can do without a mutex, it's set in New and shouldn't change after the fact. We probably should pass logger in New as well since it's a bit weird that it changes later.
PS. I wouldn't be too worried about a mutex and frequent field access, the client isn't performance critical and the delays seem negligible to me.
| // Since the runner creates the workspace on it's own, we have to keep | ||
| // listing workspaces until we find it, then wait for the build to | ||
| // finish, then start the agents. | ||
| go func() { | ||
| var workspace codersdk.Workspace | ||
| for { | ||
| res, err := client.Workspaces(ctx, codersdk.WorkspaceFilter{}) | ||
| if !assert.NoError(t, err) { | ||
| return | ||
| } | ||
| workspaces := res.Workspaces | ||
|
|
||
| if len(workspaces) == 1 { | ||
| workspace = workspaces[0] | ||
| break | ||
| } | ||
|
|
||
| time.Sleep(100 * time.Millisecond) | ||
| } | ||
|
|
||
| coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID) | ||
|
|
||
| agentClient := agentsdk.New(client.URL) | ||
| agentClient.SetSessionToken(authToken) | ||
| agentCloser := agent.New(agent.Options{ | ||
| Client: agentClient, | ||
| Logger: slogtest.Make(t, nil).Named("agent").Leveled(slog.LevelWarn), | ||
| }) | ||
| t.Cleanup(func() { | ||
| _ = agentCloser.Close() | ||
| }) | ||
|
|
||
| coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID) | ||
| }() | ||
| go eventuallyStartFakeAgent(t, ctx, client, authToken) |
There was a problem hiding this comment.
review: extracted to test helper
| require.Len(t, workspaces.Workspaces, 0) | ||
| }) | ||
|
|
||
| t.Run("NoCleanup", func(t *testing.T) { |
There was a problem hiding this comment.
review: this is mostly copy-pasta that only serves to validate that the NoCleanup option is respected.
| //go:build race | ||
|
|
||
| package testutil | ||
|
|
||
| const ( | ||
| RaceEnabled = true | ||
| ) |
There was a problem hiding this comment.
Review: this may be controversial, but conditionally skipping tests that we know are flaky and slow under the race detector is preferable to just leaving these tests languishing in a perma-skipped state.
|
I didn't review yet, but
I think PR might be better tagged as |
I'll update with the |
| // Logger is optionally provided to log requests. | ||
| // logger is optionally provided to log requests. | ||
| // Method, URL, and response code will be logged by default. | ||
| Logger slog.Logger | ||
| logger slog.Logger | ||
|
|
||
| // LogBodies can be enabled to print request and response bodies to the logger. | ||
| LogBodies bool | ||
| // logBodies can be enabled to print request and response bodies to the logger. | ||
| logBodies bool |
There was a problem hiding this comment.
This treatment could theoretically be extended to other fields, but some fields (like URL) are very frequently accessed, so such a change has a large blast radius.
I think the URL can do without a mutex, it's set in New and shouldn't change after the fact. We probably should pass logger in New as well since it's a bit weird that it changes later.
PS. I wouldn't be too worried about a mutex and frequent field access, the client isn't performance critical and the delays seem negligible to me.
mtojek
left a comment
There was a problem hiding this comment.
I'd rather set logger and logBoddies once during object creation, but I guess that this is beyond the scope of this PR.
|
|
||
| // Logger returns the logger for the client. | ||
| func (c *Client) Logger() slog.Logger { | ||
| c.mu.RLock() |
There was a problem hiding this comment.
I would add a comment to mu sync.RWMutex, which describes what exactly you want to guard. I have a feeling that one-day future somebody will try to remove it and misses its point.
Agreed, I'd honestly push for all the fields of |
To be fair, Then again, we could use functional options instead, the aforementioned option is useful on a per-request basis. Essentially |
This attempts to address the test flakes reported in #4942 -- sadly, the runner logs have expired and are no longer available so I've done a best-effort fix here.
scaletest/createworkspaces.testutil.RaceEnabledbool const and conditionaly skips previously-skipped tests under scaletest/ if the race detector is enabled. This is unfortunate and sad, but I would prefer to have these tests at least running without the race detector than not running at all.