mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +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:
@@ -32,9 +32,9 @@ func Test_ProxyServer_Headers(t *testing.T) {
|
||||
// We're not going to actually start a proxy, we're going to point it
|
||||
// towards a fake server that returns an unexpected status code. This'll
|
||||
// cause the proxy to exit with an error that we can check for.
|
||||
var called int64
|
||||
var called atomic.Int64
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt64(&called, 1)
|
||||
called.Add(1)
|
||||
assert.Equal(t, headerVal1, r.Header.Get(headerName1))
|
||||
assert.Equal(t, headerVal2, r.Header.Get(headerName2))
|
||||
|
||||
@@ -57,7 +57,7 @@ func Test_ProxyServer_Headers(t *testing.T) {
|
||||
require.ErrorContains(t, err, "unexpected status code 418")
|
||||
require.NoError(t, pty.Close())
|
||||
|
||||
assert.EqualValues(t, 1, atomic.LoadInt64(&called))
|
||||
assert.EqualValues(t, 1, called.Load())
|
||||
}
|
||||
|
||||
//nolint:paralleltest,tparallel // Test uses a static port.
|
||||
|
||||
@@ -39,9 +39,9 @@ func Test_IssueSignedAppTokenHTML(t *testing.T) {
|
||||
expectedSessionToken = "user-session-token"
|
||||
expectedSignedTokenStr = "signed-app-token"
|
||||
)
|
||||
var called int64
|
||||
var called atomic.Int64
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt64(&called, 1)
|
||||
called.Add(1)
|
||||
|
||||
assert.Equal(t, r.Method, http.MethodPost)
|
||||
assert.Equal(t, r.URL.Path, "/api/v2/workspaceproxies/me/issue-signed-app-token")
|
||||
@@ -87,7 +87,7 @@ func Test_IssueSignedAppTokenHTML(t *testing.T) {
|
||||
require.Equal(t, expectedSignedTokenStr, tokenRes.SignedTokenStr)
|
||||
require.False(t, rw.WasWritten())
|
||||
|
||||
require.EqualValues(t, called, 1)
|
||||
require.EqualValues(t, called.Load(), 1)
|
||||
})
|
||||
|
||||
t.Run("Error", func(t *testing.T) {
|
||||
@@ -98,9 +98,9 @@ func Test_IssueSignedAppTokenHTML(t *testing.T) {
|
||||
expectedResponseStatus = http.StatusBadRequest
|
||||
expectedResponseBody = "bad request"
|
||||
)
|
||||
var called int64
|
||||
var called atomic.Int64
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt64(&called, 1)
|
||||
called.Add(1)
|
||||
|
||||
assert.Equal(t, r.Method, http.MethodPost)
|
||||
assert.Equal(t, r.URL.Path, "/api/v2/workspaceproxies/me/issue-signed-app-token")
|
||||
@@ -132,7 +132,7 @@ func Test_IssueSignedAppTokenHTML(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expectedResponseBody, string(body))
|
||||
|
||||
require.EqualValues(t, called, 1)
|
||||
require.EqualValues(t, called.Load(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user