refactor: go fix ftw

This commit is contained in:
2026-02-19 17:37:42 +01:00
parent 9185ffa6b5
commit 85882bae14
21 changed files with 82 additions and 98 deletions

View File

@@ -283,16 +283,16 @@ func TestInMemoryCacheConcurrent(t *testing.T) {
t.Run("Concurrent writes", func(t *testing.T) {
done := make(chan bool, numGoroutines)
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
go func(id int) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Goroutine %d panicked: %v", id, r)
}
}()
for j := 0; j < numOps; j++ {
for j := range numOps {
entry := &CacheEntry{
Data: []byte(fmt.Sprintf("data-%d-%d", id, j)),
Data: fmt.Appendf(nil, "data-%d-%d", id, j),
Headers: make(http.Header),
Timestamp: time.Now(),
TTL: 5 * time.Minute,
@@ -306,16 +306,16 @@ func TestInMemoryCacheConcurrent(t *testing.T) {
}(i)
}
for i := 0; i < numGoroutines; i++ {
for range numGoroutines {
<-done
}
})
t.Run("Concurrent reads and writes", func(t *testing.T) {
for i := 0; i < 10; i++ {
for i := range 10 {
entry := &CacheEntry{
Data: []byte(fmt.Sprintf("data-%d", i)),
Data: fmt.Appendf(nil, "data-%d", i),
Headers: make(http.Header),
Timestamp: time.Now(),
TTL: 5 * time.Minute,
@@ -325,16 +325,16 @@ func TestInMemoryCacheConcurrent(t *testing.T) {
done := make(chan bool, numGoroutines*2)
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
go func(id int) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Writer goroutine %d panicked: %v", id, r)
}
}()
for j := 0; j < numOps; j++ {
for j := range numOps {
entry := &CacheEntry{
Data: []byte(fmt.Sprintf("write-%d-%d", id, j)),
Data: fmt.Appendf(nil, "write-%d-%d", id, j),
Headers: make(http.Header),
Timestamp: time.Now(),
TTL: 5 * time.Minute,
@@ -346,14 +346,14 @@ func TestInMemoryCacheConcurrent(t *testing.T) {
}(i)
}
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
go func(id int) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Reader goroutine %d panicked: %v", id, r)
}
}()
for j := 0; j < numOps; j++ {
for j := range numOps {
key := fmt.Sprintf("key-%d", j%10)
cache.Get(key)
}
@@ -368,9 +368,9 @@ func TestInMemoryCacheConcurrent(t *testing.T) {
t.Run("Concurrent deletes", func(t *testing.T) {
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
entry := &CacheEntry{
Data: []byte(fmt.Sprintf("data-%d", i)),
Data: fmt.Appendf(nil, "data-%d", i),
Headers: make(http.Header),
Timestamp: time.Now(),
TTL: 5 * time.Minute,
@@ -379,7 +379,7 @@ func TestInMemoryCacheConcurrent(t *testing.T) {
}
done := make(chan bool, numGoroutines)
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
go func(id int) {
defer func() {
if r := recover(); r != nil {
@@ -391,7 +391,7 @@ func TestInMemoryCacheConcurrent(t *testing.T) {
}(i)
}
for i := 0; i < numGoroutines; i++ {
for range numGoroutines {
<-done
}
})

View File

@@ -318,7 +318,7 @@ func TestConcurrentAccess(t *testing.T) {
collector := NewMetricsCollector(monitor)
done := make(chan bool, 10)
for i := 0; i < 10; i++ {
for range 10 {
go func() {
monitor.LogQuery("SELECT * FROM users", 50*time.Millisecond, nil)
collector.RecordRequest(100*time.Millisecond, false)
@@ -326,7 +326,7 @@ func TestConcurrentAccess(t *testing.T) {
}()
}
for i := 0; i < 10; i++ {
for range 10 {
<-done
}
@@ -384,7 +384,7 @@ func TestThreadSafety(t *testing.T) {
numGoroutines := 100
done := make(chan bool, numGoroutines)
for i := 0; i < numGoroutines; i++ {
for i := range numGoroutines {
go func(id int) {
if id%2 == 0 {
@@ -398,7 +398,7 @@ func TestThreadSafety(t *testing.T) {
}(i)
}
for i := 0; i < numGoroutines; i++ {
for range numGoroutines {
<-done
}

View File

@@ -388,7 +388,7 @@ func TestRateLimiterMaxKeys(t *testing.T) {
limiter := NewRateLimiterWithConfig(1*time.Minute, 10, 5, 1*time.Minute, 2*time.Minute)
defer limiter.StopCleanup()
for i := 0; i < 5; i++ {
for i := range 5 {
key := fmt.Sprintf("key-%d", i)
if !limiter.Allow(key) {
t.Errorf("Key %s should be allowed", key)
@@ -435,7 +435,7 @@ func TestRateLimiterRegistry(t *testing.T) {
request := httptest.NewRequest("GET", "/test", nil)
request.RemoteAddr = "127.0.0.1:12345"
for i := 0; i < 50; i++ {
for i := range 50 {
recorder := httptest.NewRecorder()
server1.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
@@ -443,7 +443,7 @@ func TestRateLimiterRegistry(t *testing.T) {
}
}
for i := 0; i < 50; i++ {
for i := range 50 {
recorder2 := httptest.NewRecorder()
server2.ServeHTTP(recorder2, request)
if recorder2.Code != http.StatusOK {
@@ -463,7 +463,7 @@ func TestRateLimiterRegistry(t *testing.T) {
t.Error("101st request to server2 should be rejected (shared limiter reached limit)")
}
for i := 0; i < 50; i++ {
for i := range 50 {
recorder3 := httptest.NewRecorder()
server3.ServeHTTP(recorder3, request)
if recorder3.Code != http.StatusOK {

View File

@@ -458,13 +458,13 @@ func TestIsRapidRequest(t *testing.T) {
ip := "192.168.1.1"
for i := 0; i < 50; i++ {
for i := range 50 {
if isRapidRequest(ip) {
t.Errorf("Request %d should not be considered rapid", i+1)
}
}
for i := 0; i < 110; i++ {
for i := range 110 {
result := isRapidRequest(ip)
if i < 50 {
if result {

View File

@@ -39,7 +39,7 @@ func TestValidationMiddleware(t *testing.T) {
body, _ := json.Marshal(user)
request := httptest.NewRequest("POST", "/users", bytes.NewBuffer(body))
request.Header.Set("Content-Type", "application/json")
ctx := context.WithValue(request.Context(), DTOTypeKey, reflect.TypeOf(TestUser{}))
ctx := context.WithValue(request.Context(), DTOTypeKey, reflect.TypeFor[TestUser]())
request = request.WithContext(ctx)
recorder := httptest.NewRecorder()
@@ -70,7 +70,7 @@ func TestValidationMiddleware(t *testing.T) {
body, _ := json.Marshal(user)
request := httptest.NewRequest("POST", "/users", bytes.NewBuffer(body))
request.Header.Set("Content-Type", "application/json")
ctx := context.WithValue(request.Context(), DTOTypeKey, reflect.TypeOf(TestUser{}))
ctx := context.WithValue(request.Context(), DTOTypeKey, reflect.TypeFor[TestUser]())
request = request.WithContext(ctx)
recorder := httptest.NewRecorder()
@@ -117,7 +117,7 @@ func TestValidationMiddleware(t *testing.T) {
body, _ := json.Marshal(user)
request := httptest.NewRequest("POST", "/users", bytes.NewBuffer(body))
request.Header.Set("Content-Type", "application/json")
ctx := context.WithValue(request.Context(), DTOTypeKey, reflect.TypeOf(TestUser{}))
ctx := context.WithValue(request.Context(), DTOTypeKey, reflect.TypeFor[TestUser]())
request = request.WithContext(ctx)
recorder := httptest.NewRecorder()
@@ -144,7 +144,7 @@ func TestValidationMiddleware(t *testing.T) {
body, _ := json.Marshal(user)
request := httptest.NewRequest("POST", "/users", bytes.NewBuffer(body))
request.Header.Set("Content-Type", "application/json")
ctx := context.WithValue(request.Context(), DTOTypeKey, reflect.TypeOf(TestUser{}))
ctx := context.WithValue(request.Context(), DTOTypeKey, reflect.TypeFor[TestUser]())
request = request.WithContext(ctx)
recorder := httptest.NewRecorder()