-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_cache_test.go
More file actions
56 lines (49 loc) · 1.16 KB
/
Copy pathlog_cache_test.go
File metadata and controls
56 lines (49 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"testing"
)
func TestLogCache(t *testing.T) {
size := 5
lc := newLogCache(size)
// Test writing fewer than size
for i := 0; i < 3; i++ {
fmt.Fprintf(lc, "log message %d\n", i)
}
logs := lc.GetLogs()
if len(logs) != 3 {
t.Errorf("expected 3 logs, got %d", len(logs))
}
if logs[0] != "log message 0" {
t.Errorf("expected 'log message 0', got '%s'", logs[0])
}
// Test filling the cache
for i := 3; i < 5; i++ {
fmt.Fprintf(lc, "log message %d\n", i)
}
logs = lc.GetLogs()
if len(logs) != 5 {
t.Errorf("expected 5 logs, got %d", len(logs))
}
// Test overflowing the cache
fmt.Fprintf(lc, "log message 5\n")
logs = lc.GetLogs()
if len(logs) != 5 {
t.Errorf("expected 5 logs, got %d", len(logs))
}
// Should contain messages 1 to 5
if logs[0] != "log message 1" {
t.Errorf("expected 'log message 1', got '%s'", logs[0])
}
if logs[4] != "log message 5" {
t.Errorf("expected 'log message 5', got '%s'", logs[4])
}
// Test Write method directly
n, err := lc.Write([]byte("test write"))
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if n != 10 {
t.Errorf("expected 10 bytes written, got %d", n)
}
}