mirror of
https://github.com/HDT3213/godis.git
synced 2025-09-26 21:01:17 +08:00
test and protect edge case
This commit is contained in:
@@ -42,6 +42,9 @@ func NewSlowLogger(maxEntries int, threshold int64) *SlowLogger {
|
||||
}
|
||||
|
||||
func (sl *SlowLogger) Record(start time.Time, args [][]byte, client string) {
|
||||
if sl == nil || len(sl.entries) == 0 {
|
||||
return
|
||||
}
|
||||
duration := time.Since(start)
|
||||
micros := duration.Microseconds()
|
||||
|
||||
|
@@ -12,18 +12,26 @@ import (
|
||||
func TestSlowLogger_Record(t *testing.T) {
|
||||
logger := NewSlowLogger(5, 1000)
|
||||
|
||||
start := time.Now()
|
||||
time.Sleep(2 * time.Millisecond)
|
||||
|
||||
logger.Record(start, utils.ToCmdLine("GET", "key1"), "127.0.0.1:12345")
|
||||
|
||||
if logger.Len() != 1 {
|
||||
t.Errorf("Expected 1 entry, got %d", logger.Len())
|
||||
start := time.Now().Add(-time.Minute)
|
||||
for i := 0; i < 10; i++ {
|
||||
logger.Record(start, utils.ToCmdLine("GET", strconv.Itoa(i)), "127.0.0.1:12345")
|
||||
}
|
||||
|
||||
if logger.Len() != 5 {
|
||||
t.Errorf("Expected 1 entry, got %d", logger.Len())
|
||||
}
|
||||
for i, e := range logger.entries {
|
||||
actual := string(e.Command[1])
|
||||
expect := strconv.Itoa(i+5)
|
||||
if actual != expect {
|
||||
t.Errorf("Expected %s, got %s", expect, actual)
|
||||
}
|
||||
}
|
||||
|
||||
logger2 := NewSlowLogger(5, 1000)
|
||||
start = time.Now()
|
||||
logger.Record(start, utils.ToCmdLine("SET", "key2"), "127.0.0.1:12346")
|
||||
if logger.Len() != 1 {
|
||||
logger2.Record(start, utils.ToCmdLine("SET", "key2"), "127.0.0.1:12346")
|
||||
if logger2.Len() != 0 {
|
||||
t.Errorf("Below threshold query should not be recorded, got %d entries", logger.Len())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user