-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtruncbuffer_test.go
More file actions
68 lines (60 loc) · 1.47 KB
/
truncbuffer_test.go
File metadata and controls
68 lines (60 loc) · 1.47 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
57
58
59
60
61
62
63
64
65
66
67
68
package truncbuffer
import (
"io"
"sync"
"testing"
)
var _ io.Writer = (*TruncBuffer)(nil)
func TestTruncBuffer_SmallWrites(t *testing.T) {
tb := NewTruncBuffer(10)
tb.Write([]byte("hello"))
if got := string(tb.Bytes()); got != "hello" {
t.Fatalf("got %q, want %q", got, "hello")
}
}
func TestTruncBuffer_ExactMax(t *testing.T) {
tb := NewTruncBuffer(5)
tb.Write([]byte("abcde"))
if got := string(tb.Bytes()); got != "abcde" {
t.Fatalf("got %q, want %q", got, "abcde")
}
}
func TestTruncBuffer_OverflowSingleWrite(t *testing.T) {
tb := NewTruncBuffer(5)
tb.Write([]byte("abcdefgh"))
if got := string(tb.Bytes()); got != "defgh" {
t.Fatalf("got %q, want %q", got, "defgh")
}
}
func TestTruncBuffer_OverflowMultipleWrites(t *testing.T) {
tb := NewTruncBuffer(6)
tb.Write([]byte("abc"))
tb.Write([]byte("defgh"))
if got := string(tb.Bytes()); got != "cdefgh" {
t.Fatalf("got %q, want %q", got, "cdefgh")
}
}
func TestTruncBuffer_ManySmallWrites(t *testing.T) {
tb := NewTruncBuffer(4)
for _, b := range []byte("abcdefg") {
tb.Write([]byte{b})
}
if got := string(tb.Bytes()); got != "defg" {
t.Fatalf("got %q, want %q", got, "defg")
}
}
func TestTruncBuffer_ConcurrentWrites(t *testing.T) {
tb := NewTruncBuffer(64)
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
tb.Write([]byte("abcdefgh"))
}()
}
wg.Wait()
if got := len(tb.Bytes()); got > 64 {
t.Fatalf("buffer exceeded max: got %d bytes", got)
}
}