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:
parent
c4eb682606
commit
120f2b9fbf
6 changed files with 98 additions and 67 deletions
|
|
@ -59,9 +59,11 @@ import (
|
|||
// running as subsequent Tasks to reuse the Session connections
|
||||
// created with the OONI probe services backends.
|
||||
type Task struct {
|
||||
cancel context.CancelFunc
|
||||
isdone *atomicx.Int64
|
||||
out chan *event
|
||||
cancel context.CancelFunc
|
||||
isdone *atomicx.Int64
|
||||
isstarted chan interface{} // for testing
|
||||
isstopped chan interface{} // for testing
|
||||
out chan *event
|
||||
}
|
||||
|
||||
// StartTask starts an asynchronous task. The input argument is a
|
||||
|
|
@ -74,13 +76,17 @@ func StartTask(input string) (*Task, error) {
|
|||
const bufsiz = 128 // common case: we don't want runner to block
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
task := &Task{
|
||||
cancel: cancel,
|
||||
isdone: &atomicx.Int64{},
|
||||
out: make(chan *event, bufsiz),
|
||||
cancel: cancel,
|
||||
isdone: &atomicx.Int64{},
|
||||
isstarted: make(chan interface{}),
|
||||
isstopped: make(chan interface{}),
|
||||
out: make(chan *event, bufsiz),
|
||||
}
|
||||
go func() {
|
||||
close(task.isstarted)
|
||||
run(ctx, &settings, task.out)
|
||||
task.out <- nil // signal that we're done w/o closing the channel
|
||||
close(task.isstopped)
|
||||
}()
|
||||
return task, nil
|
||||
}
|
||||
|
|
@ -107,10 +113,6 @@ func (t *Task) IsDone() bool {
|
|||
return t.isdone.Load() != 0
|
||||
}
|
||||
|
||||
func (t *Task) isRunning() bool {
|
||||
return !t.IsDone()
|
||||
}
|
||||
|
||||
// Interrupt interrupts the task.
|
||||
func (t *Task) Interrupt() {
|
||||
t.cancel()
|
||||
|
|
|
|||
Loading…
Reference in a new issue