Skip to content

Commit

Permalink
Beanstalk client test using local beanstalk in the test image
Browse files Browse the repository at this point in the history
  • Loading branch information
alok87 committed Jun 17, 2020
1 parent 243d836 commit 18dd462
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ IMAGE := $(REGISTRY)/$(BIN)
TAG := $(VERSION)

BUILD_IMAGE ?= golang:1.14.2-alpine
TEST_IMAGE ?= practodev/golang:1.14.2-alpine-test

# If you want to build all binaries, see the 'all-build' rule.
# If you want to build all containers, see the 'all-container' rule.
Expand Down Expand Up @@ -177,7 +178,7 @@ test: $(BUILD_DIRS)
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
$(BUILD_IMAGE) \
$(TEST_IMAGE) \
/bin/sh -c " \
ARCH=$(ARCH) \
OS=$(OS) \
Expand Down
2 changes: 1 addition & 1 deletion hack/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export GOFLAGS="-mod=vendor"
TARGETS=$(for d in "$@"; do echo ./$d/...; done)

echo "Running tests:"
go test -cover -installsuffix "static" ${TARGETS}
go test -v -cover -installsuffix "static" ${TARGETS}
# go test -cover -v -installsuffix "static" ./pkg/queue/...
# exit 0

Expand Down
60 changes: 50 additions & 10 deletions pkg/queue/beanstalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package queue

import (
"k8s.io/klog"
"os/exec"
"testing"
"time"

"github.com/golang/mock/gomock"
// _ "github.com/golang/mock/mockgen"
"github.com/beanstalkd/go-beanstalk"
"github.com/practo/k8s-worker-pod-autoscaler/pkg/signals"
)

Expand All @@ -16,7 +16,7 @@ var (
)

const (
localBeanstalkHost = "host.docker.internal"
localBeanstalkHost = "localhost"
)

func init() {
Expand Down Expand Up @@ -337,18 +337,54 @@ func TestPollSyncWhenNoMessagesInQueueAndNoMessagesAreInFlight(t *testing.T) {
}
}

func TestBeanstalkClientGetStats(t *testing.T) {
func runBeanstalkdProcess(
startCh chan bool, killCh chan bool, doneCh chan bool) {

cmd := exec.Command("beanstalkd", "-l", "localhost", "-p", "11300")
if err := cmd.Start(); err != nil {
klog.Errorf("Error starting the beanstald process: %v\n", err)
return
}
klog.Info("Started local beanstalkd process")
startCh <- true
for {
switch {
case <-killCh:
klog.Info("Killing beanstalkd process")
if err := cmd.Process.Kill(); err != nil {
klog.Errorf("Error killing the beanstalkd process: %v\n", err)
return
}
klog.Info("Killed beanstalkd.")
doneCh <- true
return
}
}
}

func TestBeanstalkClient(t *testing.T) {
startCh := make(chan bool)
killCh := make(chan bool)
doneCh := make(chan bool)
go runBeanstalkdProcess(startCh, killCh, doneCh)
<-startCh

queueName := "otpsender"
queueURI := getQueueURI("", queueName)

beastalkClient, err := NewBeanstalkClient(queueURI)
e, ok := err.(beanstalk.ConnError)
if ok && e.Err != beanstalk.ErrNotFound {
t.Skipf("Skipping, connection %v:11300 failed", localBeanstalkHost)
for connect := 0; err != nil && connect < 3; connect++ {
beastalkClient, err = NewBeanstalkClient(queueURI)
if err != nil {
klog.Warningf("Retrying connection to local beanstalk")
time.Sleep(1 * time.Second)
continue
}
}
if err != nil {
t.Errorf("Failed to connect to %v:11300", localBeanstalkHost)
return
}

klog.Info("Note: Testing locally require beanstalkd restart " +
" in every `make test` invocation. Tests fail if this is not done." +
" MAC Users: `brew services restart beanstalkd && make test`")

jobsWaiting, idleWorkers, jobsReserved, err := beastalkClient.getStats()
if err != nil {
Expand Down Expand Up @@ -384,4 +420,8 @@ func TestBeanstalkClientGetStats(t *testing.T) {
"jobsReserved=0,"+
"got=(%v, %v, %v) resp.\n", jobsWaiting, idleWorkers, jobsReserved)
}

killCh <- true
<-doneCh
klog.Info("Beanstalkd process gracefully shutdown, ending test.")
}

0 comments on commit 18dd462

Please sign in to comment.