Skip to content

Commit 0379cc5

Browse files
gabyefectnReneWerner87
authored
fix: Inconsistent and flaky unit-tests (#2892)
* Fixes for some of the failing tests * Add readiness check to serverStart() * Use net/http client for tests listen test * Use different key for this test * Run Proxy Middleware tests in parallel. Add nil checks for potential issues pointed by nilaway * Enable parallel client tests * Do not run timing sensitive tests in parallel * Remove TODO * Revert Test_Proxy_DoTimeout_Timeout, and remove t.Parallel() for it * Do not calculate favicon len on each handler call * Revert logic change * Increase timeout of SaveFile tests * Do not run time sensitive tests in parallel * The Agent can't be run in parallel * Do not run time sensitive tests in parallel * Fixes based on uber/nilaway * Revert change to Client test * Run parallel * Update client_test.go * Update client_test.go * Update cache_test.go * Update cookiejar_test.go * Remove parallel for test using timeouts * Remove t.Parallel() from logger middleware tests * Do not use testify.require in a goroutine * Fix import, and update golangci-lint * Remove changes to template_chain.go * Run more tests in parallel * Add more parallel tests * Add more parallel tests * SetLogger can't run in parallel * Run more tests in parallel, fix issue with goroutine in limiter middleware * Update internal/storage/memory, add more benchmarks * Increase sleep for csrf test by 100 milliseconds. Implement asserted and parallel benchmarks for Session middleware * Add 100 milliseconds to sleep during test * Revert name change * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests * fix: Inconsistent and flaky unit-tests --------- Co-authored-by: M. Efe Çetin <[email protected]> Co-authored-by: René <[email protected]>
1 parent 1fdef7f commit 0379cc5

32 files changed

+691
-294
lines changed

.golangci.yml

-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ output:
1313
linters-settings:
1414
testifylint:
1515
enable-all: true
16-
disable:
17-
- go-require
1816
errcheck:
1917
check-type-assertions: true
2018
check-blank: true

app_test.go

+60-31
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/gofiber/utils/v2"
2727

28+
"github.com/stretchr/testify/assert"
2829
"github.com/stretchr/testify/require"
2930
"github.com/valyala/fasthttp"
3031
"github.com/valyala/fasthttp/fasthttputil"
@@ -197,6 +198,7 @@ func (*customConstraint) Execute(param string, args ...string) bool {
197198
}
198199

199200
func Test_App_CustomConstraint(t *testing.T) {
201+
t.Parallel()
200202
app := New()
201203
app.RegisterCustomConstraint(&customConstraint{})
202204

@@ -821,20 +823,20 @@ func Test_App_ShutdownWithTimeout(t *testing.T) {
821823
time.Sleep(5 * time.Second)
822824
return c.SendString("body")
823825
})
826+
824827
ln := fasthttputil.NewInmemoryListener()
825828
go func() {
826-
require.NoError(t, app.Listener(ln))
829+
err := app.Listener(ln)
830+
assert.NoError(t, err)
827831
}()
832+
828833
time.Sleep(1 * time.Second)
829834
go func() {
830835
conn, err := ln.Dial()
831-
if err != nil {
832-
t.Errorf("unexepcted error: %v", err)
833-
}
836+
assert.NoError(t, err)
834837

835-
if _, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")); err != nil {
836-
t.Errorf("unexpected error: %v", err)
837-
}
838+
_, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"))
839+
assert.NoError(t, err)
838840
}()
839841
time.Sleep(1 * time.Second)
840842

@@ -866,20 +868,18 @@ func Test_App_ShutdownWithContext(t *testing.T) {
866868
ln := fasthttputil.NewInmemoryListener()
867869

868870
go func() {
869-
require.NoError(t, app.Listener(ln))
871+
err := app.Listener(ln)
872+
assert.NoError(t, err)
870873
}()
871874

872875
time.Sleep(1 * time.Second)
873876

874877
go func() {
875878
conn, err := ln.Dial()
876-
if err != nil {
877-
t.Errorf("unexepcted error: %v", err)
878-
}
879+
assert.NoError(t, err)
879880

880-
if _, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")); err != nil {
881-
t.Errorf("unexpected error: %v", err)
882-
}
881+
_, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n"))
882+
assert.NoError(t, err)
883883
}()
884884

