-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogtailTransport_test.go
189 lines (162 loc) · 4.07 KB
/
logtailTransport_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package ulog
import (
"errors"
"sync"
"testing"
"time"
"github.com/blugnu/test"
)
func TestLogtailTransport(t *testing.T) {
// ARRANGE
testcases := []struct {
scenario string
exec func(t *testing.T)
}{
// LogtailTransport tests
{scenario: "LogtailTransport/option error",
exec: func(t *testing.T) {
// ARRANGE
opterr := errors.New("option error")
opt := func(*logtail) error { return opterr }
// ACT
result, err := LogtailTransport(opt)()
// ASSERT
test.That(t, result).IsNil()
test.Error(t, err).Is(opterr)
},
},
{scenario: "LogtailTransport/with no options",
exec: func(t *testing.T) {
// ACT
result, err := LogtailTransport()()
// ASSERT
test.That(t, result).IsNotNil()
test.That(t, err).IsNil()
if result, ok := test.IsType[*logtail](t, result); ok {
batch := result.batch
test.That(t, batch, "batch").IsNotNil()
test.That(t, batch.max, "batch capacity").Equals(16)
if handler, ok := test.IsType[*logtailBatchHandler](t, batch.batchHandler); ok {
test.That(t, handler.endpoint, "endpoint").Equals("https://in.logs.betterstack.com")
}
test.That(t, result.maxLatency, "max latency").Equals(10 * time.Second)
}
},
},
{scenario: "log",
exec: func(t *testing.T) {
// ARRANGE
sut := &logtail{ch: make(chan []byte)}
// setup a coroutine to listen to the channel used by the transport
// when sending log entries, copying the sent bytes
sent := []byte{}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
sent = append(sent, <-sut.ch...)
}()
// ACT
sut.log([]byte("bytes sent"))
// CLEANUP
close(sut.ch)
wg.Wait()
// ASSERT
test.That(t, sent).Equals([]byte("bytes sent"))
},
},
// run scenarios
{scenario: "run",
exec: func(t *testing.T) {
// ARRANGE
mh := &mockBatchHandler{}
sut := &logtail{
ch: make(chan []byte),
batch: &Batch{},
maxLatency: 50 * time.Millisecond,
}
testcases := []struct {
scenario string
exec func(t *testing.T)
}{
{scenario: "adds entries to batch and flushes when batch full or channel closed",
exec: func(t *testing.T) {
// ARRANGE
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
sut.run()
}()
// ACT
sut.ch <- []byte("entry 1")
sut.ch <- []byte("entry 2")
sut.ch <- []byte("entry 3") // <- fills the first batch
sut.ch <- []byte("entry 4") // <- incomplete batch will be sent when the channel is closed
// CLEANUP
close(sut.ch)
wg.Wait()
// ASSERT
test.That(t, mh.sendCalls).Equals(2)
test.That(t, mh.sentEntries).Equals(4)
},
},
{scenario: "sends partial batch when max latency exceeded",
exec: func(t *testing.T) {
// ARRANGE
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
sut.run()
}()
// ACT
sut.ch <- []byte("entry 1")
sut.ch <- []byte("entry 2")
time.Sleep(sut.maxLatency * 2)
// CLEANUP
close(sut.ch)
wg.Wait()
// ASSERT
test.That(t, mh.sendCalls).Equals(1)
test.That(t, mh.sentEntries).Equals(2)
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
// ARRANGE
mh.reset()
sut.batch.init(mh, 3)
sut.ch = make(chan []byte)
// ACT
tc.exec(t)
})
}
},
},
// stop
{scenario: "stop",
exec: func(t *testing.T) {
// ARRANGE
sut := &logtail{ch: make(chan []byte)}
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
<-sut.ch // will not receive anything; will yield nil when the channel is closed
}()
// ACT
sut.stop()
// ACT / ASSERT
// (nothing to assert; the test will timeout if the channel is not closed)
wg.Wait()
},
},
}
for _, tc := range testcases {
t.Run(tc.scenario, func(t *testing.T) {
tc.exec(t)
})
}
}