From 31168dea3e52b6b00499b4fc0c6a39ed0ea0f858 Mon Sep 17 00:00:00 2001 From: Volodymyr Stashchenko Date: Wed, 20 May 2020 21:15:13 +0300 Subject: [PATCH] Add workflows badge --- .github/workflows/go.yml | 105 +++++++++++++++++++++++++++------------ README.md | 5 ++ cmd/main.go | 2 +- message_queue_test.go | 9 ++-- worker.go | 2 +- 5 files changed, 87 insertions(+), 36 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index d31e87f..5837320 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -1,37 +1,80 @@ -name: Go - -on: - push: - branches: [ master ] - pull_request: - branches: [ master ] - +on: [push, pull_request] +name: test and build jobs: - - build: - name: Build + lint: runs-on: ubuntu-latest steps: + - name: Install Go + uses: actions/setup-go@v1 + with: + go-version: 1.13.x + - name: Checkout code + uses: actions/checkout@v1 + - name: Install golangci-lint + run: | + go get github.com/golangci/golangci-lint/cmd/golangci-lint + - name: Run linters + run: | + export PATH=$PATH:$(go env GOPATH)/bin && golangci-lint --tests=false -E bodyclose,misspell,gocyclo,dupl,gofmt,golint,unconvert,depguard,gocritic,funlen,interfacer run - - name: Set up Go 1.x - uses: actions/setup-go@v2 - with: - go-version: ^1.13 - id: go - - - name: Check out code into the Go module directory - uses: actions/checkout@v2 - - - name: Get dependencies - run: | - go get -v -t -d ./... - if [ -f Gopkg.toml ]; then - curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh - dep ensure - fi + test: + strategy: + matrix: + go-version: [1.12.x, 1.13.x] + platform: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.platform }} + steps: + - name: Install Go + if: success() + uses: actions/setup-go@v1 + with: + go-version: ${{ matrix.go-version }} + - name: Checkout code + uses: actions/checkout@v1 + - name: Run tests + run: go test -v -covermode=count - - name: Build - run: go build -v . + coverage: + runs-on: ubuntu-latest + steps: + - name: Install Go + if: success() + uses: actions/setup-go@v1 + with: + go-version: 1.13.x + - name: Checkout code + uses: actions/checkout@v1 + - name: Calc coverage + run: | + export PATH=$PATH:$(go env GOPATH)/bin + go test -v -covermode=count -coverprofile=coverage.out + - name: Convert coverage to lcov + uses: jandelgado/gcov2lcov-action@v1.0.0 + with: + infile: coverage.out + outfile: coverage.lcov + - name: Coveralls + uses: coverallsapp/github-action@v1.0.1 + with: + github-token: ${{ secrets.github_token }} + path-to-lcov: coverage.lcov - - name: Test - run: go test -v . + build: + runs-on: ubuntu-latest + needs: [lint, test] + steps: + - name: Install Go + uses: actions/setup-go@v1 + with: + go-version: 1.13.x + - name: Checkout code + uses: actions/checkout@v1 + - name: build + run: | + export GO111MODULE=on + GOOS=linux GOARCH=amd64 go build -o bin/ci-test-linux-amd64 + - name: upload artifacts + uses: actions/upload-artifact@master + with: + name: binaries + path: bin/ diff --git a/README.md b/README.md index a228142..a621d0e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ # sqs-worker + +![Test&Build](https://github.com/Stashchenko/sqs-worker/workflows/test%20and%20build/badge.svg) +[![Coverage Status](https://coveralls.io/repos/github/Stashchenko/sqs-worker/badge.svg?branch=master)](https://coveralls.io/github/Stashchenko/sqs-worker?branch=master) +[![Go Report Card](https://goreportcard.com/badge/github.com/Stashchenko/sqs-worker)](https://goreportcard.com/report/github.com/Stashchenko/sqs-worker) + GoLang SQS Queue Worker using the AWS SDK: `go get github.com/Stashchenko/sqs-worker` diff --git a/cmd/main.go b/cmd/main.go index e70de7e..5f51b2b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -46,7 +46,7 @@ func main() { func stopGracefully(cancel context.CancelFunc) { // Wait for a signal to quit: signalChan := make(chan os.Signal, 1) - signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM) + signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM) <-signalChan fmt.Println("Stopping sqs workers gracefully...") cancel() diff --git a/message_queue_test.go b/message_queue_test.go index fafa98b..24637f4 100644 --- a/message_queue_test.go +++ b/message_queue_test.go @@ -50,7 +50,6 @@ func TestStart(t *testing.T) { queue := newJobMessageQueue(config, mockSQSClient) ctx, cancel := contextAndCancel() - defer cancel() t.Run("the queue has correct configuration", func(t *testing.T) { assert.Equal(t, queue.config.QueueURL, queueURL, "QueueURL has been set properly") @@ -71,13 +70,16 @@ func TestStart(t *testing.T) { t.Run("Should put a job to the job queue", func(t1 *testing.T) { go queue.listen(ctx) + defer cancel() + clientParams := buildClientParams() deleteInput := &sqs.DeleteMessageInput{QueueUrl: clientParams.QueueUrl} t1.Run("the queue successfully sends a message", func(t *testing.T) { mockSQSClient.On("ReceiveMessage", clientParams).Return() - jobQ := <-queue.GetJobs() + jobQ, ok := <-queue.GetJobs() + assert.Equal(t, ok, true) assert.IsType(t, jobQ, &job{}) assert.IsType(t, jobQ.Message, sqsMessage) @@ -88,7 +90,8 @@ func TestStart(t *testing.T) { mockSQSClient.On("ReceiveMessage", clientParams).Return() mockSQSClient.On("DeleteMessage", deleteInput).Return() - jobQ := <-queue.GetJobs() + jobQ, ok := <-queue.GetJobs() + assert.Equal(t, ok, true) err := queue.DeleteMessage(jobQ.Message.ReceiptHandle) assert.NoError(t, err) diff --git a/worker.go b/worker.go index 1ed7765..fb8d9e2 100644 --- a/worker.go +++ b/worker.go @@ -114,7 +114,7 @@ func (w *Worker) processJob(job *job, h Handler) error { } w.log.Debug(fmt.Sprintf("worker %d: deleted message from queue: %s", w.id, aws.StringValue(job.Message.MessageId))) - job.Duration = time.Now().Sub(job.StartedAt) + job.Duration = time.Since(job.StartedAt) w.log.Debug(fmt.Sprintf("worker %d: processed job in: %s", w.id, job.Duration)) return nil }