refactor: modernize code using go fix

This commit is contained in:
2026-02-19 17:31:06 +01:00
parent ac6e1ba80b
commit 986b4e9388
7 changed files with 34 additions and 44 deletions

View File

@@ -422,9 +422,7 @@ func TestE2E_ConcurrentPostCreation(t *testing.T) {
for _, user := range users { for _, user := range users {
u := user u := user
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
client, err := ctx.loginUserSafe(t, u.Username, u.Password) client, err := ctx.loginUserSafe(t, u.Username, u.Password)
if err != nil || client == nil { if err != nil || client == nil {
results <- nil results <- nil
@@ -443,7 +441,7 @@ func TestE2E_ConcurrentPostCreation(t *testing.T) {
post, err := client.CreatePostSafe("Concurrent Post", url, "Content") post, err := client.CreatePostSafe("Concurrent Post", url, "Content")
results <- post results <- post
}() })
} }
wg.Wait() wg.Wait()

View File

@@ -56,10 +56,8 @@ func TestE2E_DatabaseFailureRecovery(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
errors := make(chan error, 10) errors := make(chan error, 10)
for i := 0; i < 5; i++ { for range 5 {
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
conn, err := sqlDB.Conn(context.Background()) conn, err := sqlDB.Conn(context.Background())
if err != nil { if err != nil {
errors <- err errors <- err
@@ -68,7 +66,7 @@ func TestE2E_DatabaseFailureRecovery(t *testing.T) {
defer conn.Close() defer conn.Close()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
}() })
} }
wg.Wait() wg.Wait()
@@ -297,7 +295,7 @@ func TestE2E_DatabaseConnectionPool(t *testing.T) {
initialStats := sqlDB.Stats() initialStats := sqlDB.Stats()
for i := 0; i < 5; i++ { for range 5 {
req, err := http.NewRequest("GET", ctx.baseURL+"/api/posts", nil) req, err := http.NewRequest("GET", ctx.baseURL+"/api/posts", nil)
if err != nil { if err != nil {
continue continue

View File

@@ -59,7 +59,7 @@ func TestE2E_Performance(t *testing.T) {
var totalTime time.Duration var totalTime time.Duration
iterations := 10 iterations := 10
for i := 0; i < iterations; i++ { for range iterations {
req, err := endpoint.req() req, err := endpoint.req()
if err != nil { if err != nil {
t.Fatalf("Failed to create request: %v", err) t.Fatalf("Failed to create request: %v", err)
@@ -98,11 +98,9 @@ func TestE2E_Performance(t *testing.T) {
var errorCount int64 var errorCount int64
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < concurrency; i++ { for range concurrency {
wg.Add(1) wg.Go(func() {
go func() { for range requestsPerGoroutine {
defer wg.Done()
for j := 0; j < requestsPerGoroutine; j++ {
req, err := http.NewRequest("GET", ctx.baseURL+"/api/posts", nil) req, err := http.NewRequest("GET", ctx.baseURL+"/api/posts", nil)
if err != nil { if err != nil {
atomic.AddInt64(&errorCount, 1) atomic.AddInt64(&errorCount, 1)
@@ -123,7 +121,7 @@ func TestE2E_Performance(t *testing.T) {
atomic.AddInt64(&errorCount, 1) atomic.AddInt64(&errorCount, 1)
} }
} }
}() })
} }
wg.Wait() wg.Wait()
@@ -138,7 +136,7 @@ func TestE2E_Performance(t *testing.T) {
createdUser := ctx.createUserWithCleanup(t, "dbperf", "StrongPass123!") createdUser := ctx.createUserWithCleanup(t, "dbperf", "StrongPass123!")
authClient := ctx.loginUser(t, createdUser.Username, createdUser.Password) authClient := ctx.loginUser(t, createdUser.Username, createdUser.Password)
for i := 0; i < 10; i++ { for i := range 10 {
authClient.CreatePost(t, fmt.Sprintf("Post %d", i), fmt.Sprintf("https://example.com/%d", i), "Content") authClient.CreatePost(t, fmt.Sprintf("Post %d", i), fmt.Sprintf("https://example.com/%d", i), "Content")
} }
@@ -160,7 +158,7 @@ func TestE2E_Performance(t *testing.T) {
authClient := ctx.loginUser(t, createdUser.Username, createdUser.Password) authClient := ctx.loginUser(t, createdUser.Username, createdUser.Password)
initialPosts := 50 initialPosts := 50
for i := 0; i < initialPosts; i++ { for i := range initialPosts {
authClient.CreatePost(t, fmt.Sprintf("Memory Test Post %d", i), fmt.Sprintf("https://example.com/mem%d", i), "Content") authClient.CreatePost(t, fmt.Sprintf("Memory Test Post %d", i), fmt.Sprintf("https://example.com/mem%d", i), "Content")
} }
@@ -271,16 +269,14 @@ func TestE2E_ConcurrentWrites(t *testing.T) {
for _, user := range users { for _, user := range users {
u := user u := user
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
authClient, err := ctx.loginUserSafe(t, u.Username, u.Password) authClient, err := ctx.loginUserSafe(t, u.Username, u.Password)
if err != nil { if err != nil {
atomic.AddInt64(&errorCount, 1) atomic.AddInt64(&errorCount, 1)
return return
} }
for i := 0; i < 5; i++ { for i := range 5 {
post, err := authClient.CreatePostSafe( post, err := authClient.CreatePostSafe(
fmt.Sprintf("Concurrent Post %d", i), fmt.Sprintf("Concurrent Post %d", i),
fmt.Sprintf("https://example.com/concurrent%d-%d", u.ID, i), fmt.Sprintf("https://example.com/concurrent%d-%d", u.ID, i),
@@ -292,7 +288,7 @@ func TestE2E_ConcurrentWrites(t *testing.T) {
atomic.AddInt64(&errorCount, 1) atomic.AddInt64(&errorCount, 1)
} }
} }
}() })
} }
wg.Wait() wg.Wait()
@@ -311,7 +307,7 @@ func TestE2E_ResponseSize(t *testing.T) {
createdUser := ctx.createUserWithCleanup(t, "sizetest", "StrongPass123!") createdUser := ctx.createUserWithCleanup(t, "sizetest", "StrongPass123!")
authClient := ctx.loginUser(t, createdUser.Username, createdUser.Password) authClient := ctx.loginUser(t, createdUser.Username, createdUser.Password)
for i := 0; i < 100; i++ { for i := range 100 {
authClient.CreatePost(t, fmt.Sprintf("Post %d", i), fmt.Sprintf("https://example.com/%d", i), "Content") authClient.CreatePost(t, fmt.Sprintf("Post %d", i), fmt.Sprintf("https://example.com/%d", i), "Content")
} }

View File

@@ -38,7 +38,7 @@ func TestE2E_RateLimitingHeaders(t *testing.T) {
t.Error("Expected Retry-After header when rate limited") t.Error("Expected Retry-After header when rate limited")
} }
var jsonResponse map[string]interface{} var jsonResponse map[string]any
body, _ := json.Marshal(map[string]string{}) body, _ := json.Marshal(map[string]string{})
_ = json.Unmarshal(body, &jsonResponse) _ = json.Unmarshal(body, &jsonResponse)
@@ -72,7 +72,7 @@ func TestE2E_RateLimitingHeaders(t *testing.T) {
if resp.StatusCode != http.StatusTooManyRequests { if resp.StatusCode != http.StatusTooManyRequests {
t.Errorf("Expected status 429 on request %d, got %d", i+1, resp.StatusCode) t.Errorf("Expected status 429 on request %d, got %d", i+1, resp.StatusCode)
} else { } else {
var errorResponse map[string]interface{} var errorResponse map[string]any
body, _ := io.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
if err := json.Unmarshal(body, &errorResponse); err == nil { if err := json.Unmarshal(body, &errorResponse); err == nil {
if errorResponse["error"] == nil { if errorResponse["error"] == nil {

View File

@@ -30,12 +30,12 @@ func TestE2E_VersionEndpoint(t *testing.T) {
return return
} }
var apiInfo map[string]interface{} var apiInfo map[string]any
if err := json.NewDecoder(resp.Body).Decode(&apiInfo); err != nil { if err := json.NewDecoder(resp.Body).Decode(&apiInfo); err != nil {
t.Fatalf("Failed to decode API info response: %v", err) t.Fatalf("Failed to decode API info response: %v", err)
} }
data, ok := apiInfo["data"].(map[string]interface{}) data, ok := apiInfo["data"].(map[string]any)
if !ok { if !ok {
t.Fatalf("API info data is not a map") t.Fatalf("API info data is not a map")
} }
@@ -74,12 +74,12 @@ func TestE2E_VersionEndpoint(t *testing.T) {
return return
} }
var healthInfo map[string]interface{} var healthInfo map[string]any
if err := json.NewDecoder(resp.Body).Decode(&healthInfo); err != nil { if err := json.NewDecoder(resp.Body).Decode(&healthInfo); err != nil {
t.Fatalf("Failed to decode health response: %v", err) t.Fatalf("Failed to decode health response: %v", err)
} }
data, ok := healthInfo["data"].(map[string]interface{}) data, ok := healthInfo["data"].(map[string]any)
if !ok { if !ok {
t.Fatalf("Health data is not a map") t.Fatalf("Health data is not a map")
} }
@@ -130,18 +130,18 @@ func TestE2E_VersionEndpoint(t *testing.T) {
return return
} }
var apiInfo map[string]interface{} var apiInfo map[string]any
if err := json.NewDecoder(apiResp.Body).Decode(&apiInfo); err != nil { if err := json.NewDecoder(apiResp.Body).Decode(&apiInfo); err != nil {
t.Fatalf("Failed to decode API info: %v", err) t.Fatalf("Failed to decode API info: %v", err)
} }
var healthInfo map[string]interface{} var healthInfo map[string]any
if err := json.NewDecoder(healthResp.Body).Decode(&healthInfo); err != nil { if err := json.NewDecoder(healthResp.Body).Decode(&healthInfo); err != nil {
t.Fatalf("Failed to decode health info: %v", err) t.Fatalf("Failed to decode health info: %v", err)
} }
apiData, _ := apiInfo["data"].(map[string]interface{}) apiData, _ := apiInfo["data"].(map[string]any)
healthData, _ := healthInfo["data"].(map[string]interface{}) healthData, _ := healthInfo["data"].(map[string]any)
apiVersion, apiOk := apiData["version"].(string) apiVersion, apiOk := apiData["version"].(string)
healthVersion, healthOk := healthData["version"].(string) healthVersion, healthOk := healthData["version"].(string)
@@ -169,12 +169,12 @@ func TestE2E_VersionEndpoint(t *testing.T) {
return return
} }
var apiInfo map[string]interface{} var apiInfo map[string]any
if err := json.NewDecoder(resp.Body).Decode(&apiInfo); err != nil { if err := json.NewDecoder(resp.Body).Decode(&apiInfo); err != nil {
t.Fatalf("Failed to decode API info: %v", err) t.Fatalf("Failed to decode API info: %v", err)
} }
data, ok := apiInfo["data"].(map[string]interface{}) data, ok := apiInfo["data"].(map[string]any)
if !ok { if !ok {
return return
} }

View File

@@ -455,7 +455,7 @@ func TestE2E_ConcurrentRequestsWithSameSession(t *testing.T) {
authClient := ctx.loginUser(t, createdUser.Username, createdUser.Password) authClient := ctx.loginUser(t, createdUser.Username, createdUser.Password)
results := make(chan bool, 5) results := make(chan bool, 5)
for i := 0; i < 5; i++ { for range 5 {
go func() { go func() {
profile := authClient.GetProfile(t) profile := authClient.GetProfile(t)
results <- (profile != nil && profile.Data.Username == createdUser.Username) results <- (profile != nil && profile.Data.Username == createdUser.Username)
@@ -463,7 +463,7 @@ func TestE2E_ConcurrentRequestsWithSameSession(t *testing.T) {
} }
successCount := 0 successCount := 0
for i := 0; i < 5; i++ { for range 5 {
if <-results { if <-results {
successCount++ successCount++
} }
@@ -562,7 +562,7 @@ func TestE2E_RapidSuccessiveActions(t *testing.T) {
post := authClient.CreatePost(t, "Rapid Vote Test", "https://example.com/rapid", "Content") post := authClient.CreatePost(t, "Rapid Vote Test", "https://example.com/rapid", "Content")
for i := 0; i < 10; i++ { for i := range 10 {
voteType := "up" voteType := "up"
if i%2 == 0 { if i%2 == 0 {
voteType = "down" voteType = "down"

View File

@@ -134,9 +134,7 @@ func TestE2E_ConcurrentUserWorkflows(t *testing.T) {
for _, user := range users { for _, user := range users {
u := user u := user
wg.Add(1) wg.Go(func() {
go func() {
defer wg.Done()
var err error var err error
authClient, loginErr := ctx.loginUserSafe(t, u.Username, u.Password) authClient, loginErr := ctx.loginUserSafe(t, u.Username, u.Password)
if loginErr != nil || authClient == nil || authClient.Token == "" { if loginErr != nil || authClient == nil || authClient.Token == "" {
@@ -157,7 +155,7 @@ func TestE2E_ConcurrentUserWorkflows(t *testing.T) {
case results <- result{userID: u.ID, err: err}: case results <- result{userID: u.ID, err: err}:
case <-done: case <-done:
} }
}() })
} }
go func() { go func() {