-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream_test.go
54 lines (46 loc) · 1.35 KB
/
stream_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
package stream
import (
"reflect"
"testing"
)
func TestTopLength(t *testing.T) {
summary := NewSummary(2)
summary.Observe("one")
assertEqual(t, len(summary.Top(10)), 1)
summary.Observe("two")
assertEqual(t, len(summary.Top(1)), 1)
assertEqual(t, len(summary.Top(0)), 0)
}
func TestTopOrderedDescending(t *testing.T) {
summary := NewSummary(3)
summary.Observe("once a")
summary.Observe("once b")
summary.Observe("twice")
summary.Observe("twice")
assertEqual(t, summary.Top(3)[0].Item(), "twice")
assertEqual(t, summary.Top(3)[1].Item(), "once a")
assertEqual(t, summary.Top(3)[2].Item(), "once b")
}
func TestExceedCapacity(t *testing.T) {
summary := NewSummary(1)
summary.Observe("twice")
summary.Observe("one")
summary.Observe("twice")
assertEqual(t, summary.Top(1)[0].Item(), "twice")
assertEqual(t, summary.Top(1)[0].Count(), uint64(3))
assertEqual(t, summary.Top(1)[0].ErrorRate(), uint64(2))
}
func TestObserved(t *testing.T) {
summary := NewSummary(1)
assertEqual(t, summary.Observed(), uint64(0))
summary.Observe("item a")
assertEqual(t, summary.Observed(), uint64(1))
summary.Observe("item b")
summary.Observe("item c")
assertEqual(t, summary.Observed(), uint64(3))
}
func assertEqual(t *testing.T, actual, expected interface{}) {
if !reflect.DeepEqual(actual, expected) {
t.Errorf("Got %+v but expected %+v", actual, expected)
}
}