mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-12 23:54:01 +09:00
The log stores are shared by every namespace routed to them, and the root store also serves platform Grafana, so one tenant's dashboard must not be able to monopolise vlselect. Live tails are counted apart from request concurrency because they hold their slot for minutes and would otherwise starve the same user's panels. Assisted-by: LLM Signed-off-by: ZverGuy <maximbel2003@gmail.com>
105 lines
2.7 KiB
Go
105 lines
2.7 KiB
Go
/*
|
|
Copyright 2026 The Cozystack Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package observabilityproxy
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLimiterConcurrency(t *testing.T) {
|
|
l := newLimiter(LimiterConfig{MaxConcurrentPerUser: 2})
|
|
r1, ok1 := l.acquire("alice")
|
|
_, ok2 := l.acquire("alice")
|
|
if !ok1 || !ok2 {
|
|
t.Fatal("first two requests refused")
|
|
}
|
|
if _, ok := l.acquire("alice"); ok {
|
|
t.Fatal("third concurrent request admitted")
|
|
}
|
|
if _, ok := l.acquire("bob"); !ok {
|
|
t.Fatal("another user is limited by alice's requests")
|
|
}
|
|
r1()
|
|
r1()
|
|
if _, ok := l.acquire("alice"); !ok {
|
|
t.Fatal("slot not returned on release")
|
|
}
|
|
if _, ok := l.acquire("alice"); ok {
|
|
t.Fatal("double release freed a second slot")
|
|
}
|
|
}
|
|
|
|
func TestLimiterTails(t *testing.T) {
|
|
l := newLimiter(LimiterConfig{MaxConcurrentPerUser: 1, MaxTailsPerUser: 2, MaxTails: 3})
|
|
if _, ok := l.acquire("alice"); !ok {
|
|
t.Fatal("request refused")
|
|
}
|
|
a1, ok1 := l.acquireTail("alice")
|
|
_, ok2 := l.acquireTail("alice")
|
|
if !ok1 || !ok2 {
|
|
t.Fatal("tails share the request concurrency budget")
|
|
}
|
|
if _, ok := l.acquireTail("alice"); ok {
|
|
t.Fatal("per-user tail cap not enforced")
|
|
}
|
|
if _, ok := l.acquireTail("bob"); !ok {
|
|
t.Fatal("bob refused below the total cap")
|
|
}
|
|
if _, ok := l.acquireTail("carol"); ok {
|
|
t.Fatal("total tail cap not enforced")
|
|
}
|
|
if got := l.openTails(); got != 3 {
|
|
t.Fatalf("openTails = %d, want 3", got)
|
|
}
|
|
a1()
|
|
if _, ok := l.acquireTail("carol"); !ok {
|
|
t.Fatal("total slot not returned on release")
|
|
}
|
|
}
|
|
|
|
func TestLimiterRate(t *testing.T) {
|
|
now := time.Unix(1000, 0)
|
|
l := newLimiter(LimiterConfig{RatePerUser: 2, RateBurst: 2})
|
|
l.now = func() time.Time { return now }
|
|
if !l.allowRate("alice") || !l.allowRate("alice") {
|
|
t.Fatal("burst refused")
|
|
}
|
|
if l.allowRate("alice") {
|
|
t.Fatal("request over the burst admitted")
|
|
}
|
|
if !l.allowRate("bob") {
|
|
t.Fatal("bob limited by alice's rate")
|
|
}
|
|
now = now.Add(500 * time.Millisecond)
|
|
if !l.allowRate("alice") {
|
|
t.Fatal("token not refilled after 1/rate")
|
|
}
|
|
if l.allowRate("alice") {
|
|
t.Fatal("refilled more than elapsed*rate")
|
|
}
|
|
}
|
|
|
|
func TestLimiterRateDisabled(t *testing.T) {
|
|
l := newLimiter(LimiterConfig{})
|
|
for range 100 {
|
|
if !l.allowRate("alice") {
|
|
t.Fatal("rate limit applied while disabled")
|
|
}
|
|
}
|
|
}
|