test: use typed atomics in test files (#25071)

Use typed atomics (atomic.Int64, atomic.Int32, etc.) in test files to prevent
mixing atomic and non-atomic access on the same value, guarantee 64-bit
alignment on 32-bit platforms, and provide a cleaner API.
This commit is contained in:
Zach
2026-05-11 08:41:17 -06:00
committed by GitHub
parent a1dbd758bc
commit 81e2be69e9
21 changed files with 115 additions and 116 deletions
+3 -3
View File
@@ -536,7 +536,7 @@ func TestAgent(t *testing.T) {
t.Run("NotInfinite", func(t *testing.T) {
t.Parallel()
var fetchCalled uint64
var fetchCalled atomic.Uint64
cmd := &serpent.Command{
Handler: func(inv *serpent.Invocation) error {
@@ -544,7 +544,7 @@ func TestAgent(t *testing.T) {
err := cliui.Agent(inv.Context(), &buf, uuid.Nil, cliui.AgentOptions{
FetchInterval: 10 * time.Millisecond,
Fetch: func(ctx context.Context, agentID uuid.UUID) (codersdk.WorkspaceAgent, error) {
atomic.AddUint64(&fetchCalled, 1)
fetchCalled.Add(1)
return codersdk.WorkspaceAgent{
Status: codersdk.WorkspaceAgentConnected,
@@ -557,7 +557,7 @@ func TestAgent(t *testing.T) {
}
require.Never(t, func() bool {
called := atomic.LoadUint64(&fetchCalled)
called := fetchCalled.Load()
return called > 5 || called == 0
}, time.Second, 100*time.Millisecond)
+5 -5
View File
@@ -80,7 +80,7 @@ func Test_OutputFormatter(t *testing.T) {
t.Run("OK", func(t *testing.T) {
t.Parallel()
var called int64
var called atomic.Int64
f := cliui.NewOutputFormatter(
cliui.JSONFormat(),
&format{
@@ -95,7 +95,7 @@ func Test_OutputFormatter(t *testing.T) {
})
},
formatFn: func(_ context.Context, _ any) (string, error) {
atomic.AddInt64(&called, 1)
called.Add(1)
return "foo", nil
},
},
@@ -121,18 +121,18 @@ func Test_OutputFormatter(t *testing.T) {
var got []string
require.NoError(t, json.Unmarshal([]byte(out), &got))
require.Equal(t, data, got)
require.EqualValues(t, 0, atomic.LoadInt64(&called))
require.EqualValues(t, 0, called.Load())
require.NoError(t, fs.Set("output", "foo"))
out, err = f.Format(ctx, data)
require.NoError(t, err)
require.Equal(t, "foo", out)
require.EqualValues(t, 1, atomic.LoadInt64(&called))
require.EqualValues(t, 1, called.Load())
require.Error(t, fs.Set("output", "bar"))
out, err = f.Format(ctx, data)
require.NoError(t, err)
require.Equal(t, "foo", out)
require.EqualValues(t, 2, atomic.LoadInt64(&called))
require.EqualValues(t, 2, called.Load())
})
}