mirror of
https://github.com/coder/coder.git
synced 2026-09-21 20:51:01 +08:00
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:
@@ -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)
|
||||
|
||||
|
||||
@@ -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())
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user