885885
time.Sleep(1 * time.Second)
@@ -903,6 +903,7 @@ func Test_App_ShutdownWithContext(t *testing.T) {
903903

904904
// go test -run Test_App_Static_Index_Default
905905
func Test_App_Static_Index_Default(t *testing.T) {
906+
t.Parallel()
906907
app := New()
907908

908909
app.Static("/prefix", "./.github/workflows")
@@ -932,6 +933,7 @@ func Test_App_Static_Index_Default(t *testing.T) {
932933

933934
// go test -run Test_App_Static_Index
934935
func Test_App_Static_Direct(t *testing.T) {
936+
t.Parallel()
935937
app := New()
936938

937939
app.Static("/", "./.github")
@@ -960,6 +962,7 @@ func Test_App_Static_Direct(t *testing.T) {
960962

961963
// go test -run Test_App_Static_MaxAge
962964
func Test_App_Static_MaxAge(t *testing.T) {
965+
t.Parallel()
963966
app := New()
964967

965968
app.Static("/", "./.github", Static{MaxAge: 100})
@@ -974,6 +977,7 @@ func Test_App_Static_MaxAge(t *testing.T) {
974977

975978
// go test -run Test_App_Static_Custom_CacheControl
976979
func Test_App_Static_Custom_CacheControl(t *testing.T) {
980+
t.Parallel()
977981
app := New()
978982

979983
app.Static("/", "./.github", Static{ModifyResponse: func(c Ctx) error {
@@ -994,6 +998,7 @@ func Test_App_Static_Custom_CacheControl(t *testing.T) {
994998

995999
// go test -run Test_App_Static_Download
9961000
func Test_App_Static_Download(t *testing.T) {
1001+
t.Parallel()
9971002
app := New()
9981003

9991004
app.Static("/fiber.png", "./.github/testdata/fs/img/fiber.png", Static{Download: true})
@@ -1008,6 +1013,7 @@ func Test_App_Static_Download(t *testing.T) {
10081013

10091014
// go test -run Test_App_Static_Group
10101015
func Test_App_Static_Group(t *testing.T) {
1016+
t.Parallel()
10111017
app := New()
10121018

10131019
grp := app.Group("/v1", func(c Ctx) error {
@@ -1037,6 +1043,7 @@ func Test_App_Static_Group(t *testing.T) {
10371043
}
10381044

10391045
func Test_App_Static_Wildcard(t *testing.T) {
1046+
t.Parallel()
10401047
app := New()
10411048

10421049
app.Static("*", "./.github/index.html")
@@ -1054,6 +1061,7 @@ func Test_App_Static_Wildcard(t *testing.T) {
10541061
}
10551062

10561063
func Test_App_Static_Prefix_Wildcard(t *testing.T) {
1064+
t.Parallel()
10571065
app := New()
10581066

10591067
app.Static("/test/*", "./.github/index.html")
@@ -1079,6 +1087,7 @@ func Test_App_Static_Prefix_Wildcard(t *testing.T) {
10791087
}
10801088

10811089
func Test_App_Static_Prefix(t *testing.T) {
1090+
t.Parallel()
10821091
app := New()
10831092
app.Static("/john", "./.github")
10841093

@@ -1109,6 +1118,7 @@ func Test_App_Static_Prefix(t *testing.T) {
11091118
}
11101119

11111120
func Test_App_Static_Trailing_Slash(t *testing.T) {
1121+
t.Parallel()
11121122
app := New()
11131123
app.Static("/john", "./.github")
11141124

@@ -1155,6 +1165,7 @@ func Test_App_Static_Trailing_Slash(t *testing.T) {
11551165
}
11561166

11571167
func Test_App_Static_Next(t *testing.T) {
1168+
t.Parallel()
11581169
app := New()
11591170
app.Static("/", ".github", Static{
11601171
Next: func(c Ctx) bool {
@@ -1168,6 +1179,7 @@ func Test_App_Static_Next(t *testing.T) {
11681179
})
11691180

11701181
t.Run("app.Static is skipped: invoking Get handler", func(t *testing.T) {
1182+
t.Parallel()
11711183
req := httptest.NewRequest(MethodGet, "/", nil)
11721184
req.Header.Set("X-Custom-Header", "skip")
11731185
resp, err := app.Test(req)
@@ -1182,6 +1194,7 @@ func Test_App_Static_Next(t *testing.T) {
11821194
})
11831195

11841196
t.Run("app.Static is not skipped: serving index.html", func(t *testing.T) {
1197+
t.Parallel()
11851198
req := httptest.NewRequest(MethodGet, "/", nil)
11861199
req.Header.Set("X-Custom-Header", "don't skip")
11871200
resp, err := app.Test(req)
@@ -1198,6 +1211,7 @@ func Test_App_Static_Next(t *testing.T) {
11981211

11991212
// go test -run Test_App_Mixed_Routes_WithSameLen
12001213
func Test_App_Mixed_Routes_WithSameLen(t *testing.T) {
1214+
t.Parallel()
12011215
app := New()
12021216

12031217
// middleware
@@ -1476,6 +1490,7 @@ func (invalidView) Render(io.Writer, string, any, ...string) error { panic("impl
14761490

14771491
// go test -run Test_App_Init_Error_View
14781492
func Test_App_Init_Error_View(t *testing.T) {
1493+
t.Parallel()
14791494
app := New(Config{Views: invalidView{}})
14801495

14811496
defer func() {
@@ -1542,23 +1557,23 @@ func Test_App_ReadTimeout(t *testing.T) {
15421557
time.Sleep(500 * time.Millisecond)
15431558

15441559
conn, err := net.Dial(NetworkTCP4, "127.0.0.1:4004")
1545-
require.NoError(t, err)
1560+
assert.NoError(t, err)
15461561
defer func(conn net.Conn) {
15471562
err := conn.Close()
1548-
require.NoError(t, err)
1563+
assert.NoError(t, err)
15491564
}(conn)
15501565

15511566
_, err = conn.Write([]byte("HEAD /read-timeout HTTP/1.1\r\n"))
1552-
require.NoError(t, err)
1567+
assert.NoError(t, err)
15531568

15541569
buf := make([]byte, 1024)
15551570
var n int
15561571
n, err = conn.Read(buf)
15571572

1558-
require.NoError(t, err)
1559-
require.True(t, bytes.Contains(buf[:n], []byte("408 Request Timeout")))
1573+
assert.NoError(t, err)
1574+
assert.True(t, bytes.Contains(buf[:n], []byte("408 Request Timeout")))
15601575

1561-
require.NoError(t, app.Shutdown())
1576+
assert.NoError(t, app.Shutdown())
15621577
}()
15631578

15641579
require.NoError(t, app.Listen(":4004", ListenConfig{DisableStartupMessage: true}))
@@ -1576,23 +1591,22 @@ func Test_App_BadRequest(t *testing.T) {
15761591
go func() {
15771592
time.Sleep(500 * time.Millisecond)
15781593
conn, err := net.Dial(NetworkTCP4, "127.0.0.1:4005")
1579-
require.NoError(t, err)
1594+
assert.NoError(t, err)
15801595
defer func(conn net.Conn) {
15811596
err := conn.Close()
1582-
require.NoError(t, err)
1597+
assert.NoError(t, err)
15831598
}(conn)
15841599

15851600
_, err = conn.Write([]byte("BadRequest\r\n"))
1586-
require.NoError(t, err)
1601+
assert.NoError(t, err)
15871602

15881603
buf := make([]byte, 1024)
15891604
var n int
15901605
n, err = conn.Read(buf)
1591-
require.NoError(t, err)
1592-
1593-
require.True(t, bytes.Contains(buf[:n], []byte("400 Bad Request")))
15941606

1595-
require.NoError(t, app.Shutdown())
1607+
assert.NoError(t, err)
1608+
assert.True(t, bytes.Contains(buf[:n], []byte("400 Bad Request")))
1609+
assert.NoError(t, app.Shutdown())
15961610
}()
15971611

15981612
require.NoError(t, app.Listen(":4005", ListenConfig{DisableStartupMessage: true}))
@@ -1612,12 +1626,12 @@ func Test_App_SmallReadBuffer(t *testing.T) {
16121626
go func() {
16131627
time.Sleep(500 * time.Millisecond)
16141628
req, err := http.NewRequestWithContext(context.Background(), MethodGet, "http://127.0.0.1:4006/small-read-buffer", nil)
1615-
require.NoError(t, err)
1629+
assert.NoError(t, err)
16161630
var client http.Client
16171631
resp, err := client.Do(req)
1618-
require.NoError(t, err)
1619-
require.Equal(t, 431, resp.StatusCode)
1620-
require.NoError(t, app.Shutdown())
1632+
assert.NoError(t, err)
1633+
assert.Equal(t, 431, resp.StatusCode)
1634+
assert.NoError(t, app.Shutdown())
16211635
}()
16221636

16231637
require.NoError(t, app.Listen(":4006", ListenConfig{DisableStartupMessage: true}))
@@ -1755,6 +1769,19 @@ func Test_App_Test_no_timeout_infinitely(t *testing.T) {
17551769
}
17561770
}
17571771

1772+
func Test_App_Test_timeout(t *testing.T) {
1773+
t.Parallel()
1774+
1775+
app := New()
1776+
app.Get("/", func(_ Ctx) error {
1777+
time.Sleep(1 * time.Second)
1778+
return nil
1779+
})
1780+
1781+
_, err := app.Test(httptest.NewRequest(MethodGet, "/", nil), 100*time.Millisecond)
1782+
require.Equal(t, errors.New("test: timeout error after 100ms"), err)
1783+
}
1784+
17581785
func Test_App_SetTLSHandler(t *testing.T) {
17591786
t.Parallel()
17601787
tlsHandler := &TLSHandler{clientHelloInfo: &tls.ClientHelloInfo{
@@ -1815,6 +1842,7 @@ func TestApp_GetRoutes(t *testing.T) {
18151842
}
18161843

18171844
func Test_Middleware_Route_Naming_With_Use(t *testing.T) {
1845+
t.Parallel()
18181846
named := "named"
18191847
app := New()
18201848

@@ -1874,6 +1902,7 @@ func Test_Middleware_Route_Naming_With_Use(t *testing.T) {
18741902
}
18751903

18761904
func Test_Route_Naming_Issue_2671_2685(t *testing.T) {
1905+
t.Parallel()
18771906
app := New()
18781907

18791908
app.Get("/", emptyHandler).Name("index")

client/client_test.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/gofiber/fiber/v3/addon/retry"
1717
"github.com/gofiber/fiber/v3/internal/tlstest"
1818
"github.com/gofiber/utils/v2"
19+
"github.com/stretchr/testify/assert"
1920
"github.com/stretchr/testify/require"
2021
"github.com/valyala/bytebufferpool"
2122
)
@@ -277,8 +278,8 @@ func Test_Client_ConcurrencyRequests(t *testing.T) {
277278
go func(m string) {
278279
defer wg.Done()
279280
resp, err := client.Custom("http://example.com", m)
280-
require.NoError(t, err)
281-
require.Equal(t, "example.com "+m, utils.UnsafeString(resp.RawResponse.Body()))
281+
assert.NoError(t, err)
282+
assert.Equal(t, "example.com "+m, utils.UnsafeString(resp.RawResponse.Body()))
282283
}(method)
283284
}
284285
}
@@ -1258,10 +1259,11 @@ func Test_Client_TLS(t *testing.T) {
12581259
})
12591260

12601261
go func() {
1261-
require.NoError(t, app.Listener(ln, fiber.ListenConfig{
1262+
assert.NoError(t, app.Listener(ln, fiber.ListenConfig{
12621263
DisableStartupMessage: true,
12631264
}))
12641265
}()
1266+
time.Sleep(1 * time.Second)
12651267

12661268
client := New()
12671269
resp, err := client.SetTLSConfig(clientTLSConf).Get("https://" + ln.Addr().String())
@@ -1291,10 +1293,11 @@ func Test_Client_TLS_Error(t *testing.T) {
12911293
})
12921294

12931295
go func() {
1294-
require.NoError(t, app.Listener(ln, fiber.ListenConfig{
1296+
assert.NoError(t, app.Listener(ln, fiber.ListenConfig{
12951297
DisableStartupMessage: true,
12961298
}))
12971299
}()
1300+
time.Sleep(1 * time.Second)
12981301

12991302
client := New()
13001303
resp, err := client.SetTLSConfig(clientTLSConf).Get("https://" + ln.Addr().String())
@@ -1321,10 +1324,11 @@ func Test_Client_TLS_Empty_TLSConfig(t *testing.T) {
13211324
})
13221325

13231326
go func() {
1324-
require.NoError(t, app.Listener(ln, fiber.ListenConfig{
1327+
assert.NoError(t, app.Listener(ln, fiber.ListenConfig{
13251328
DisableStartupMessage: true,
13261329
}))
13271330
}()
1331+
time.Sleep(1 * time.Second)
13281332

13291333
client := New()
13301334
resp, err := client.Get("https://" + ln.Addr().String())
@@ -1579,18 +1583,17 @@ func Test_Client_SetProxyURL(t *testing.T) {
15791583

15801584
func Test_Client_SetRetryConfig(t *testing.T) {
15811585
t.Parallel()
1582-
15831586
retryConfig := &retry.Config{
15841587
InitialInterval: 1 * time.Second,
15851588
MaxRetryCount: 3,
15861589
}
15871590

15881591
core, client, req := newCore(), New(), AcquireRequest()
1589-
req.SetURL("http://example.com")
1592+
req.SetURL("http://exampleretry.com")
15901593
client.SetRetryConfig(retryConfig)
15911594
_, err := core.execute(context.Background(), client, req)
15921595

1593-
require.NoError(t, err)
1596+
require.Error(t, err)
15941597
require.Equal(t, retryConfig.InitialInterval, client.RetryConfig().InitialInterval)
15951598
require.Equal(t, retryConfig.MaxRetryCount, client.RetryConfig().MaxRetryCount)
15961599
}

0 commit comments

Comments
 (0)