120f2b9fbf
1. add eof channel to event emitter and use this channel as signal that we shouldn't be sending anymore instead of using a pattern where we use a timer to decide sending has timed out (because we're using a buffered channel, it is still possible for some evetns to end up in the channel if there is space, but this is not a concern, because the events will be deleted when the channel itself is gone); 2. refactor all tests where we assumed the output channel was closed to actually use a parallel "eof" channel and use it as signal we should not be sending anymore (not strictly required but still the right thing to do in terms of using consistent patterns); 3. modify how we construct a runner so that it passes to the event emitter an "eof" channel and closes this channel when the main goroutine running the task is terminating; 4. modify the task to signal events such as "task goroutine started" and "task goroutine stopped" using channels, which helps to write much more correct tests; 5. take advantage of the previous change to improve the test that ensures we're not blocking for a small number of events and also improve the name of such a test to reflect what it's testing. The related issue in term of fixing the channel usage pattern is https://github.com/ooni/probe/issues/1438. Regarding improving testability, instead, the correct reference issue is https://github.com/ooni/probe/issues/1903. There are possibly more changes to apply here to improve this package and its testability, but let's land this diff first and then see how much time is left for further improvements. I've run unit and integration tests with `-race` locally. This diff will need to be backported to `release/3.11`.
85 lines
1.5 KiB
Go
85 lines
1.5 KiB
Go
package oonimkall
|
|
|
|
import "testing"
|
|
|
|
func TestDisabledEvents(t *testing.T) {
|
|
out := make(chan *event)
|
|
eof := make(chan interface{})
|
|
emitter := newEventEmitter([]string{"log"}, out, eof)
|
|
go func() {
|
|
emitter.Emit("log", eventLog{Message: "foo"})
|
|
close(eof)
|
|
}()
|
|
var count int64
|
|
Loop:
|
|
for {
|
|
select {
|
|
case ev := <-out:
|
|
if ev.Key == "log" {
|
|
count++
|
|
}
|
|
case <-eof:
|
|
break Loop
|
|
}
|
|
}
|
|
if count > 0 {
|
|
t.Fatal("cannot disable events")
|
|
}
|
|
}
|
|
|
|
func TestEmitFailureStartup(t *testing.T) {
|
|
out := make(chan *event)
|
|
eof := make(chan interface{})
|
|
emitter := newEventEmitter([]string{}, out, eof)
|
|
go func() {
|
|
emitter.EmitFailureStartup("mocked error")
|
|
close(eof)
|
|
}()
|
|
var found bool
|
|
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
|
|
}
|
|
}
|
|
case <-eof:
|
|
break Loop
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("did not see expected event")
|
|
}
|
|
}
|
|
|
|
func TestEmitStatusProgress(t *testing.T) {
|
|
out := make(chan *event)
|
|
eof := make(chan interface{})
|
|
emitter := newEventEmitter([]string{}, out, eof)
|
|
go func() {
|
|
emitter.EmitStatusProgress(0.7, "foo")
|
|
close(eof)
|
|
}()
|
|
var found bool
|
|
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
|
|
}
|
|
}
|
|
case <-eof:
|
|
break Loop
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("did not see expected event")
|
|
}
|
|
}
|