fix: resolve race conditions and update golangci-lint config
- Fix TestCertificateHandler_Delete race condition: - Add WAL mode and busy_timeout to SQLite connection - Add sleep to allow background sync goroutine to complete - Fix TestNotificationService_SendExternal_EdgeCases race condition: - Use atomic.Value for cross-goroutine string access - Update .golangci.yml for version 2: - Add version field - Move linters-settings under linters.settings - Remove deprecated typecheck and gosimple linters - Update govet shadow check syntax
This commit is contained in:
35
backend/.golangci.yml
Normal file
35
backend/.golangci.yml
Normal file
@@ -0,0 +1,35 @@
|
||||
version: "2"
|
||||
|
||||
run:
|
||||
timeout: 5m
|
||||
tests: true
|
||||
|
||||
linters:
|
||||
enable:
|
||||
- bodyclose
|
||||
- gocritic
|
||||
- gosec
|
||||
- govet
|
||||
- ineffassign
|
||||
- staticcheck
|
||||
- unused
|
||||
- revive
|
||||
|
||||
settings:
|
||||
gocritic:
|
||||
enabled-tags:
|
||||
- diagnostic
|
||||
- experimental
|
||||
- opinionated
|
||||
- performance
|
||||
- style
|
||||
disabled-checks:
|
||||
- whyNoLint
|
||||
- wrapperFunc
|
||||
govet:
|
||||
enable:
|
||||
- shadow
|
||||
revive:
|
||||
rules:
|
||||
- name: exported
|
||||
severity: warning
|
||||
@@ -133,7 +133,8 @@ func TestCertificateHandler_Upload(t *testing.T) {
|
||||
func TestCertificateHandler_Delete(t *testing.T) {
|
||||
// Setup
|
||||
tmpDir := t.TempDir()
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||
// Use WAL mode and busy timeout for better concurrency with race detector
|
||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared&_journal_mode=WAL&_busy_timeout=5000"), &gorm.Config{})
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, db.AutoMigrate(&models.SSLCertificate{}))
|
||||
|
||||
@@ -147,6 +148,8 @@ func TestCertificateHandler_Delete(t *testing.T) {
|
||||
require.NotZero(t, cert.ID)
|
||||
|
||||
service := services.NewCertificateService(tmpDir, db)
|
||||
// Allow background sync goroutine to complete before testing
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
ns := services.NewNotificationService(db)
|
||||
handler := NewCertificateHandler(service, ns)
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -498,12 +499,13 @@ func TestNotificationService_SendExternal_EdgeCases(t *testing.T) {
|
||||
db := setupNotificationTestDB(t)
|
||||
svc := NewNotificationService(db)
|
||||
|
||||
receivedCustom := ""
|
||||
var receivedCustom atomic.Value
|
||||
receivedCustom.Store("")
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var body map[string]interface{}
|
||||
json.NewDecoder(r.Body).Decode(&body)
|
||||
if custom, ok := body["custom"]; ok {
|
||||
receivedCustom = custom.(string)
|
||||
receivedCustom.Store(custom.(string))
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
@@ -525,7 +527,7 @@ func TestNotificationService_SendExternal_EdgeCases(t *testing.T) {
|
||||
svc.SendExternal("proxy_host", "Title", "Message", customData)
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
assert.Equal(t, "test-value", receivedCustom)
|
||||
assert.Equal(t, "test-value", receivedCustom.Load().(string))
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user