-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge_helpers.go
50 lines (42 loc) · 1.09 KB
/
bridge_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package opinionatedevents
import (
"context"
"errors"
"fmt"
"time"
)
type testDestinationHandler = func(ctx context.Context, batch []*Message) error
type testDestination struct {
handlers []testDestinationHandler
}
func newTestDestination() *testDestination {
return &testDestination{handlers: []testDestinationHandler{}}
}
func (d *testDestination) Deliver(ctx context.Context, batch []*Message) error {
if handler, err := d.nextHandler(); err != nil {
return err
} else {
return handler(ctx, batch)
}
}
func (d *testDestination) nextHandler() (testDestinationHandler, error) {
if len(d.handlers) == 0 {
return nil, fmt.Errorf("no handlers left")
}
handler := d.handlers[0]
d.handlers = d.handlers[1:]
return handler, nil
}
func (d *testDestination) pushHandler(handler testDestinationHandler) {
d.handlers = append(d.handlers, handler)
}
func waitForSuccessEnvelope(envelope *envelope) error {
select {
case <-envelope.onSuccess():
return nil
case <-envelope.onFailure():
return errors.New("failed")
case <-time.After(1 * time.Second):
return errors.New("timeout reached")
}
}