-
Notifications
You must be signed in to change notification settings - Fork 86
/
reducer_test.go
183 lines (147 loc) · 4.39 KB
/
reducer_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
package gonx
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestReducer(t *testing.T) {
Convey("Test process input channel with reducers", t, func() {
input := make(chan *Entry, 10)
Convey("ReadAll reducer", func() {
reducer := new(ReadAll)
// Prepare import channel
entry := NewEmptyEntry()
input <- entry
close(input)
output := make(chan *Entry, 1) // Make it buffered to avoid deadlock
reducer.Reduce(input, output)
// ReadAll reducer writes input channel to the output
result, ok := <-output
So(ok, ShouldBeTrue)
So(result, ShouldEqual, entry)
})
Convey("With filled input channel", func() {
// Prepare import channel
input <- NewEntry(Fields{
"uri": "/asd/fgh",
"host": "alpha.example.com",
"foo": "1",
"bar": "2",
"baz": "3",
})
input <- NewEntry(Fields{
"uri": "/zxc/vbn",
"host": "beta.example.com",
"foo": "4",
"bar": "5",
"baz": "6",
})
input <- NewEntry(Fields{
"uri": "/ijk/lmn",
"host": "beta.example.com",
"foo": "7",
"bar": "8",
"baz": "9",
})
close(input)
total := float64(len(input))
output := make(chan *Entry, 10) // Make it buffered to avoid deadlock
Convey("Count reducer", func() {
reducer := new(Count)
reducer.Reduce(input, output)
result, ok := <-output
So(ok, ShouldBeTrue)
count, err := result.FloatField("count")
So(err, ShouldBeNil)
So(count, ShouldEqual, total)
})
Convey("Sum reducer", func() {
reducer := &Sum{[]string{"foo", "bar"}}
reducer.Reduce(input, output)
result, ok := <-output
So(ok, ShouldBeTrue)
value, err := result.FloatField("foo")
So(err, ShouldBeNil)
So(value, ShouldEqual, 1+4+7)
value, err = result.FloatField("bar")
So(err, ShouldBeNil)
So(value, ShouldEqual, 2+5+8)
_, err = result.Field("buz")
So(err, ShouldNotBeNil)
})
Convey("Avg reducer", func() {
reducer := &Avg{[]string{"foo", "bar"}}
reducer.Reduce(input, output)
result, ok := <-output
So(ok, ShouldBeTrue)
value, err := result.FloatField("foo")
So(err, ShouldBeNil)
So(value, ShouldEqual, (1+4+7)/total)
value, err = result.FloatField("bar")
So(err, ShouldBeNil)
So(value, ShouldEqual, (2+5+8)/total)
_, err = result.Field("buz")
So(err, ShouldNotBeNil)
})
Convey("Chain reducer", func() {
reducer := NewChain(&Avg{[]string{"foo", "bar"}}, &Count{})
So(len(reducer.reducers), ShouldEqual, 2)
reducer.Reduce(input, output)
result, ok := <-output
So(ok, ShouldBeTrue)
value, err := result.FloatField("foo")
So(err, ShouldBeNil)
So(value, ShouldEqual, (1+4+7)/total)
value, err = result.FloatField("bar")
So(err, ShouldBeNil)
So(value, ShouldEqual, (2+5+8)/total)
count, err := result.FloatField("count")
So(err, ShouldBeNil)
So(count, ShouldEqual, total)
_, err = result.Field("buz")
So(err, ShouldNotBeNil)
})
Convey("Group reducer", func() {
reducer := NewGroupBy(
// Fields to group by
[]string{"host"},
// Result reducers
&Sum{[]string{"foo", "bar"}},
new(Count),
)
So(len(reducer.reducers), ShouldEqual, 2)
reducer.Reduce(input, output)
// Collect result entries from output channel to the map, because reading
// from channel can be in any order, it depends on each reducer processing
resultMap := make(map[string]*Entry)
for result := range output {
value, err := result.Field("host")
So(err, ShouldBeNil)
resultMap[value] = result
}
So(len(resultMap), ShouldEqual, 2)
// Read and assert first group result
result := resultMap["alpha.example.com"]
floatVal, err := result.FloatField("foo")
So(err, ShouldBeNil)
So(floatVal, ShouldEqual, 1)
floatVal, err = result.FloatField("bar")
So(err, ShouldBeNil)
So(floatVal, ShouldEqual, 2)
count, err := result.FloatField("count")
So(err, ShouldBeNil)
So(count, ShouldEqual, 1)
// Read and assert second group result
result = resultMap["beta.example.com"]
floatVal, err = result.FloatField("foo")
So(err, ShouldBeNil)
So(floatVal, ShouldEqual, 4+7)
floatVal, err = result.FloatField("bar")
So(err, ShouldBeNil)
So(floatVal, ShouldEqual, 5+8)
count, err = result.FloatField("count")
So(err, ShouldBeNil)
So(count, ShouldEqual, 2)
})
})
})
}