-
Notifications
You must be signed in to change notification settings - Fork 86
/
http_log_writer_test.go
90 lines (68 loc) · 1.92 KB
/
http_log_writer_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
package worker
import (
"testing"
"time"
gocontext "context"
"github.com/stretchr/testify/assert"
)
func buildTestHTTPLogWriter() (gocontext.CancelFunc, *httpLogWriter, error) {
ctx, cancel := gocontext.WithCancel(gocontext.TODO())
hlw, err := newHTTPLogWriter(
ctx,
"https://jobs.example.org/foo",
"fafafaf",
1337,
time.Second)
hlw.SetMaxLogLength(100)
hlw.SetCancelFunc(noCancel)
return cancel, hlw, err
}
func TestNewHTTPLogWriter(t *testing.T) {
cancel, hlw, err := buildTestHTTPLogWriter()
defer cancel()
assert.Nil(t, err)
assert.NotNil(t, hlw)
assert.Implements(t, (*LogWriter)(nil), hlw)
}
func TestHTTPLogWriter_Write(t *testing.T) {
cancel, hlw, _ := buildTestHTTPLogWriter()
defer cancel()
assert.NotNil(t, hlw)
n, err := hlw.Write([]byte("it's a hot one out there"))
assert.Nil(t, err)
assert.True(t, n > 0)
}
func TestHTTPLogWriter_Write_HitsMaxLogLength(t *testing.T) {
cancel, hlw, _ := buildTestHTTPLogWriter()
defer cancel()
assert.NotNil(t, hlw)
hlw.bytesWritten = 1000
n, err := hlw.Write([]byte("there's a strong wind blowing"))
assert.Nil(t, err)
assert.True(t, hlw.MaxLengthReached())
assert.Equal(t, 0, n)
}
func TestHTTPLogWriter_Write_HitsMaxLogLength_CannotWriteAndClose(t *testing.T) {
cancel, hlw, _ := buildTestHTTPLogWriter()
defer cancel()
assert.NotNil(t, hlw)
hlw.bytesWritten = 1000
mbs := hlw.lps.maxBufferSize
hlw.lps.maxBufferSize = 0
defer func() { hlw.lps.maxBufferSize = mbs }()
n, err := hlw.Write([]byte("looks like rain mmm hmm"))
assert.Nil(t, err)
assert.True(t, hlw.MaxLengthReached())
assert.Equal(t, 0, n)
}
func TestHTTPLogWriter_Write_HitsMaxLogLength_CannotWriteAndClose_LogClosed(t *testing.T) {
cancel, hlw, _ := buildTestHTTPLogWriter()
defer cancel()
assert.NotNil(t, hlw)
hlw.bytesWritten = 1000
err := hlw.Close()
assert.Nil(t, err)
n, err := hlw.Write([]byte("a storm is a comin"))
assert.NotNil(t, err)
assert.Equal(t, 0, n)
}