-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.go
109 lines (91 loc) · 2.73 KB
/
mock.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package core
import (
"context"
"errors"
"sync"
"time"
awsevents "github.com/aws/aws-lambda-go/events"
"github.com/golang/protobuf/proto"
utils "github.com/tommzn/go-utils"
events "github.com/tommzn/hdb-events-go"
)
// Mocked datasource which retruns a dummy event.
type dataSourceMock struct {
shouldReturnError bool
}
// Fetch returns a dummy event for testing.
func (mock *dataSourceMock) Fetch() (proto.Message, error) {
if mock.shouldReturnError {
return nil, errors.New("Unable to fetch data.")
}
return &events.Dummy{Id: utils.NewId()}, nil
}
// newDataSourceMock creates a new mocked datasource which returns always a new Dummy event.
func newDataSourceMock() DataSource {
return &dataSourceMock{shouldReturnError: false}
}
// newDataSourceMockWithError creates a new mocked datasource which returns always an error.
func newDataSourceMockWithError() DataSource {
return &dataSourceMock{shouldReturnError: true}
}
// publisherMock can be used to mock AWS SQS publisher for testing.
type publisherMock struct {
shouldFail bool
messageCount int
}
// Counts calls to send message methods and returns a new message id. If you pass "error" as queue name it will returns with
// an error and doesn't count this call.
func (mock *publisherMock) Send(message proto.Message) error {
if mock.shouldFail {
return errors.New("Unable to send message.")
}
mock.messageCount += 2
return nil
}
// newPublisherMock returns a new mock for a AWS SQS publisher.
func newPublisherMock() Publisher {
return &publisherMock{shouldFail: false, messageCount: 0}
}
// s3EventProcessorMock can be used for testing, processing will aalways succeed.
type s3EventProcessorMock struct {
}
// ProcessEvent will return always no message and no error.
func (mock *s3EventProcessorMock) ProcessEvent(entity awsevents.S3Entity, content []byte) (proto.Message, error) {
return nil, nil
}
// newS3EventProcessorMock returns a newS3 event processor mock for testing.
func newS3EventProcessorMock() S3EventProcessor {
return &s3EventProcessorMock{}
}
// collectorMock is used to test continuous collector.
type collectorMock struct {
counter int
hasFinished bool
}
// Run will increade a counter every 100 milliseconds and stops execution
// if context has been canceled.
func (mock *collectorMock) Run(ctx context.Context) error {
stopChan := make(chan bool, 1)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
mock.counter++
time.Sleep(100 * time.Millisecond)
if len(stopChan) > 0 {
return
}
}()
<-ctx.Done()
stopChan <- true
wg.Wait()
mock.hasFinished = true
return nil
}
// newCollectorMock returns a new collector mock for testing.
func newCollectorMock() Collector {
return &collectorMock{
counter: 0,
hasFinished: false,
}
}