-
Notifications
You must be signed in to change notification settings - Fork 54
/
taskemitter_test.go
109 lines (102 loc) · 2.51 KB
/
taskemitter_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
package oonimkall
import "testing"
func TestTaskEmitterUsingChan(t *testing.T) {
t.Run("ordinary emit", func(t *testing.T) {
out := make(chan *event)
emitter := newTaskEmitterUsingChan(out)
go func() {
emitter.Emit("foo", nil)
}()
ev := <-out
if ev.Key != "foo" {
t.Fatal("invalid key")
}
if ev.Value != nil {
t.Fatal("invalid value")
}
})
t.Run("emit after close", func(t *testing.T) {
out := make(chan *event)
emitter := newTaskEmitterUsingChan(out)
emitter.Close()
done := make(chan interface{})
go func() {
emitter.Emit("foo", nil)
close(done)
}()
<-done
select {
case <-out:
t.Fatal("should not receive event here")
default:
}
})
t.Run("close is idempotent", func(t *testing.T) {
out := make(chan *event)
emitter := newTaskEmitterUsingChan(out)
for i := 0; i < 4; i++ {
emitter.Close()
}
})
}
func TestTaskEmitterWrapper(t *testing.T) {
t.Run("emit failureStartup", func(t *testing.T) {
expect := "antani"
collector := &CollectorTaskEmitter{}
emitter := &taskEmitterWrapper{collector}
emitter.EmitFailureStartup(expect)
events := collector.Collect()
if len(events) != 1 {
t.Fatal("invalid number of events")
}
ev := events[0]
if ev.Key != eventTypeFailureStartup {
t.Fatal("invalid key")
}
value := ev.Value.(eventFailure)
if value.Failure != expect {
t.Fatal("invalid failure value")
}
})
t.Run("emit failureGeneric", func(t *testing.T) {
expectName := "mascetti"
expectFailure := "antani"
collector := &CollectorTaskEmitter{}
emitter := &taskEmitterWrapper{collector}
emitter.EmitFailureGeneric(expectName, expectFailure)
events := collector.Collect()
if len(events) != 1 {
t.Fatal("invalid number of events")
}
ev := events[0]
if ev.Key != expectName {
t.Fatal("invalid key")
}
value := ev.Value.(eventFailure)
if value.Failure != expectFailure {
t.Fatal("invalid failure value")
}
})
t.Run("emit statusProgress", func(t *testing.T) {
percentage := 0.66
message := "mascetti"
collector := &CollectorTaskEmitter{}
emitter := &taskEmitterWrapper{collector}
emitter.EmitStatusProgress(percentage, message)
events := collector.Collect()
if len(events) != 1 {
t.Fatal("invalid number of events")
}
ev := events[0]
if ev.Key != eventTypeStatusProgress {
t.Fatal("invalid key")
}
value := ev.Value.(eventStatusProgress)
if value.Percentage != percentage {
t.Fatal("invalid percentage value")
}
if value.Message != message {
t.Fatal("invalid message value")
}
})
}