-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_test.go
41 lines (37 loc) · 997 Bytes
/
server_test.go
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
package lwes
import (
"fmt"
"strings"
"sync"
"testing"
)
func TestBufferRetention(t *testing.T) {
msg := `test`
pool := &sync.Pool{}
reader := strings.NewReader(msg)
buffer1 := NewFixedBuffer(pool, len(msg))
_, err := buffer1.ReadFrom(reader)
if err != nil {
t.Fatalf("Unexpected error while writing to buffer %v", err)
}
if string(buffer1.Bytes()) != msg {
t.Fatalf(`Unexpected message written "%s"`, string(buffer1.Bytes()))
}
buffer1.Done()
buffer2 := NewFixedBuffer(pool, len(msg))
if string(buffer2.Bytes()) != "" {
t.Fatalf(`Unexpected data buffered "%s"`, string(buffer2.Bytes()))
}
}
func TestBufferMultWrite(t *testing.T) {
msg := `test`
reader := strings.NewReader(msg)
buffer := NewFixedBuffer(&sync.Pool{}, 100)
buffer.ReadFrom(reader)
reader.Seek(0, 0)
buffer.ReadFrom(reader)
expected := fmt.Sprintf("%s%s", msg, msg)
if string(buffer.Bytes()) != expected {
t.Fatalf("Unexpected written message: %s != %s", string(buffer.Bytes()), expected)
}
}