-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_test.go
108 lines (94 loc) · 2.41 KB
/
collector_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
package gomet
import (
"path/filepath"
"reflect"
"runtime"
"testing"
"time"
)
func _equal(t *testing.T, got, expected interface{}) {
if !reflect.DeepEqual(expected, got) {
_, path, line, _ := runtime.Caller(2)
_, file := filepath.Split(path)
t.Fatalf("\n%s:%d got %#v (%s) but expected %#v (%s)\n", file, line, got, reflect.TypeOf(got), expected, reflect.TypeOf(expected))
}
}
func equal(t *testing.T, got, expected interface{}) {
// have to implement this wrapper in order to Caller func wroks properly
_equal(t, got, expected)
}
func equalf(t *testing.T, got, expected float32) {
var epsilon float32 = 0.001
if (got-expected) < epsilon && (expected-got) < epsilon {
return
}
_equal(t, got, expected)
}
func equald(t *testing.T, got, expected time.Duration) {
_equal(t, got, expected)
}
func circad(t *testing.T, got, expected time.Duration) {
if got-expected < time.Millisecond && expected-got < time.Millisecond {
return
}
_equal(t, got, expected)
}
// send 10 events, expect receiving 10 events
func TestCollect(t *testing.T) {
in := make(chan Event, 100)
period := time.Millisecond
eventsc := collect(in, period)
go func() {
for i := 0; i < 10; i++ {
in <- Event{}
time.Sleep(period / 10)
}
// have to sleep some time to allow collector process all events
time.Sleep(period)
close(in)
}()
cnt := 0
for evs := range eventsc {
// substract one auxiliary event
evs = evs[:len(evs)-1]
for range evs {
cnt++
}
t.Logf("events in tick: %d", len(evs))
}
equal(t, cnt, 10)
}
// Test case [1 group, 1 tick]:
// |**|.
// |+ |
// | +|.
//
// expected:
// group scale = 2
// st1 load = 50%
// st2 load = 50%
// st1 time = 0 (beacouse incomplete)
// st2 time = 0,5 sec
func TestAggregate(t *testing.T) {
period := time.Second
half := 500 * time.Millisecond
in := make(chan []Event)
out := aggregate(in, period)
start := time.Now()
evs := []Event{
Event{Group: "g", Worker: 1, State: "st1", Time: start},
Event{Group: "g", Worker: 2, State: "st2", Time: start},
Event{Group: "g", Worker: 2, State: "", Time: start.Add(half)},
Event{Group: "g", Worker: 3, State: "st2", Time: start.Add(half)},
Event{Group: "_auxiliary_", Time: start.Add(period)},
}
in <- evs
tick := <-out
close(in)
g := tick.Groups["g"]
equalf(t, g.Scale(period), 2)
equalf(t, g.Load("st1"), 0.5)
equalf(t, g.Load("st2"), 0.5)
equald(t, g.Lasted("st1"), 0)
equald(t, g.Lasted("st2"), 500*time.Millisecond)
}