lnd/chanfitness/rate_limit_test.go
carla 70bca1f350
chanfitness: add flap count based rate limiting
To prevent flapping peers from endlessly dos-ing us with online and
offline events, we rate limit the number of events we will store per
period using their flap rate to determine how often we will add their
events to our in memory list of online events.

Since we are tracking online events, we need to track the aggregate
change over the rate limited period, otherwise we will lose track of
a peer's current state. For example, if we store an online event, then
do not store the subsequent offline event, we will believe that the
peer is online when they actually aren't. To address this, we "stage"
a single event which keeps track of all the events that occurred while
we were rate limiting the peer. At the end of the rate limting period,
we will store the last state for that peer, thereby ensureing that
we maintain our record of their most recent state.
2020-09-08 13:47:20 +02:00

52 lines
1.1 KiB
Go

package chanfitness
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestGetRateLimit tests getting of our rate limit using the current constants.
// It creates test cases that are relative to our constants so that they
// can be adjusted without breaking the unit test.
func TestGetRateLimit(t *testing.T) {
tests := []struct {
name string
flapCount int
rateLimit time.Duration
}{
{
name: "zero flaps",
flapCount: 0,
rateLimit: rateLimits[0],
},
{
name: "middle tier",
flapCount: rateLimitScale * (len(rateLimits) / 2),
rateLimit: rateLimits[len(rateLimits)/2],
},
{
name: "last tier",
flapCount: rateLimitScale * (len(rateLimits) - 1),
rateLimit: rateLimits[len(rateLimits)-1],
},
{
name: "beyond last tier",
flapCount: rateLimitScale * (len(rateLimits) * 2),
rateLimit: rateLimits[len(rateLimits)-1],
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
limit := getRateLimit(test.flapCount)
require.Equal(t, test.rateLimit, limit)
})
}
}