From 85ca35dad5a9c43f8a1766d46a00b679e88d0af6 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Tue, 21 Nov 2023 12:45:01 +0300 Subject: [PATCH] Merging to release-5.2: [TT-10518] Tests/address some racy tests (#5771) [TT-10518] Tests/address some racy tests (#5771) - Adds `unstable` build tag to tests with `!race` (so we can run them with `-race -tags=unstable`) - Fixes a race over base middleware Logger(), SetLogger() and Base() - looping tests marked as flaky or racy respectively (jira tickets in comments) https://tyktech.atlassian.net/browse/TT-10518 --------- Co-authored-by: Tit Petric --- gateway/looping_test.go | 8 +++-- gateway/middleware.go | 42 +++++++++++++++++++----- gateway/mw_organisation_activity_test.go | 4 +-- gateway/rpc_test.go | 4 +-- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/gateway/looping_test.go b/gateway/looping_test.go index 7970deeeae9..ea573350d80 100644 --- a/gateway/looping_test.go +++ b/gateway/looping_test.go @@ -1,5 +1,5 @@ -//go:build !race -// +build !race +//go:build !race || unstable +// +build !race unstable // Looping by itself has race nature package gateway @@ -185,6 +185,8 @@ func TestLooping(t *testing.T) { }) t.Run("VirtualEndpoint or plugins", func(t *testing.T) { + test.Flaky(t) // TT-10511 + ts.testPrepareVirtualEndpoint(` function testVirtData(request, session, config) { var loopLocation = "/default" @@ -326,6 +328,8 @@ func TestLooping(t *testing.T) { } func TestConcurrencyReloads(t *testing.T) { + test.Racy(t) // TT-10510 + var wg sync.WaitGroup ts := StartTest(nil) diff --git a/gateway/middleware.go b/gateway/middleware.go index 678602e054c..4a5c75f5829 100644 --- a/gateway/middleware.go +++ b/gateway/middleware.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "net/http" "strconv" + "sync" "time" "github.com/TykTechnologies/tyk/internal/cache" @@ -232,15 +233,30 @@ func (gw *Gateway) mwList(mws ...TykMiddleware) []alice.Constructor { // BaseMiddleware wraps up the ApiSpec and Proxy objects to be included in a // middleware handler, this can probably be handled better. type BaseMiddleware struct { - Spec *APISpec - Proxy ReturningHttpHandler - logger *logrus.Entry - Gw *Gateway `json:"-"` + Spec *APISpec + Proxy ReturningHttpHandler + Gw *Gateway `json:"-"` + + loggerMu sync.Mutex + logger *logrus.Entry +} + +func (t *BaseMiddleware) Base() *BaseMiddleware { + t.loggerMu.Lock() + defer t.loggerMu.Unlock() + + return &BaseMiddleware{ + Spec: t.Spec, + Proxy: t.Proxy, + Gw: t.Gw, + logger: t.logger, + } } -func (t BaseMiddleware) Base() *BaseMiddleware { return &t } +func (t *BaseMiddleware) Logger() (logger *logrus.Entry) { + t.loggerMu.Lock() + defer t.loggerMu.Unlock() -func (t BaseMiddleware) Logger() (logger *logrus.Entry) { if t.logger == nil { t.logger = logrus.NewEntry(log) } @@ -249,11 +265,21 @@ func (t BaseMiddleware) Logger() (logger *logrus.Entry) { } func (t *BaseMiddleware) SetName(name string) { - t.logger = t.Logger().WithField("mw", name) + logger := t.Logger() + + t.loggerMu.Lock() + defer t.loggerMu.Unlock() + + t.logger = logger.WithField("mw", name) } func (t *BaseMiddleware) SetRequestLogger(r *http.Request) { - t.logger = t.Gw.getLogEntryForRequest(t.Logger(), r, ctxGetAuthToken(r), nil) + logger := t.Logger() + + t.loggerMu.Lock() + defer t.loggerMu.Unlock() + + t.logger = t.Gw.getLogEntryForRequest(logger, r, ctxGetAuthToken(r), nil) } func (t BaseMiddleware) Init() {} diff --git a/gateway/mw_organisation_activity_test.go b/gateway/mw_organisation_activity_test.go index 8442495dfa5..abb9b1b0457 100644 --- a/gateway/mw_organisation_activity_test.go +++ b/gateway/mw_organisation_activity_test.go @@ -1,5 +1,5 @@ -//go:build !race -// +build !race +//go:build !race || unstable +// +build !race unstable package gateway diff --git a/gateway/rpc_test.go b/gateway/rpc_test.go index 2592659fb2f..5f6270b5613 100644 --- a/gateway/rpc_test.go +++ b/gateway/rpc_test.go @@ -1,5 +1,5 @@ -//go:build !race -// +build !race +//go:build !race || unstable +// +build !race unstable package gateway