This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Recording is a test Publisher that records events synchronously.
|
||||
type Recording struct {
|
||||
mu sync.Mutex
|
||||
evs []any
|
||||
}
|
||||
|
||||
// Publish appends ev.
|
||||
func (r *Recording) Publish(_ context.Context, ev any) {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.evs = append(r.evs, ev)
|
||||
}
|
||||
|
||||
// Len returns the number of recorded events.
|
||||
func (r *Recording) Len() int {
|
||||
if r == nil {
|
||||
return 0
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return len(r.evs)
|
||||
}
|
||||
|
||||
// Snapshot returns a copy of recorded events.
|
||||
func (r *Recording) Snapshot() []any {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
out := make([]any, len(r.evs))
|
||||
copy(out, r.evs)
|
||||
return out
|
||||
}
|
||||
Reference in New Issue
Block a user