2021-11-26 20:21:42 +01:00
|
|
|
package oonimkall
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2021-11-26 20:21:42 +01:00
|
|
|
import "testing"
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
func TestDisabledEvents(t *testing.T) {
|
2021-11-26 20:21:42 +01:00
|
|
|
out := make(chan *event)
|
2021-12-01 15:40:25 +01:00
|
|
|
eof := make(chan interface{})
|
|
|
|
emitter := newEventEmitter([]string{"log"}, out, eof)
|
2021-02-02 12:05:47 +01:00
|
|
|
go func() {
|
2021-11-26 20:21:42 +01:00
|
|
|
emitter.Emit("log", eventLog{Message: "foo"})
|
2021-12-01 15:40:25 +01:00
|
|
|
close(eof)
|
2021-02-02 12:05:47 +01:00
|
|
|
}()
|
|
|
|
var count int64
|
2021-12-01 15:40:25 +01:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-out:
|
|
|
|
if ev.Key == "log" {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
case <-eof:
|
|
|
|
break Loop
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if count > 0 {
|
|
|
|
t.Fatal("cannot disable events")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitFailureStartup(t *testing.T) {
|
2021-11-26 20:21:42 +01:00
|
|
|
out := make(chan *event)
|
2021-12-01 15:40:25 +01:00
|
|
|
eof := make(chan interface{})
|
|
|
|
emitter := newEventEmitter([]string{}, out, eof)
|
2021-02-02 12:05:47 +01:00
|
|
|
go func() {
|
|
|
|
emitter.EmitFailureStartup("mocked error")
|
2021-12-01 15:40:25 +01:00
|
|
|
close(eof)
|
2021-02-02 12:05:47 +01:00
|
|
|
}()
|
|
|
|
var found bool
|
2021-12-01 15:40:25 +01:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-out:
|
|
|
|
if ev.Key == "failure.startup" {
|
|
|
|
evv := ev.Value.(eventFailure) // panic if not castable
|
|
|
|
if evv.Failure == "mocked error" {
|
|
|
|
found = true
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-12-01 15:40:25 +01:00
|
|
|
case <-eof:
|
|
|
|
break Loop
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatal("did not see expected event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmitStatusProgress(t *testing.T) {
|
2021-11-26 20:21:42 +01:00
|
|
|
out := make(chan *event)
|
2021-12-01 15:40:25 +01:00
|
|
|
eof := make(chan interface{})
|
|
|
|
emitter := newEventEmitter([]string{}, out, eof)
|
2021-02-02 12:05:47 +01:00
|
|
|
go func() {
|
|
|
|
emitter.EmitStatusProgress(0.7, "foo")
|
2021-12-01 15:40:25 +01:00
|
|
|
close(eof)
|
2021-02-02 12:05:47 +01:00
|
|
|
}()
|
|
|
|
var found bool
|
2021-12-01 15:40:25 +01:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-out:
|
|
|
|
if ev.Key == "status.progress" {
|
|
|
|
evv := ev.Value.(eventStatusProgress) // panic if not castable
|
|
|
|
if evv.Message == "foo" && evv.Percentage == 0.7 {
|
|
|
|
found = true
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-12-01 15:40:25 +01:00
|
|
|
case <-eof:
|
|
|
|
break Loop
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Fatal("did not see expected event")
|
|
|
|
}
|
|
|
|
}
|