Skip to content

Commit

Permalink
test(limiter): fix intermittent failures (gofiber#2716)
Browse files Browse the repository at this point in the history
The limiter middleware unit tests are failing due to a race between the
storage garbage collector and the unit test itself. The sliding window
limiter tracks requests using memory storage. In several of the unit
tests, this storage expiry ends up being 4 seconds. The test waits for 4
seconds, then sends a request, expecting it to succeed. However, the
unit test occasionally wakes up before the storage GC kicks in. As an
effect of the very coarse timer (using seconds as units), the middleware
correctly rejects the request, causing the test to fail.

Update the sleep to 4.5 seconds. This will not slow down the execution
of the test suite, as these tests run in parallel with a separate 9
second long test.

I'm not 100% sure this solves the issue, and ideally we'd be able to
run tests without time.Sleep.
  • Loading branch information
nickajacks1 committed Nov 10, 2023
1 parent 5d888ce commit 1e55045
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions middleware/limiter/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func Test_Limiter_Sliding_Window_No_Skip_Choices(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 429, resp.StatusCode)

time.Sleep(4 * time.Second)
time.Sleep(4*time.Second + 500*time.Millisecond)

resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/success", nil))
utils.AssertEqual(t, nil, err)
Expand Down Expand Up @@ -258,7 +258,7 @@ func Test_Limiter_Sliding_Window_Custom_Storage_No_Skip_Choices(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 429, resp.StatusCode)

time.Sleep(4 * time.Second)
time.Sleep(4*time.Second + 500*time.Millisecond)

resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/success", nil))
utils.AssertEqual(t, nil, err)
Expand Down Expand Up @@ -373,7 +373,7 @@ func Test_Limiter_Sliding_Window_Skip_Failed_Requests(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 429, resp.StatusCode)

time.Sleep(4 * time.Second)
time.Sleep(4*time.Second + 500*time.Millisecond)

resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/success", nil))
utils.AssertEqual(t, nil, err)
Expand Down Expand Up @@ -412,7 +412,7 @@ func Test_Limiter_Sliding_Window_Custom_Storage_Skip_Failed_Requests(t *testing.
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 429, resp.StatusCode)

time.Sleep(4 * time.Second)
time.Sleep(4*time.Second + 500*time.Millisecond)

resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/success", nil))
utils.AssertEqual(t, nil, err)
Expand Down Expand Up @@ -533,7 +533,7 @@ func Test_Limiter_Sliding_Window_Skip_Successful_Requests(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 429, resp.StatusCode)

time.Sleep(4 * time.Second)
time.Sleep(4*time.Second + 500*time.Millisecond)

resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/fail", nil))
utils.AssertEqual(t, nil, err)
Expand Down Expand Up @@ -574,7 +574,7 @@ func Test_Limiter_Sliding_Window_Custom_Storage_Skip_Successful_Requests(t *test
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 429, resp.StatusCode)

time.Sleep(4 * time.Second)
time.Sleep(4*time.Second + 500*time.Millisecond)

resp, err = app.Test(httptest.NewRequest(fiber.MethodGet, "/fail", nil))
utils.AssertEqual(t, nil, err)
Expand Down

0 comments on commit 1e55045

Please sign in to comment.