75 lines
2.3 KiB
Go
75 lines
2.3 KiB
Go
|
|
package monitoring
|
||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestStore_RecordAndSummary(t *testing.T) {
|
||
|
|
s := NewStore()
|
||
|
|
|
||
|
|
s.Record(RequestRecord{Time: time.Now(), Method: "GET", Path: "/x", StatusCode: 200, DurationMs: 10})
|
||
|
|
s.Record(RequestRecord{Time: time.Now(), Method: "GET", Path: "/x", StatusCode: 500, DurationMs: 20})
|
||
|
|
|
||
|
|
sum := s.Summary()
|
||
|
|
if sum.TotalRequests != 2 {
|
||
|
|
t.Fatalf("expected 2 total requests, got %d", sum.TotalRequests)
|
||
|
|
}
|
||
|
|
if sum.TotalErrors != 1 {
|
||
|
|
t.Fatalf("expected 1 error (status>=400), got %d", sum.TotalErrors)
|
||
|
|
}
|
||
|
|
if sum.ErrorRatePct != 50 {
|
||
|
|
t.Fatalf("expected 50%% error rate, got %v", sum.ErrorRatePct)
|
||
|
|
}
|
||
|
|
if sum.AvgLatencyMs != 15 {
|
||
|
|
t.Fatalf("expected avg latency 15ms, got %v", sum.AvgLatencyMs)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestStore_RecentOrder(t *testing.T) {
|
||
|
|
s := NewStore()
|
||
|
|
s.Record(RequestRecord{Time: time.Now(), Path: "/1", StatusCode: 200})
|
||
|
|
s.Record(RequestRecord{Time: time.Now(), Path: "/2", StatusCode: 200})
|
||
|
|
s.Record(RequestRecord{Time: time.Now(), Path: "/3", StatusCode: 200})
|
||
|
|
|
||
|
|
recent := s.Recent(10)
|
||
|
|
if len(recent) != 3 {
|
||
|
|
t.Fatalf("expected 3 records, got %d", len(recent))
|
||
|
|
}
|
||
|
|
// جدیدترین باید اول باشه
|
||
|
|
if recent[0].Path != "/3" || recent[1].Path != "/2" || recent[2].Path != "/1" {
|
||
|
|
t.Fatalf("expected newest-first order, got %+v", recent)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestStore_RecentLimit(t *testing.T) {
|
||
|
|
s := NewStore()
|
||
|
|
for i := 0; i < 5; i++ {
|
||
|
|
s.Record(RequestRecord{Time: time.Now(), Path: "/x", StatusCode: 200})
|
||
|
|
}
|
||
|
|
if got := len(s.Recent(2)); got != 2 {
|
||
|
|
t.Fatalf("expected limit of 2, got %d", got)
|
||
|
|
}
|
||
|
|
if got := len(s.Recent(100)); got != 5 {
|
||
|
|
t.Fatalf("expected all 5 when limit exceeds count, got %d", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestStore_RingBufferWraparound(t *testing.T) {
|
||
|
|
s := NewStore()
|
||
|
|
// بیشتر از ظرفیت بافر ثبت میکنیم تا رفتار wraparound رو تست کنیم
|
||
|
|
for i := 0; i < bufferSize+50; i++ {
|
||
|
|
s.Record(RequestRecord{Time: time.Now(), Path: "/x", StatusCode: 200, DurationMs: 1})
|
||
|
|
}
|
||
|
|
|
||
|
|
sum := s.Summary()
|
||
|
|
if sum.TotalRequests != int64(bufferSize+50) {
|
||
|
|
t.Fatalf("expected total requests to keep counting past buffer size, got %d", sum.TotalRequests)
|
||
|
|
}
|
||
|
|
|
||
|
|
recent := s.Recent(bufferSize + 100)
|
||
|
|
if len(recent) != bufferSize {
|
||
|
|
t.Fatalf("expected Recent to be capped at bufferSize (%d), got %d", bufferSize, len(recent))
|
||
|
|
}
|
||
|
|
}
|