ooni-probe-cli/pkg/oonimkall/eventemitter_test.go
Simone Basso ee5be24900
[forwardport] refactor(oonimkall): merge internal/task into oonimkall (#617) (#618)
This diff forward ports bc4b9f1ea89158bfa7b7a80ae59a90b43c784ed2 to `master`.

See https://github.com/ooni/probe/issues/1903
2021-11-26 20:21:42 +01:00

64 lines
1.3 KiB
Go

package oonimkall
import "testing"
func TestDisabledEvents(t *testing.T) {
out := make(chan *event)
emitter := newEventEmitter([]string{"log"}, out)
go func() {
emitter.Emit("log", eventLog{Message: "foo"})
close(out)
}()
var count int64
for ev := range out {
if ev.Key == "log" {
count++
}
}
if count > 0 {
t.Fatal("cannot disable events")
}
}
func TestEmitFailureStartup(t *testing.T) {
out := make(chan *event)
emitter := newEventEmitter([]string{}, out)
go func() {
emitter.EmitFailureStartup("mocked error")
close(out)
}()
var found bool
for ev := range out {
if ev.Key == "failure.startup" {
evv := ev.Value.(eventFailure) // panic if not castable
if evv.Failure == "mocked error" {
found = true
}
}
}
if !found {
t.Fatal("did not see expected event")
}
}
func TestEmitStatusProgress(t *testing.T) {
out := make(chan *event)
emitter := newEventEmitter([]string{}, out)
go func() {
emitter.EmitStatusProgress(0.7, "foo")
close(out)
}()
var found bool
for ev := range out {
if ev.Key == "status.progress" {
evv := ev.Value.(eventStatusProgress) // panic if not castable
if evv.Message == "foo" && evv.Percentage == 0.7 {
found = true
}
}
}
if !found {
t.Fatal("did not see expected event")
}
}