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 -5
View File
@@ -264,14 +264,12 @@ func setupRunnerTest(t *testing.T) (client *codersdk.Client, agentID uuid.UUID)
func testServer(t *testing.T) (string, func() int64) {
t.Helper()
var count int64
var count atomic.Int64
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&count, 1)
count.Add(1)
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(srv.Close)
return srv.URL, func() int64 {
return atomic.LoadInt64(&count)
}
return srv.URL, count.Load
}
+12 -12
View File
@@ -58,21 +58,21 @@ func Test_TestRun(t *testing.T) {
var (
name, id = "test", "1"
runCalled int64
cleanupCalled int64
collectableCalled int64
runCalled atomic.Int64
cleanupCalled atomic.Int64
collectableCalled atomic.Int64
testFns = testFns{
RunFn: func(ctx context.Context, id string, logs io.Writer) error {
atomic.AddInt64(&runCalled, 1)
runCalled.Add(1)
return nil
},
CleanupFn: func(ctx context.Context, id string, logs io.Writer) error {
atomic.AddInt64(&cleanupCalled, 1)
cleanupCalled.Add(1)
return nil
},
GetMetricsFn: func() map[string]any {
atomic.AddInt64(&collectableCalled, 1)
collectableCalled.Add(1)
return nil
},
}
@@ -83,12 +83,12 @@ func Test_TestRun(t *testing.T) {
err := run.Run(context.Background())
require.NoError(t, err)
require.EqualValues(t, 1, atomic.LoadInt64(&runCalled))
require.EqualValues(t, 1, atomic.LoadInt64(&collectableCalled))
require.EqualValues(t, 1, runCalled.Load())
require.EqualValues(t, 1, collectableCalled.Load())
err = run.Cleanup(context.Background())
require.NoError(t, err)
require.EqualValues(t, 1, atomic.LoadInt64(&cleanupCalled))
require.EqualValues(t, 1, cleanupCalled.Load())
})
t.Run("Cleanup", func(t *testing.T) {
@@ -111,20 +111,20 @@ func Test_TestRun(t *testing.T) {
t.Run("NotDone", func(t *testing.T) {
t.Parallel()
var cleanupCalled int64
var cleanupCalled atomic.Int64
run := harness.NewTestRun("test", "1", testFns{
RunFn: func(ctx context.Context, id string, logs io.Writer) error {
return nil
},
CleanupFn: func(ctx context.Context, id string, logs io.Writer) error {
atomic.AddInt64(&cleanupCalled, 1)
cleanupCalled.Add(1)
return nil
},
})
err := run.Cleanup(context.Background())
require.NoError(t, err)
require.EqualValues(t, 0, atomic.LoadInt64(&cleanupCalled))
require.EqualValues(t, 0, cleanupCalled.Load())
})
})
+6 -5
View File
@@ -19,12 +19,13 @@ import (
//nolint:paralleltest // this tests uses timings to determine if it's working
func Test_LinearExecutionStrategy(t *testing.T) {
var (
lastSeenI int64 = -1
count int64
lastSeenI atomic.Int64
count atomic.Int64
)
lastSeenI.Store(-1)
runs, fns := strategyTestData(100, func(_ context.Context, i int, _ io.Writer) error {
atomic.AddInt64(&count, 1)
swapped := atomic.CompareAndSwapInt64(&lastSeenI, int64(i-1), int64(i))
count.Add(1)
swapped := lastSeenI.CompareAndSwap(int64(i-1), int64(i))
assert.True(t, swapped)
time.Sleep(2 * time.Millisecond)
@@ -38,7 +39,7 @@ func Test_LinearExecutionStrategy(t *testing.T) {
runErrs, err := strategy.Run(context.Background(), fns)
require.NoError(t, err)
require.Len(t, runErrs, 50)
require.EqualValues(t, 100, atomic.LoadInt64(&count))
require.EqualValues(t, 100, count.Load())
lastStartTime := time.Time{}
for _, run := range runs {