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:
@@ -50,13 +50,13 @@ func TestRequireAPIKeyOrWorkspaceProxyAuth(t *testing.T) {
|
||||
)
|
||||
r.Header.Set(codersdk.SessionTokenHeader, token)
|
||||
|
||||
var called int64
|
||||
var called atomic.Int64
|
||||
httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
|
||||
DB: db,
|
||||
RedirectToLogin: false,
|
||||
})(
|
||||
httpmw.RequireAPIKeyOrWorkspaceProxyAuth()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt64(&called, 1)
|
||||
called.Add(1)
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
}))).
|
||||
ServeHTTP(rw, r)
|
||||
@@ -68,7 +68,7 @@ func TestRequireAPIKeyOrWorkspaceProxyAuth(t *testing.T) {
|
||||
t.Log(string(dump))
|
||||
|
||||
require.Equal(t, http.StatusOK, rw.Code)
|
||||
require.Equal(t, int64(1), atomic.LoadInt64(&called))
|
||||
require.Equal(t, int64(1), called.Load())
|
||||
})
|
||||
|
||||
t.Run("WorkspaceProxy", func(t *testing.T) {
|
||||
@@ -122,12 +122,12 @@ func TestRequireAPIKeyOrWorkspaceProxyAuth(t *testing.T) {
|
||||
)
|
||||
r.Header.Set(httpmw.WorkspaceProxyAuthTokenHeader, fmt.Sprintf("%s:%s", proxy.ID, token))
|
||||
|
||||
var called int64
|
||||
var called atomic.Int64
|
||||
httpmw.ExtractWorkspaceProxy(httpmw.ExtractWorkspaceProxyConfig{
|
||||
DB: db,
|
||||
})(
|
||||
httpmw.RequireAPIKeyOrWorkspaceProxyAuth()(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt64(&called, 1)
|
||||
called.Add(1)
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
}))).
|
||||
ServeHTTP(rw, r)
|
||||
@@ -139,6 +139,6 @@ func TestRequireAPIKeyOrWorkspaceProxyAuth(t *testing.T) {
|
||||
t.Log(string(dump))
|
||||
|
||||
require.Equal(t, http.StatusOK, rw.Code)
|
||||
require.Equal(t, int64(1), atomic.LoadInt64(&called))
|
||||
require.Equal(t, int64(1), called.Load())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -802,9 +802,9 @@ func TestAPIKey(t *testing.T) {
|
||||
r = httptest.NewRequest("GET", "/", nil)
|
||||
rw = httptest.NewRecorder()
|
||||
|
||||
count int64
|
||||
count atomic.Int64
|
||||
handler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||
atomic.AddInt64(&count, 1)
|
||||
count.Add(1)
|
||||
|
||||
apiKey, ok := httpmw.APIKeyOptional(r)
|
||||
assert.False(t, ok)
|
||||
@@ -823,7 +823,7 @@ func TestAPIKey(t *testing.T) {
|
||||
res := rw.Result()
|
||||
defer res.Body.Close()
|
||||
require.Equal(t, http.StatusOK, res.StatusCode)
|
||||
require.EqualValues(t, 1, atomic.LoadInt64(&count))
|
||||
require.EqualValues(t, 1, count.Load())
|
||||
})
|
||||
|
||||
t.Run("Tokens", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user