Skip to content

Commit

Permalink
Encapsulated and mocked the external dependency
Browse files Browse the repository at this point in the history
Reason: Pave the way for unit tests
  • Loading branch information
alok87 committed Jun 13, 2020
1 parent 839e8b6 commit 351af55
Show file tree
Hide file tree
Showing 19 changed files with 3,400 additions and 25 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/aws/aws-sdk-go v1.29.15
github.com/beanstalkd/go-beanstalk v0.0.0-20200526060843-1cc502ecaf3c
github.com/golang/mock v1.4.3 // indirect
github.com/golang/mock v1.4.3
github.com/mitchellh/go-homedir v1.1.0
github.com/prometheus/client_golang v0.9.3
github.com/spf13/cobra v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
rsc.io/quote/v3 v3.1.0 h1:9JKUTTIUgS6kzR9mK1YuGKv6Nl+DijDNIc0ghT58FaY=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/QiW4=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/structured-merge-diff v0.0.0-20190302045857-e85c7b244fd2/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
Expand Down
10 changes: 5 additions & 5 deletions pkg/queue/beanstalk.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package queue

import (
"errors"
"net/url"
"path"
"strconv"
"sync"
"time"
"errors"

"github.com/beanstalkd/go-beanstalk"
"k8s.io/klog"
Expand All @@ -15,8 +15,8 @@ import (
// Beanstalk is used to by the Poller to get the queue
// information from Beanstalk, it implements the QueuingService interface
type Beanstalk struct {
queues *Queues
clientPool *sync.Map
queues *Queues
clientPool *sync.Map

shortPollInterval time.Duration
longPollInterval int64
Expand All @@ -28,8 +28,8 @@ func NewBeanstalk(
longPollInterval int) (QueuingService, error) {

return &Beanstalk{
queues: queues,
clientPool: new(sync.Map),
queues: queues,
clientPool: new(sync.Map),

shortPollInterval: time.Second * time.Duration(shortPollInterval),
longPollInterval: int64(longPollInterval),
Expand Down
66 changes: 66 additions & 0 deletions pkg/queue/beanstalk_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 14 additions & 19 deletions pkg/queue/beanstalk_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package queue

import (
"testing"
_ "github.com/golang/mock/gomock"
_ "github.com/golang/mock/mockgen"
"testing"
)

func getTestBeanstalk() (QueuingService) {
q := NewQueues()

q.Add(
"namespace1",
"queuename1",
"beanstalk://beanstalkd.namespace1:11300/dev-queuename1",
)

q.Add(
"namespace2",
"queuename2",
"beanstalk://beanstalkd.namespace2:11300/dev-queuename2",
)

return NewBeanstalk(q, 10, 10)
}
const (
queueNameOne = "q1"
)

func TestPoll(t *testing.T) {
func newTestQueues() *Queues {
q := NewQueues()

q.Add(
"namespace1",
"queuename1",
"beanstalk://beanstalkd.namespace1:11300/"+queueNameOne,
)
return q
}
12 changes: 12 additions & 0 deletions vendor/github.com/golang/mock/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions vendor/github.com/golang/mock/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 351af55

Please sign in to comment.