-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper_test.go
97 lines (77 loc) · 2.64 KB
/
wrapper_test.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
package wrappercloudwatchlogs
import (
"ClientCloudwatchLogs/mocks"
"context"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/stretchr/testify/mock"
"strings"
"testing"
"time"
)
const (
logsPerBatch = 4
batchSize = 2048
)
func TestCreateClient(t *testing.T) {
opts := WrapperOptions{
logGroup: "test-group",
logStream: "my-stream",
batchSize: batchSize,
logsPerBatch: logsPerBatch,
sendAfter: time.Second * 10,
retentionDays: OneMonth,
}
ctx := context.Background()
clientAwsMock := getClientMock(t)
t.Run("after sending a full batch + 1 extra log, the internal buffer should have 1 item", func(t *testing.T) {
wrapper, err := New(ctx, clientAwsMock, &opts)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
msg := "Hello!"
logEvent := types.InputLogEvent{Message: &msg}
for i := 0; i < logsPerBatch; i++ {
wrapper.addLogEvent(ctx, logEvent)
}
if len(wrapper.bufferLogEvents) != 0 {
t.Errorf("buffer should be empty")
}
// when the buffer is full, should send the data and clear the buffer
wrapper.addLogEvent(ctx, logEvent)
if len(wrapper.bufferLogEvents) != 1 {
t.Errorf("buffer should have only one item")
}
})
t.Run("should send the data before exceeding the batchSize", func(t *testing.T) {
wrapper, err := New(ctx, clientAwsMock, &opts)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
longString := strings.Repeat("a", 1024/4)
logEvent := types.InputLogEvent{Message: &longString}
wrapper.addLogEvent(ctx, logEvent)
wrapper.addLogEvent(ctx, logEvent)
// following log doesn't fit in the batchSize, so previous ones are sent
wrapper.addLogEvent(ctx, logEvent)
if len(wrapper.bufferLogEvents) != 1 {
t.Errorf("Buffer should have one element")
}
})
}
func getClientMock(t *testing.T) CreateOperationsCloudwatch {
clientMock := mocks.NewCreateOperationsCloudwatch(t)
clientMock.EXPECT().
CreateLogGroup(mock.Anything, mock.AnythingOfType("*cloudwatchlogs.CreateLogGroupInput")).
Return(&cloudwatchlogs.CreateLogGroupOutput{}, nil)
clientMock.EXPECT().
CreateLogStream(mock.Anything, mock.AnythingOfType("*cloudwatchlogs.CreateLogStreamInput")).
Return(&cloudwatchlogs.CreateLogStreamOutput{}, nil)
clientMock.EXPECT().
PutLogEvents(mock.Anything, mock.AnythingOfType("*cloudwatchlogs.PutLogEventsInput")).
Return(&cloudwatchlogs.PutLogEventsOutput{}, nil)
clientMock.EXPECT().
PutRetentionPolicy(mock.Anything, mock.AnythingOfType("*cloudwatchlogs.PutRetentionPolicyInput")).
Return(&cloudwatchlogs.PutRetentionPolicyOutput{}, nil)
return clientMock
}