Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttling UT improvement #279

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions jsonrpc/throttling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jsonrpc
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"

Expand All @@ -13,26 +14,28 @@ import (
func TestThrottling(t *testing.T) {
t.Parallel()

const maxRequests = 5

var requests atomic.Int32
var attempts = []struct {
duration time.Duration
delay time.Duration
isError bool
}{
// 1st 5 starts immediately, no error, order of execution is irrelevant
{200 * time.Millisecond, 0, false},
{1000 * time.Millisecond, 0, false},
{1000 * time.Millisecond, 0, false},
{1000 * time.Millisecond, 0, false},
{1000 * time.Millisecond, 0, false},
{200 * time.Millisecond, 0},
{1000 * time.Millisecond, 0},
{1000 * time.Millisecond, 0},
{1000 * time.Millisecond, 0},
{1000 * time.Millisecond, 0},

// 6th & 8th attempt should fail, from now on order of execution is relevant, hence delay > 0
{20 * time.Millisecond, 100 * time.Millisecond, true},
{200 * time.Millisecond, 300 * time.Millisecond, false},
{20 * time.Millisecond, 400 * time.Millisecond, true},
{200 * time.Millisecond, 600 * time.Millisecond, false},
{20 * time.Millisecond, 100 * time.Millisecond},
{200 * time.Millisecond, 300 * time.Millisecond},
{20 * time.Millisecond, 400 * time.Millisecond},
{200 * time.Millisecond, 600 * time.Millisecond},
}

th := NewThrottling(5, 20*time.Millisecond)
th := NewThrottling(maxRequests, 20*time.Millisecond)
sfn := func(value int, sleep time.Duration) func() (interface{}, error) {
return func() (interface{}, error) {
time.Sleep(sleep)
Expand All @@ -42,23 +45,29 @@ func TestThrottling(t *testing.T) {
}

wg := sync.WaitGroup{}
wg.Add(9)
wg.Add(len(attempts))

for i := 0; i < len(attempts); i++ {
go func(value int, duration time.Duration, delay time.Duration, isError bool) {
go func(value int, duration time.Duration, delay time.Duration) {
defer wg.Done()
time.Sleep(delay)

var isError bool
if requests.Add(1) > maxRequests {
isError = true
}
res, err := th.AttemptRequest(context.Background(), sfn(value, duration))

requests.Add(-1)

if isError {
require.ErrorIs(t, err, errRequestLimitExceeded)
assert.Nil(t, res)
} else {
require.NoError(t, err)
assert.Equal(t, value, res.(int))
}
}(i, attempts[i].duration, attempts[i].delay, attempts[i].isError)
}(i, attempts[i].duration, attempts[i].delay)
}

wg.Wait()
Expand Down
Loading