-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunked_io_test.go
70 lines (60 loc) · 1.23 KB
/
chunked_io_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
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
69
70
package netconf
import (
"bytes"
"io"
"strings"
"testing"
"github.com/freeconf/yang/fc"
)
func TestChunkedRdr(t *testing.T) {
msg := `
#4
1234
#10
1234567890
##
#1
1
##
`
rdrs := NewChunkedRdr(strings.NewReader(msg))
msg1, err := io.ReadAll(<-rdrs)
fc.AssertEqual(t, nil, err)
fc.AssertEqual(t, `12341234567890`, string(msg1))
msg2, err := io.ReadAll(<-rdrs)
fc.AssertEqual(t, nil, err)
fc.AssertEqual(t, `1`, string(msg2))
msg3, err := io.ReadAll(<-rdrs)
fc.AssertEqual(t, nil, err)
// unfortunately artifact of underlying implementation is that
// there will always be an empty reader just before closing.
fc.AssertEqual(t, ``, string(msg3))
fc.AssertEqual(t, nil, <-rdrs)
}
func TestChunkedWtr(t *testing.T) {
var buf bytes.Buffer
msg := NewChunkedWtr(&buf)
n, err := msg.Write([]byte("1234567890"))
fc.AssertEqual(t, nil, err)
fc.AssertEqual(t, 10, n)
fc.AssertEqual(t, nil, msg.Close())
msg = NewChunkedWtr(&buf)
n, err = msg.Write([]byte("1234"))
fc.AssertEqual(t, nil, err)
fc.AssertEqual(t, 4, n)
n, err = msg.Write([]byte("1"))
fc.AssertEqual(t, nil, err)
fc.AssertEqual(t, 1, n)
fc.AssertEqual(t, nil, msg.Close())
expected := `
#10
1234567890
##
#4
1234
#1
1
##
`
fc.AssertEqual(t, expected, buf.String())
}