fix: resolve test race conditions and update golangci-lint action
- Fix race condition in gateway tests by using TestMain to set Gin mode once - Remove duplicate gin.SetMode(gin.TestMode) calls from individual tests - Add TestMain function to initialize test environment before all tests - Prevents race conditions when tests run in parallel with -race flag - Update golangci-lint-action from v6 to v7 - v6 doesn't support golangci-lint v2.x versions - v7 supports golangci-lint v2.x and automatically selects compatible version - Change version from v2.6.0 to latest for automatic compatibility All tests now pass with race detector enabled.
This commit is contained in:
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@@ -79,9 +79,9 @@ jobs:
|
|||||||
go-version: '1.25.3'
|
go-version: '1.25.3'
|
||||||
|
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v6
|
uses: golangci/golangci-lint-action@v7
|
||||||
with:
|
with:
|
||||||
version: v2.6.0
|
version: latest
|
||||||
args: --timeout=5m
|
args: --timeout=5m
|
||||||
|
|
||||||
build:
|
build:
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -18,11 +19,17 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TestMain sets up the test environment before running tests.
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
// Set Gin to test mode once for all tests to avoid race conditions
|
||||||
|
gin.SetMode(gin.TestMode)
|
||||||
|
// Run tests
|
||||||
|
os.Exit(m.Run())
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewGateway(t *testing.T) {
|
func TestNewGateway(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
gin.SetMode(gin.TestMode)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
cfg config.ConfigProvider
|
cfg config.ConfigProvider
|
||||||
@@ -70,8 +77,6 @@ func TestNewGateway(t *testing.T) {
|
|||||||
func TestGateway_SetupRoutes(t *testing.T) {
|
func TestGateway_SetupRoutes(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
gin.SetMode(gin.TestMode)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
routes []RouteConfig
|
routes []RouteConfig
|
||||||
@@ -127,8 +132,6 @@ func TestGateway_SetupRoutes(t *testing.T) {
|
|||||||
func TestGateway_handleRoute(t *testing.T) {
|
func TestGateway_handleRoute(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
gin.SetMode(gin.TestMode)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
route RouteConfig
|
route RouteConfig
|
||||||
@@ -199,8 +202,6 @@ func TestGateway_handleRoute(t *testing.T) {
|
|||||||
func TestGateway_handleRoute_AllMethods(t *testing.T) {
|
func TestGateway_handleRoute_AllMethods(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
gin.SetMode(gin.TestMode)
|
|
||||||
|
|
||||||
methods := []string{
|
methods := []string{
|
||||||
http.MethodGet,
|
http.MethodGet,
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
@@ -271,8 +272,6 @@ func TestLoadRoutes(t *testing.T) {
|
|||||||
func TestGateway_NoRoute(t *testing.T) {
|
func TestGateway_NoRoute(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
gin.SetMode(gin.TestMode)
|
|
||||||
|
|
||||||
gateway := &Gateway{
|
gateway := &Gateway{
|
||||||
config: &mockConfigProvider{},
|
config: &mockConfigProvider{},
|
||||||
log: &mockLogger{},
|
log: &mockLogger{},
|
||||||
|
|||||||
Reference in New Issue
Block a user