-
Notifications
You must be signed in to change notification settings - Fork 104
/
parser_test.go
92 lines (71 loc) · 1.91 KB
/
parser_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package manta
import (
"bytes"
"io"
"net"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type timeoutReader struct {
r io.Reader
maxBytes int
readBytes int
}
var _ io.Reader = &timeoutReader{}
func newTimeoutReader(data []byte, maxBytes int) *timeoutReader {
return &timeoutReader{r: bytes.NewReader(data), maxBytes: maxBytes}
}
func (r *timeoutReader) Read(p []byte) (int, error) {
n, err := r.r.Read(p)
r.readBytes += n
if r.readBytes >= r.maxBytes {
return n, timeoutError{}
}
return n, err
}
type timeoutError struct{}
var _ net.Error = timeoutError{}
func (e timeoutError) Error() string {
return "timed out"
}
func (e timeoutError) Timeout() bool {
return true
}
func (e timeoutError) Temporary() bool {
return true
}
func TestStreamingReaderTimeout(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping test in CI environment")
}
assert := assert.New(t)
// get data
data := mustGetReplayData("2159568145", "https://s3-us-west-2.amazonaws.com/manta.dotabuff/2159568145.dem")
reader := newTimeoutReader(data, 10000)
// create a new parser with the streaming response
parser, err := NewStreamParser(reader)
if err != nil {
t.Fatalf("unable to create parser: %s", err)
}
// expect the parser to fail after a bit under 2 seconds with a timeout error
err = parser.Start()
netErr, ok := err.(net.Error)
assert.True(ok)
assert.True(netErr.Timeout())
}
func TestStreamingReaderUnexpectedEOF(t *testing.T) {
assert := assert.New(t)
// get data
data := mustGetReplayData("2159568145", "https://s3-us-west-2.amazonaws.com/manta.dotabuff/2159568145.dem")
// cut the data at a random point
reader := bytes.NewReader(data[:6666])
// begin parsing the incomplete data
parser, err := NewStreamParser(reader)
if err != nil {
t.Fatalf("unable to create parser: %s", err)
}
// expect an unexpected EOF error
err = parser.Start()
assert.Equal(io.ErrUnexpectedEOF, err)
}