1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2026-01-28 05:55:56 +00:00

extension: split out an async specific broker for "after" events (#346)

Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
James Hillyerd
2023-02-16 16:17:06 -08:00
committed by GitHub
parent e1b8996412
commit 36095a2cdf
8 changed files with 205 additions and 71 deletions

View File

@@ -1,9 +1,7 @@
package extension
import (
"errors"
"sync"
"time"
)
// EventBroker maintains a list of listeners interested in a specific type
@@ -59,38 +57,3 @@ func (eb *EventBroker[E, R]) lockedRemoveListener(name string) {
}
}
}
// AsyncTestListener returns a func that will wait for an event and return it, or timeout
// with an error.
func (eb *EventBroker[E, R]) AsyncTestListener(capacity int) func() (*E, error) {
const name = "asyncTestListener"
// Send event down channel.
events := make(chan E, capacity)
eb.AddListener(name,
func(msg E) *R {
events <- msg
return nil
})
count := 0
return func() (*E, error) {
count++
defer func() {
if count >= capacity {
eb.RemoveListener(name)
close(events)
}
}()
select {
case event := <-events:
return &event, nil
case <-time.After(time.Second * 2):
return nil, errors.New("Timeout waiting for event")
}
}
}