-
Notifications
You must be signed in to change notification settings - Fork 50
/
callback.go
52 lines (41 loc) · 1.32 KB
/
callback.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
51
52
package retry_handler_sqs
import (
"context"
"fmt"
"sync"
"github.com/justtrackio/gosoline/pkg/funk"
"github.com/justtrackio/gosoline/pkg/test/suite"
)
type Callback struct {
aut suite.AppUnderTest
receivedModels []*DataModel
receivedAttributes []map[string]string
retryCount int
stopAt int
lck sync.Mutex
}
func NewCallback() *Callback {
return &Callback{}
}
func (c *Callback) GetModel(map[string]string) interface{} {
return &DataModel{}
}
func (c *Callback) Consume(_ context.Context, m interface{}, attributes map[string]string) (bool, error) {
c.lck.Lock()
defer c.lck.Unlock()
// clone the attributes and remove the sqs specific ones to make for simpler checks later
// we need to make a clone because deleting the message idea is a Bad Idea™ as it is needed to acknowledge the message
attributes = funk.MergeMaps(attributes)
delete(attributes, "sqsMessageId")
delete(attributes, "sqsReceiptHandle")
model := m.(*DataModel)
c.receivedModels = append(c.receivedModels, model)
c.receivedAttributes = append(c.receivedAttributes, attributes)
if len(c.receivedModels) <= c.retryCount {
return false, fmt.Errorf("something went wrong on consume no %d", len(c.receivedModels))
}
if len(c.receivedModels) == c.stopAt {
c.aut.Stop()
}
return true, nil
}