grain/tests/sizeLimits_test.go

75 lines
2.7 KiB
Go
Raw Normal View History

2024-07-26 20:46:01 +00:00
package tests
import (
2024-07-30 15:27:38 +00:00
"grain/config"
2024-07-26 20:46:01 +00:00
"testing"
)
func TestSizeLimiterGlobalMaxSize(t *testing.T) {
2024-07-30 15:27:38 +00:00
sizeLimiter := config.NewSizeLimiter(1024) // Set global max size to 1024 bytes
2024-07-26 20:46:01 +00:00
// Test that an event within the global max size is allowed
if allowed, _ := sizeLimiter.AllowSize(0, 512); !allowed {
t.Error("Event within global max size should be allowed")
}
// Test that an event exceeding the global max size is not allowed
if allowed, msg := sizeLimiter.AllowSize(0, 2048); allowed {
t.Error("Event exceeding global max size should not be allowed")
} else {
expectedMsg := "Global event size limit exceeded"
if msg != expectedMsg {
t.Errorf("Expected message: %s, got: %s", expectedMsg, msg)
}
}
}
func TestSizeLimiterKindSpecificSize(t *testing.T) {
2024-07-30 15:27:38 +00:00
sizeLimiter := config.NewSizeLimiter(1024) // Set global max size to 1024 bytes
2024-07-26 20:46:01 +00:00
sizeLimiter.AddKindSizeLimit(1, 512) // Set max size for kind 1 to 512 bytes
// Test that an event within the kind-specific max size is allowed
if allowed, _ := sizeLimiter.AllowSize(1, 256); !allowed {
t.Error("Event within kind-specific max size should be allowed")
}
// Test that an event exceeding the kind-specific max size is not allowed
if allowed, msg := sizeLimiter.AllowSize(1, 1024); allowed {
t.Error("Event exceeding kind-specific max size should not be allowed")
} else {
expectedMsg := "Event size limit exceeded for kind"
if msg != expectedMsg {
t.Errorf("Expected message: %s, got: %s", expectedMsg, msg)
}
}
// Test that an event exceeding the global max size is not allowed even if within the kind-specific max size
if allowed, msg := sizeLimiter.AllowSize(1, 2048); allowed {
t.Error("Event exceeding global max size should not be allowed even if within kind-specific max size")
} else {
expectedMsg := "Global event size limit exceeded"
if msg != expectedMsg {
t.Errorf("Expected message: %s, got: %s", expectedMsg, msg)
}
}
}
func TestSizeLimiterNoKindSpecificLimit(t *testing.T) {
2024-07-30 15:27:38 +00:00
sizeLimiter := config.NewSizeLimiter(1024) // Set global max size to 1024 bytes
2024-07-26 20:46:01 +00:00
// Test that an event for a kind without a specific limit is governed by the global limit
if allowed, _ := sizeLimiter.AllowSize(2, 512); !allowed {
t.Error("Event within global max size should be allowed for kinds without specific limit")
}
// Test that an event exceeding the global max size is not allowed for kinds without a specific limit
if allowed, msg := sizeLimiter.AllowSize(2, 2048); allowed {
t.Error("Event exceeding global max size should not be allowed for kinds without specific limit")
} else {
expectedMsg := "Global event size limit exceeded"
if msg != expectedMsg {
t.Errorf("Expected message: %s, got: %s", expectedMsg, msg)
}
}
}