57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package proxy
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRateLimiter_Allow_Good(t *testing.T) {
|
|
limiter := NewRateLimiter(RateLimit{MaxConnectionsPerMinute: 2})
|
|
if !limiter.Allow("127.0.0.1:1234") {
|
|
t.Fatal("expected first connection to pass")
|
|
}
|
|
}
|
|
|
|
func TestRateLimiter_Allow_Bad(t *testing.T) {
|
|
limiter := NewRateLimiter(RateLimit{MaxConnectionsPerMinute: 1, BanDurationSeconds: 60})
|
|
if !limiter.Allow("127.0.0.1:1234") {
|
|
t.Fatal("expected first connection to pass")
|
|
}
|
|
if limiter.Allow("127.0.0.1:1234") {
|
|
t.Fatal("expected second connection to be blocked")
|
|
}
|
|
}
|
|
|
|
func TestRateLimiter_Allow_Ugly(t *testing.T) {
|
|
limiter := NewRateLimiter(RateLimit{MaxConnectionsPerMinute: 1})
|
|
limiter.Allow("127.0.0.1:1234")
|
|
time.Sleep(time.Second)
|
|
limiter.Tick()
|
|
if limiter.Allow("127.0.0.1:1234") {
|
|
t.Fatal("expected bucket not to refill fully after one second at 1/minute")
|
|
}
|
|
}
|
|
|
|
func TestCustomDiff_OnLogin_Good(t *testing.T) {
|
|
miner := &Miner{user: "wallet+5000"}
|
|
NewCustomDiff(100).OnLogin(Event{Miner: miner})
|
|
if miner.User() != "wallet" || miner.CustomDiff() != 5000 {
|
|
t.Fatalf("expected parsed custom diff, got user=%q diff=%d", miner.User(), miner.CustomDiff())
|
|
}
|
|
}
|
|
|
|
func TestCustomDiff_OnLogin_Bad(t *testing.T) {
|
|
miner := &Miner{user: "wallet"}
|
|
NewCustomDiff(100).OnLogin(Event{Miner: miner})
|
|
if miner.CustomDiff() != 100 {
|
|
t.Fatalf("expected fallback diff 100, got %d", miner.CustomDiff())
|
|
}
|
|
}
|
|
|
|
func TestCustomDiff_OnLogin_Ugly(t *testing.T) {
|
|
miner := &Miner{user: "wallet+bad"}
|
|
NewCustomDiff(100).OnLogin(Event{Miner: miner})
|
|
if miner.User() != "wallet+bad" || miner.CustomDiff() != 0 {
|
|
t.Fatalf("expected invalid suffix to preserve user and leave diff unset, got user=%q diff=%d", miner.User(), miner.CustomDiff())
|
|
}
|
|
}
|