Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CWService interface and MockCWService for testing #148

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions aws/cwatch/mock/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package mock

import (
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/nyaruka/gocommon/aws/cwatch"
)

type MockCWService struct {
namespace string
deployment types.Dimension
Batcher []types.MetricDatum

Stopped bool
}

func NewMockCWService(accessKey, secretKey, region, namespace, deployment string) (*MockCWService, error) {
mockCW := MockCWService{
namespace: namespace,
deployment: types.Dimension{Name: aws.String("Deployment"), Value: aws.String(deployment)},
Batcher: nil,
}

return &mockCW, nil

Check warning on line 26 in aws/cwatch/mock/mock.go

View check run for this annotation

Codecov / codecov/patch

aws/cwatch/mock/mock.go#L19-L26

Added lines #L19 - L26 were not covered by tests
}

func (s *MockCWService) Queue(d types.MetricDatum) {
if s.Stopped {
return
}
s.Batcher = append(s.Batcher, d)

Check warning on line 33 in aws/cwatch/mock/mock.go

View check run for this annotation

Codecov / codecov/patch

aws/cwatch/mock/mock.go#L29-L33

Added lines #L29 - L33 were not covered by tests
}

func (s *MockCWService) StartQueue(wg *sync.WaitGroup) {
s.Batcher = []types.MetricDatum{}
s.Stopped = false

Check warning on line 38 in aws/cwatch/mock/mock.go

View check run for this annotation

Codecov / codecov/patch

aws/cwatch/mock/mock.go#L36-L38

Added lines #L36 - L38 were not covered by tests
}

func (s *MockCWService) StopQueue() {
s.Stopped = true

Check warning on line 42 in aws/cwatch/mock/mock.go

View check run for this annotation

Codecov / codecov/patch

aws/cwatch/mock/mock.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}

var _ cwatch.CWService = (*MockCWService)(nil)
8 changes: 8 additions & 0 deletions aws/cwatch/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ func (s *Service) processBatch(batch []types.MetricDatum) {
slog.Error("error sending metrics to cloudwatch", "error", err, "count", len(batch))
}
}

type CWService interface {
Queue(d types.MetricDatum)
StartQueue(wg *sync.WaitGroup)
StopQueue()
}

var _ CWService = (*Service)(nil)
Loading