fix(oonimkall): improve channel management pattern (#621)

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`.
This commit is contained in:
Simone Basso 2021-12-01 15:40:25 +01:00 committed by GitHub
parent c4eb682606
commit 120f2b9fbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 67 deletions

View File

@ -1,16 +1,16 @@
package oonimkall package oonimkall
import "time"
// eventEmitter emits event on a channel // eventEmitter emits event on a channel
type eventEmitter struct { type eventEmitter struct {
disabled map[string]bool disabled map[string]bool
eof <-chan interface{}
out chan<- *event out chan<- *event
} }
// newEventEmitter creates a new Emitter // newEventEmitter creates a new Emitter
func newEventEmitter(disabledEvents []string, out chan<- *event) *eventEmitter { func newEventEmitter(disabledEvents []string, out chan<- *event,
ee := &eventEmitter{out: out} eof <-chan interface{}) *eventEmitter {
ee := &eventEmitter{eof: eof, out: out}
ee.disabled = make(map[string]bool) ee.disabled = make(map[string]bool)
for _, eventname := range disabledEvents { for _, eventname := range disabledEvents {
ee.disabled[eventname] = true ee.disabled[eventname] = true
@ -38,13 +38,10 @@ func (ee *eventEmitter) Emit(key string, value interface{}) {
if ee.disabled[key] { if ee.disabled[key] {
return return
} }
const maxSendTimeout = 250 * time.Millisecond // Prevent this goroutine from blocking on `ee.out` if the caller
timer := time.NewTimer(maxSendTimeout) // has already told us it's not going to accept more events.
defer timer.Stop()
select { select {
case ee.out <- &event{Key: key, Value: value}: case ee.out <- &event{Key: key, Value: value}:
// good, we've been able to send the new event case <-ee.eof:
case <-timer.C:
// oops, we've timed out sending
} }
} }

View File

@ -4,16 +4,23 @@ import "testing"
func TestDisabledEvents(t *testing.T) { func TestDisabledEvents(t *testing.T) {
out := make(chan *event) out := make(chan *event)
emitter := newEventEmitter([]string{"log"}, out) eof := make(chan interface{})
emitter := newEventEmitter([]string{"log"}, out, eof)
go func() { go func() {
emitter.Emit("log", eventLog{Message: "foo"}) emitter.Emit("log", eventLog{Message: "foo"})
close(out) close(eof)
}() }()
var count int64 var count int64
for ev := range out { Loop:
for {
select {
case ev := <-out:
if ev.Key == "log" { if ev.Key == "log" {
count++ count++
} }
case <-eof:
break Loop
}
} }
if count > 0 { if count > 0 {
t.Fatal("cannot disable events") t.Fatal("cannot disable events")
@ -22,19 +29,26 @@ func TestDisabledEvents(t *testing.T) {
func TestEmitFailureStartup(t *testing.T) { func TestEmitFailureStartup(t *testing.T) {
out := make(chan *event) out := make(chan *event)
emitter := newEventEmitter([]string{}, out) eof := make(chan interface{})
emitter := newEventEmitter([]string{}, out, eof)
go func() { go func() {
emitter.EmitFailureStartup("mocked error") emitter.EmitFailureStartup("mocked error")
close(out) close(eof)
}() }()
var found bool var found bool
for ev := range out { Loop:
for {
select {
case ev := <-out:
if ev.Key == "failure.startup" { if ev.Key == "failure.startup" {
evv := ev.Value.(eventFailure) // panic if not castable evv := ev.Value.(eventFailure) // panic if not castable
if evv.Failure == "mocked error" { if evv.Failure == "mocked error" {
found = true found = true
} }
} }
case <-eof:
break Loop
}
} }
if !found { if !found {
t.Fatal("did not see expected event") t.Fatal("did not see expected event")
@ -43,19 +57,26 @@ func TestEmitFailureStartup(t *testing.T) {
func TestEmitStatusProgress(t *testing.T) { func TestEmitStatusProgress(t *testing.T) {
out := make(chan *event) out := make(chan *event)
emitter := newEventEmitter([]string{}, out) eof := make(chan interface{})
emitter := newEventEmitter([]string{}, out, eof)
go func() { go func() {
emitter.EmitStatusProgress(0.7, "foo") emitter.EmitStatusProgress(0.7, "foo")
close(out) close(eof)
}() }()
var found bool var found bool
for ev := range out { Loop:
for {
select {
case ev := <-out:
if ev.Key == "status.progress" { if ev.Key == "status.progress" {
evv := ev.Value.(eventStatusProgress) // panic if not castable evv := ev.Value.(eventStatusProgress) // panic if not castable
if evv.Message == "foo" && evv.Percentage == 0.7 { if evv.Message == "foo" && evv.Percentage == 0.7 {
found = true found = true
} }
} }
case <-eof:
break Loop
}
} }
if !found { if !found {
t.Fatal("did not see expected event") t.Fatal("did not see expected event")

View File

@ -38,7 +38,9 @@ const (
// run runs the task specified by settings.Name until completion. This is the // run runs the task specified by settings.Name until completion. This is the
// top-level API that should be called by oonimkall. // top-level API that should be called by oonimkall.
func run(ctx context.Context, settings *settings, out chan<- *event) { func run(ctx context.Context, settings *settings, out chan<- *event) {
r := newRunner(settings, out) eof := make(chan interface{})
defer close(eof) // tell the emitter to not emit anymore.
r := newRunner(settings, out, eof)
r.Run(ctx) r.Run(ctx)
} }
@ -51,9 +53,9 @@ type runner struct {
} }
// newRunner creates a new task runner // newRunner creates a new task runner
func newRunner(settings *settings, out chan<- *event) *runner { func newRunner(settings *settings, out chan<- *event, eof <-chan interface{}) *runner {
return &runner{ return &runner{
emitter: newEventEmitter(settings.DisabledEvents, out), emitter: newEventEmitter(settings.DisabledEvents, out, eof),
out: out, out: out,
settings: settings, settings: settings,
} }

View File

@ -44,9 +44,13 @@ func TestRunnerMaybeLookupLocationFailure(t *testing.T) {
Version: 1, Version: 1,
} }
seench := make(chan int64) seench := make(chan int64)
eof := make(chan interface{})
go func() { go func() {
var seen int64 var seen int64
for ev := range out { Loop:
for {
select {
case ev := <-out:
switch ev.Key { switch ev.Key {
case "failure.ip_lookup", "failure.asn_lookup", case "failure.ip_lookup", "failure.asn_lookup",
"failure.cc_lookup", "failure.resolver_lookup": "failure.cc_lookup", "failure.resolver_lookup":
@ -60,16 +64,19 @@ func TestRunnerMaybeLookupLocationFailure(t *testing.T) {
default: default:
panic(fmt.Sprintf("unexpected key: %s - %+v", ev.Key, ev.Value)) panic(fmt.Sprintf("unexpected key: %s - %+v", ev.Key, ev.Value))
} }
case <-eof:
break Loop
}
} }
seench <- seen seench <- seen
}() }()
expected := errors.New("mocked error") expected := errors.New("mocked error")
r := newRunner(settings, out) r := newRunner(settings, out, eof)
r.maybeLookupLocation = func(*engine.Session) error { r.maybeLookupLocation = func(*engine.Session) error {
return expected return expected
} }
r.Run(context.Background()) r.Run(context.Background())
close(out) close(eof)
if n := <-seench; n != 4 { if n := <-seench; n != 4 {
t.Fatal("unexpected number of events") t.Fatal("unexpected number of events")
} }

View File

@ -61,6 +61,8 @@ import (
type Task struct { type Task struct {
cancel context.CancelFunc cancel context.CancelFunc
isdone *atomicx.Int64 isdone *atomicx.Int64
isstarted chan interface{} // for testing
isstopped chan interface{} // for testing
out chan *event out chan *event
} }
@ -76,11 +78,15 @@ func StartTask(input string) (*Task, error) {
task := &Task{ task := &Task{
cancel: cancel, cancel: cancel,
isdone: &atomicx.Int64{}, isdone: &atomicx.Int64{},
isstarted: make(chan interface{}),
isstopped: make(chan interface{}),
out: make(chan *event, bufsiz), out: make(chan *event, bufsiz),
} }
go func() { go func() {
close(task.isstarted)
run(ctx, &settings, task.out) run(ctx, &settings, task.out)
task.out <- nil // signal that we're done w/o closing the channel task.out <- nil // signal that we're done w/o closing the channel
close(task.isstopped)
}() }()
return task, nil return task, nil
} }
@ -107,10 +113,6 @@ func (t *Task) IsDone() bool {
return t.isdone.Load() != 0 return t.isdone.Load() != 0
} }
func (t *Task) isRunning() bool {
return !t.IsDone()
}
// Interrupt interrupts the task. // Interrupt interrupts the task.
func (t *Task) Interrupt() { func (t *Task) Interrupt() {
t.cancel() t.cancel()

View File

@ -290,7 +290,7 @@ func TestInterruptExampleWithInput(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("skip test in short mode") t.Skip("skip test in short mode")
} }
t.Skip("Skipping broken test; see https://github.com/ooni/probe-cli/v3/internal/engine/issues/992") t.Skip("Skipping broken test; see https://github.com/ooni/probe-engine/issues/992")
task, err := StartTask(`{ task, err := StartTask(`{
"assets_dir": "../testdata/oonimkall/assets", "assets_dir": "../testdata/oonimkall/assets",
"inputs": [ "inputs": [
@ -494,7 +494,9 @@ func TestPrivacyAndScrubbing(t *testing.T) {
} }
} }
func TestNonblock(t *testing.T) { func TestNonblockWithFewEvents(t *testing.T) {
// This test tests whether we won't block for a small
// number of events emitted by the task
if testing.Short() { if testing.Short() {
t.Skip("skip test in short mode") t.Skip("skip test in short mode")
} }
@ -511,16 +513,16 @@ func TestNonblock(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !task.isRunning() { // Wait for the task thread to start
t.Fatal("The runner should be running at this point") <-task.isstarted
} // Wait for the task thread to complete
// If the task blocks because it emits too much events, this test <-task.isstopped
// will run forever and will be killed. Because we have room for up var count int
// to 128 events in the buffer, we should hopefully be fine.
for task.isRunning() {
time.Sleep(time.Second)
}
for !task.IsDone() { for !task.IsDone() {
task.WaitForNextEvent() task.WaitForNextEvent()
count++
}
if count < 5 {
t.Fatal("too few events")
} }
} }