This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
offline_test.go
182 lines (164 loc) · 4.67 KB
/
offline_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
package main
import (
"bufio"
"bytes"
"io"
"log"
"testing"
"github.com/Masterminds/sprig"
"github.com/intuit/replay-zero/templates"
"github.com/kylelemons/godebug/diff"
)
func init() {
telemetry = &nopTelemetryAgent{}
}
func TestOfflineHandleEventNoFlush(t *testing.T) {
handler := offlineHandler{
defaultBatchSize: 2,
}
handler.handleEvent(exampleHTTPEvent)
if len(handler.buffer) != 1 {
t.Errorf("Events in buffer should be 1, got %d", len(handler.buffer))
}
if handler.numWrites != 0 {
t.Errorf("Number of writes should be 0, got %d", handler.numWrites)
}
}
func TestOfflineHandleEventFlushBadTemplate(t *testing.T) {
var buff bytes.Buffer
handler := offlineHandler{
format: outputFormat{
template: `{{{`,
},
currentBatchSize: 1,
writerFactory: func(h *offlineHandler) io.Writer {
return bufio.NewWriter(&buff)
},
}
handler.handleEvent(exampleHTTPEvent)
if len(handler.buffer) != 0 {
t.Errorf("Events in buffer should be 0, got %d", len(handler.buffer))
}
if handler.numWrites != 0 {
t.Errorf("Number of writes should be 0 (should have an error), got %d", handler.numWrites)
}
length := len(buff.String())
if length != 0 {
t.Errorf("Buffer should be empty, saw length of %d", length)
}
}
func TestOfflineReadReplayHeaders(t *testing.T) {
handler := offlineHandler{}
newHeaders := handler.readReplayHeaders([]Header{
Header{Name: "Replay_batch", Value: "2"},
})
if handler.currentBatchSize != 2 {
t.Fatalf("Current batch size should be 2, got %d", handler.currentBatchSize)
}
if len(newHeaders) != 0 {
t.Fatalf("New headers size should be 0, got %d", len(newHeaders))
}
}
func TestReadBadBatchSize(t *testing.T) {
defaultSize := 1
originalCurrent := 2
handler := offlineHandler{
defaultBatchSize: defaultSize,
currentBatchSize: originalCurrent,
}
// Tests non-numeric batch size which changes nothing.
// Header should still not be present in resulting array
out := handler.readReplayHeaders([]Header{Header{Name: "Replay_batch", Value: "aaa"}})
if len(out) != 0 {
t.Errorf("Array should have length 0 but was %d", len(out))
}
if handler.currentBatchSize != originalCurrent {
t.Errorf("Curent batch size should be %d but was %d", originalCurrent, handler.currentBatchSize)
}
// Tests zero batch size which should fall back to default batch size
out = handler.readReplayHeaders([]Header{Header{Name: "Replay_batch", Value: "0"}})
if len(out) != 0 {
t.Errorf("Array should have length 0 but was %d", len(out))
}
if handler.currentBatchSize != defaultSize {
t.Errorf("Curent batch size should be %d but was %d", defaultSize, handler.currentBatchSize)
}
}
// Table-driven test for validating all templates
func TestVerifyTemplates(t *testing.T) {
testFuncMap := sprig.TxtFuncMap()
testFuncMap["now"] = func() string {
return "18 Feb 20 12:22 PST"
}
var templateTests = []struct {
name string
template string
expected string
}{
{"karate", templates.KarateBase, testKarateExpected},
{"gatling", templates.GatlingBase, testGatlingExpected},
}
for _, tt := range templateTests {
t.Run(tt.name, func(t *testing.T) {
var buff bytes.Buffer
buffWriter := bufio.NewWriter(&buff)
handler := &offlineHandler{
format: outputFormat{
template: tt.template,
},
// Making sure to test that multiple scenarios don't bunch up against each other
buffer: []HTTPEvent{sampleEvent, sampleEvent},
writerFactory: func(h *offlineHandler) io.Writer {
return buffWriter
},
templateFuncMap: testFuncMap,
}
err := handler.runTemplate()
if err != nil {
t.Fatal(err)
}
if err := buffWriter.Flush(); err != nil {
t.Error(err)
}
actual := buff.String()
difference := diff.Diff(tt.expected, actual)
if len(difference) != 0 {
t.Error("Got unexpected output from template!")
t.Error(difference)
}
})
}
}
func TestRunTemplateError(t *testing.T) {
handler := &offlineHandler{
format: outputFormat{
template: `{{{`,
},
writerFactory: emptyWriter,
}
err := handler.runTemplate()
if err == nil {
t.Error("Expected error but got nil")
}
}
func TestResetHandler(t *testing.T) {
handler := offlineHandler{
defaultBatchSize: 1,
writerFactory: emptyWriter,
}
handler.buffer = append(handler.buffer, HTTPEvent{})
handler.currentBatchSize = 2
if handler.writerFactory == nil {
t.Fatal("Handler's event writer must be non-empty for this test")
}
handler.flushBuffer()
if len(handler.buffer) != 0 {
log.Fatal("Buffer should be empty")
}
if len(handler.buffer) != 0 {
log.Fatalf("Events in buffer should be 0, got %d", len(handler.buffer))
}
if handler.currentBatchSize != 1 {
log.Fatalf("Current batch size should be 1, not %d", handler.currentBatchSize)
}
}