mirror of
https://github.com/foomo/keel.git
synced 2025-10-16 12:35:34 +00:00
feat: enable paralleltest linter
This commit is contained in:
parent
86418ec317
commit
ed5880a22d
@ -17,10 +17,9 @@ linters:
|
||||
- inamedparam # reports interfaces with unnamed method parameters [fast: true, auto-fix: false]
|
||||
- loggercheck # (logrlint) Checks key value pairs for common logger libraries (kitlog,klog,logr,zap). [fast: false, auto-fix: false]
|
||||
- nakedret # Checks that functions with naked returns are not longer than a maximum size (can be zero). [fast: true, auto-fix: false]
|
||||
- paralleltest # Detects missing usage of t.Parallel() method in your Go test [fast: false, auto-fix: false]
|
||||
## Deprected linters
|
||||
# Deprected linters
|
||||
- wsl
|
||||
## Discouraged linters
|
||||
# Discouraged linters
|
||||
- noinlineerr # Disallows inline error handling (`if err := ...; err != nil {`).
|
||||
- embeddedstructfieldcheck # Embedded types should be at the top of the field list of a struct, and there must be an empty line separating embedded fields from regular fields. [fast]
|
||||
- cyclop # checks function and package cyclomatic complexity [fast: false, auto-fix: false]
|
||||
|
||||
@ -24,18 +24,24 @@ func ExampleNewWrappedError() {
|
||||
}
|
||||
|
||||
func TestNewWrappedError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
parentErr := errors.New("parent")
|
||||
childErr := errors.New("child")
|
||||
assert.Error(t, keelerrors.NewWrappedError(parentErr, childErr))
|
||||
}
|
||||
|
||||
func TestWrapped(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
parentErr := errors.New("parent")
|
||||
childErr := errors.New("child")
|
||||
assert.Error(t, keelerrors.NewWrappedError(parentErr, childErr))
|
||||
}
|
||||
|
||||
func Test_wrappedError_As(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type (
|
||||
Parent struct {
|
||||
error
|
||||
@ -63,6 +69,8 @@ func Test_wrappedError_As(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_wrappedError_Error(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
parentErr := errors.New("parent")
|
||||
childErr := errors.New("child")
|
||||
wrappedErr := keelerrors.NewWrappedError(parentErr, childErr)
|
||||
@ -70,6 +78,8 @@ func Test_wrappedError_Error(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_wrappedError_Is(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
parentErr := errors.New("parent")
|
||||
childErr := errors.New("child")
|
||||
wrappedErr := keelerrors.NewWrappedError(parentErr, childErr)
|
||||
|
||||
@ -8,6 +8,8 @@ import (
|
||||
)
|
||||
|
||||
func TestEqualInline(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
when func(t *testing.T) bool
|
||||
@ -33,12 +35,15 @@ func TestEqualInline(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert.True(t, tt.when(t))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEqualInlineJSONEq(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
when func(t *testing.T) bool
|
||||
@ -58,6 +63,7 @@ func TestEqualInlineJSONEq(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert.True(t, tt.when(t))
|
||||
})
|
||||
}
|
||||
|
||||
@ -8,25 +8,34 @@ import (
|
||||
)
|
||||
|
||||
func TestInline(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("read inline", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
value, ok := keeltest.Inline(t, 1) // INLINE: hello world
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "hello world", value)
|
||||
})
|
||||
|
||||
t.Run("read inline int", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
value, ok := keeltest.InlineInt(t, 1) // INLINE: 1
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, 1, value)
|
||||
})
|
||||
|
||||
t.Run("read inline float", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
value, ok := keeltest.InlineFloat64(t, 1) // INLINE: 1.5
|
||||
assert.True(t, ok)
|
||||
assert.InDelta(t, 1.5, value, 0)
|
||||
})
|
||||
|
||||
t.Run("read inline json", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var x struct {
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
@ -35,6 +44,8 @@ func TestInline(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("write inline", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
value, ok := keeltest.Inline(t, 1, "hello %s", "world") // INLINE: hello world
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "hello world", value)
|
||||
|
||||
@ -12,6 +12,8 @@ import (
|
||||
)
|
||||
|
||||
func TestCORS(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
target string
|
||||
@ -99,6 +101,8 @@ func TestCORS(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
handler := tt.with(zaptest.NewLogger(t), tt.name, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
@ -21,6 +21,8 @@ const (
|
||||
)
|
||||
|
||||
func TestGzip(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svr := keeltest.NewServer()
|
||||
|
||||
// get logger
|
||||
@ -53,6 +55,8 @@ func TestGzip(t *testing.T) {
|
||||
|
||||
// send payload < 1024
|
||||
t.Run("<1024", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
payload = gzipPayload1023
|
||||
body, err := gzipString(payload)
|
||||
require.NoError(t, err)
|
||||
@ -77,7 +81,7 @@ func TestGzip(t *testing.T) {
|
||||
})
|
||||
|
||||
// send payload > 1024
|
||||
t.Run(">=1024", func(t *testing.T) {
|
||||
t.Run(">=1024", func(t *testing.T) { //nolint:paralleltest
|
||||
payload = gzipPayload1024
|
||||
body, err := gzipString(payload)
|
||||
require.NoError(t, err)
|
||||
@ -106,6 +110,8 @@ func TestGzip(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGZipBadRequest(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svr := keeltest.NewServer()
|
||||
|
||||
// get logger
|
||||
|
||||
@ -47,6 +47,8 @@ var cbSettings = &roundtripware.CircuitBreakerSettings{
|
||||
}
|
||||
|
||||
func TestCircuitBreaker(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// create logger
|
||||
l := zaptest.NewLogger(t)
|
||||
|
||||
@ -138,6 +140,8 @@ func TestCircuitBreaker(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCircuitBreakerCopyBodies(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
requestData := "some request"
|
||||
responseData := "some response"
|
||||
|
||||
@ -194,6 +198,8 @@ func TestCircuitBreakerCopyBodies(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCircuitBreakerReadFromNotCopiedBodies(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
requestData := "some request"
|
||||
responseData := "some response"
|
||||
|
||||
@ -274,6 +280,8 @@ func TestCircuitBreakerReadFromNotCopiedBodies(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCircuitBreakerInterval(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// create logger
|
||||
l := zaptest.NewLogger(t)
|
||||
|
||||
@ -352,6 +360,8 @@ func TestCircuitBreakerInterval(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCircuitBreakerIgnore(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// create logger
|
||||
l := zaptest.NewLogger(t)
|
||||
|
||||
@ -401,6 +411,8 @@ func TestCircuitBreakerIgnore(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCircuitBreakerTimeout(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// create logger
|
||||
l := zaptest.NewLogger(t)
|
||||
|
||||
|
||||
@ -28,6 +28,7 @@ func DumpRequest() RoundTripware {
|
||||
span := trace.SpanFromContext(r.Context())
|
||||
span.AddEvent("DumpRequest")
|
||||
dumpRequest(r)
|
||||
|
||||
return next(r)
|
||||
}
|
||||
}
|
||||
@ -39,6 +40,7 @@ func DumpResponse() RoundTripware {
|
||||
return func(r *http.Request) (*http.Response, error) {
|
||||
span := trace.SpanFromContext(r.Context())
|
||||
span.AddEvent("DumpResponse")
|
||||
|
||||
resp, err := next(r)
|
||||
dumpResponse(r, resp)
|
||||
|
||||
|
||||
@ -24,6 +24,8 @@ const (
|
||||
)
|
||||
|
||||
func TestGZip(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
payload string
|
||||
@ -46,6 +48,8 @@ func TestGZip(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// create logger
|
||||
l := zaptest.NewLogger(t)
|
||||
|
||||
@ -63,6 +67,7 @@ func TestGZip(t *testing.T) {
|
||||
// validate request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
assert.NoError(t, err)
|
||||
|
||||
defer r.Body.Close()
|
||||
|
||||
if tt.expectRequestCompressed {
|
||||
@ -98,10 +103,12 @@ func TestGZip(t *testing.T) {
|
||||
// do request
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
// validate response header
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
if tt.expectResponseCompressed {
|
||||
assert.Equal(t, stdhttp.EncodingGzip.String(), resp.Header.Get(stdhttp.HeaderContentEncoding.String()))
|
||||
} else {
|
||||
|
||||
@ -17,6 +17,8 @@ import (
|
||||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
loggerOpts []roundtripware.LoggerOption
|
||||
@ -83,10 +85,13 @@ func TestLogger(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// create logger & validate output
|
||||
l := zaptest.NewLogger(t, zaptest.WrapOptions(zap.Hooks(func(entry zapcore.Entry) error {
|
||||
assert.Equal(t, tt.expectedLogLevel, entry.Level)
|
||||
assert.Equal(t, tt.expectedLogMessage, entry.Message)
|
||||
|
||||
return nil
|
||||
})))
|
||||
|
||||
@ -105,6 +110,7 @@ func TestLogger(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
middlewares = append(middlewares, roundtripware.Logger(tt.loggerOpts...))
|
||||
|
||||
// create http client
|
||||
@ -125,6 +131,7 @@ func TestLogger(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NotNil(t, resp)
|
||||
defer resp.Body.Close()
|
||||
}
|
||||
|
||||
@ -16,6 +16,8 @@ import (
|
||||
)
|
||||
|
||||
func TestReferer(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
contextReferer string
|
||||
@ -31,6 +33,7 @@ func TestReferer(t *testing.T) {
|
||||
refererOptions: nil,
|
||||
setupContext: nil,
|
||||
serverAssertion: func(t *testing.T, r *http.Request, expectedHeader string) {
|
||||
t.Helper()
|
||||
assert.Empty(t, r.Header.Get(expectedHeader))
|
||||
},
|
||||
},
|
||||
@ -39,10 +42,9 @@ func TestReferer(t *testing.T) {
|
||||
contextReferer: "https://foomo.org/",
|
||||
headerToCheck: keelhttp.HeaderXReferer,
|
||||
refererOptions: nil,
|
||||
setupContext: func(ctx context.Context, referer string) context.Context {
|
||||
return keelhttpcontext.SetReferer(ctx, referer)
|
||||
},
|
||||
setupContext: keelhttpcontext.SetReferer,
|
||||
serverAssertion: func(t *testing.T, r *http.Request, expectedHeader string) {
|
||||
t.Helper()
|
||||
assert.Equal(t, "https://foomo.org/", r.Header.Get(expectedHeader))
|
||||
},
|
||||
},
|
||||
@ -53,10 +55,9 @@ func TestReferer(t *testing.T) {
|
||||
refererOptions: []roundtripware.RefererOption{
|
||||
roundtripware.RefererWithHeader("X-Custom-Header"),
|
||||
},
|
||||
setupContext: func(ctx context.Context, referer string) context.Context {
|
||||
return keelhttpcontext.SetReferer(ctx, referer)
|
||||
},
|
||||
setupContext: keelhttpcontext.SetReferer,
|
||||
serverAssertion: func(t *testing.T, r *http.Request, expectedHeader string) {
|
||||
t.Helper()
|
||||
assert.Equal(t, "https://foomo.org/", r.Header.Get(expectedHeader))
|
||||
},
|
||||
},
|
||||
@ -64,6 +65,8 @@ func TestReferer(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// create logger
|
||||
l := zaptest.NewLogger(t)
|
||||
|
||||
@ -94,6 +97,7 @@ func TestReferer(t *testing.T) {
|
||||
// do request
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
// validate
|
||||
|
||||
@ -16,6 +16,8 @@ import (
|
||||
)
|
||||
|
||||
func TestRequestID(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
setupContext func(t *testing.T) context.Context
|
||||
@ -28,37 +30,46 @@ func TestRequestID(t *testing.T) {
|
||||
{
|
||||
name: "default behavior",
|
||||
setupContext: func(t *testing.T) context.Context {
|
||||
t.Helper()
|
||||
return t.Context()
|
||||
},
|
||||
requestIDOptions: nil,
|
||||
expectedHeader: keelhttp.HeaderXRequestID,
|
||||
serverAssertions: func(t *testing.T, r *http.Request, capturedID *string) {
|
||||
t.Helper()
|
||||
|
||||
*capturedID = r.Header.Get(keelhttp.HeaderXRequestID)
|
||||
assert.NotEmpty(t, *capturedID)
|
||||
},
|
||||
requestAssertions: func(t *testing.T, req *http.Request, capturedID string) {
|
||||
t.Helper()
|
||||
assert.Equal(t, capturedID, req.Header.Get(keelhttp.HeaderXRequestID))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with context",
|
||||
setupContext: func(t *testing.T) context.Context {
|
||||
t.Helper()
|
||||
return keelhttpcontext.SetRequestID(t.Context(), "123456")
|
||||
},
|
||||
requestIDOptions: nil,
|
||||
expectedHeader: keelhttp.HeaderXRequestID,
|
||||
expectedRequestID: "123456",
|
||||
serverAssertions: func(t *testing.T, r *http.Request, capturedID *string) {
|
||||
t.Helper()
|
||||
|
||||
*capturedID = r.Header.Get(keelhttp.HeaderXRequestID)
|
||||
assert.Equal(t, "123456", *capturedID)
|
||||
},
|
||||
requestAssertions: func(t *testing.T, req *http.Request, capturedID string) {
|
||||
t.Helper()
|
||||
assert.Equal(t, "123456", req.Header.Get(keelhttp.HeaderXRequestID))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with custom provider",
|
||||
setupContext: func(t *testing.T) context.Context {
|
||||
t.Helper()
|
||||
return t.Context()
|
||||
},
|
||||
requestIDOptions: []roundtripware.RequestIDOption{
|
||||
@ -69,16 +80,20 @@ func TestRequestID(t *testing.T) {
|
||||
expectedHeader: keelhttp.HeaderXRequestID,
|
||||
expectedRequestID: "123456",
|
||||
serverAssertions: func(t *testing.T, r *http.Request, capturedID *string) {
|
||||
t.Helper()
|
||||
|
||||
*capturedID = r.Header.Get(keelhttp.HeaderXRequestID)
|
||||
assert.Equal(t, "123456", *capturedID)
|
||||
},
|
||||
requestAssertions: func(t *testing.T, req *http.Request, capturedID string) {
|
||||
t.Helper()
|
||||
assert.Equal(t, "123456", req.Header.Get(keelhttp.HeaderXRequestID))
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "with custom header",
|
||||
setupContext: func(t *testing.T) context.Context {
|
||||
t.Helper()
|
||||
return t.Context()
|
||||
},
|
||||
requestIDOptions: []roundtripware.RequestIDOption{
|
||||
@ -86,10 +101,13 @@ func TestRequestID(t *testing.T) {
|
||||
},
|
||||
expectedHeader: "X-Custom-Header",
|
||||
serverAssertions: func(t *testing.T, r *http.Request, capturedID *string) {
|
||||
t.Helper()
|
||||
|
||||
*capturedID = r.Header.Get("X-Custom-Header")
|
||||
assert.NotEmpty(t, *capturedID)
|
||||
},
|
||||
requestAssertions: func(t *testing.T, req *http.Request, capturedID string) {
|
||||
t.Helper()
|
||||
assert.Equal(t, capturedID, req.Header.Get("X-Custom-Header"))
|
||||
},
|
||||
},
|
||||
@ -121,6 +139,7 @@ func TestRequestID(t *testing.T) {
|
||||
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
tt.requestAssertions(t, req, capturedRequestID)
|
||||
|
||||
@ -103,7 +103,9 @@ func Retry(opts ...RetryOption) RoundTripware {
|
||||
|
||||
err := retry.Do(func() error {
|
||||
attempt++
|
||||
|
||||
var err error
|
||||
|
||||
span.SetAttributes(semconv.HTTPRetryCountKey.Int(attempt))
|
||||
|
||||
resp, err = next(req) //nolint:bodyclose
|
||||
|
||||
@ -53,6 +53,7 @@ func SessionID(opts ...SessionIDOption) RoundTripware {
|
||||
r.Header.Set(o.Header, value)
|
||||
}
|
||||
}
|
||||
|
||||
return next(r)
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ import (
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
t.Parallel()
|
||||
testingx.Tags(t, tagx.Skip)
|
||||
|
||||
l := zaptest.NewLogger(t)
|
||||
|
||||
@ -253,5 +253,6 @@ func (s *KeelTestSuite) httpPut(url, data string) (int, string, error) {
|
||||
// In order for 'go test' to run this suite, we need to create
|
||||
// a normal test function and pass our suite to suite.Run
|
||||
func TestKeelTestSuite(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, new(KeelTestSuite))
|
||||
}
|
||||
|
||||
@ -8,6 +8,8 @@ import (
|
||||
)
|
||||
|
||||
func TestMeter(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
m := telemetry.Meter()
|
||||
assert.NotNil(t, m)
|
||||
}
|
||||
|
||||
@ -9,6 +9,8 @@ import (
|
||||
)
|
||||
|
||||
func TestNewResource(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
result, err := telemetry.NewResource(t.Context())
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, result)
|
||||
|
||||
@ -14,7 +14,7 @@ func Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption)
|
||||
}
|
||||
|
||||
func Span(ctx context.Context, handler func(ctx context.Context, span trace.Span) error) (err error) { //nolint:nonamedreturns
|
||||
name := "unkownn"
|
||||
name := "unknown"
|
||||
|
||||
pc, _, _, ok := runtime.Caller(1)
|
||||
if ok {
|
||||
|
||||
@ -9,6 +9,8 @@ import (
|
||||
)
|
||||
|
||||
func TestNewNoopTraceProvider(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tp := telemetry.NewNoopTraceProvider()
|
||||
assert.IsType(t, noop.TracerProvider{}, tp)
|
||||
}
|
||||
|
||||
@ -8,6 +8,8 @@ import (
|
||||
)
|
||||
|
||||
func TestTracer(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
m := telemetry.Tracer()
|
||||
assert.NotNil(t, m)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user