Skip to content

Commit

Permalink
Convert to a module, update docker, use rabbit's own module, update t…
Browse files Browse the repository at this point in the history
…hird party modules, and use new linter. (#41)

* Remove vendor directory.

* Package as a module.
Update third party modules.
Use rabbitmq's go module instead of the forked version.
Change linter.
Make lint's suggested changes.
Update Docker.
Run the sample app in Docker
  • Loading branch information
pjdemers authored Oct 2, 2024
1 parent 123e544 commit f377eda
Show file tree
Hide file tree
Showing 235 changed files with 165 additions and 116,722 deletions.
26 changes: 26 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
linters:
enable:
- gosimple
- gofmt
- revive
- govet
- errcheck
- ineffassign
- staticcheck
- unused
- typecheck
- bodyclose

disable-all: true

issues:
exclude-use-default: true
exclude:
# This ignores errcheck fails about unchecked errs for things like `defer file.Close()`
- ((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv)

run:
timeout: 10m
go: "1.23"
modules-download-mode: readonly

13 changes: 9 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
FROM golang:1.12
#syntax=docker/dockerfile:1.10.0

ARG GOLANG_VERSION=1.23.1
ARG GOLANG_LINT_VERSION=v1.61.0

FROM golang:${GOLANG_VERSION}
ENV CGO_ENABLED=0

RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANG_LINT_VERSION}

RUN go get -u github.com/alecthomas/gometalinter
RUN go get -u github.com/kardianos/govendor
RUN gometalinter --install
WORKDIR /go/src/github.com/mergermarket/run-amqp
ADD . /go/src/github.com/mergermarket/run-amqp
CMD ./build-app.sh
21 changes: 1 addition & 20 deletions build-app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,7 @@ set -o errexit
set -o nounset
set -o pipefail

if [ ! $(command -v gometalinter) ]
then
go get github.com/alecthomas/gometalinter
gometalinter --install --vendor
fi

gometalinter \
--vendor \
--exclude='error return value not checked.*(Close|Log|Print|fmt.Fprintln|fmt.Fprintf|fmt.Fprint).*$' \
--exclude='struct of size.*$' \
--exclude='.*_test\.go:.*error return value not checked.*\(errcheck\)$' \
--exclude='duplicate of.*_test.go.*\(dupl\)$' \
--disable=aligncheck \
--disable=gotype \
--disable=unconvert \
--disable=aligncheck \
--disable=gas \
--cyclo-over=20 \
--tests \
--deadline=80s
golangci-lint run --timeout=10m

go fmt $(go list ./... | grep -v /vendor/)
go test $(go list ./... | grep -v acceptance-tests ) --cover -timeout 25s
29 changes: 29 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

services:
test:
build:
context: .
depends_on:
rabbitmq:
condition: service_healthy

sampleapp:
build:
context: ./
dockerfile: ./sample/Dockerfile
ports:
- '8080:8080'
depends_on:
rabbitmq:
condition: service_healthy

rabbitmq:
image: rabbitmq:3-management
ports:
- '5672:5672'
- '15672:15672'
healthcheck:
test: rabbitmq-diagnostics -q ping
interval: 30s
timeout: 30s
retries: 3
2 changes: 1 addition & 1 deletion connection/channel-connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package connection

import (
"fmt"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
"strings"
)

Expand Down
2 changes: 1 addition & 1 deletion connection/channel-connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package connection

import (
"github.com/mergermarket/run-amqp/helpers"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
"testing"
"time"
)
Expand Down
8 changes: 7 additions & 1 deletion connection/connection-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package connection

import (
"fmt"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
)

type logger interface {
Expand All @@ -11,11 +11,17 @@ type logger interface {
Debug(...interface{})
}

// Disable the linter because it complains about the name ConnectionManager, which I don't want to change right now
//
//revive:disable
type ConnectionManager interface {
OpenChannel(description string) chan *amqp.Channel
sendConnectionError(err *amqp.Error)
sendChannelError(index uint8, err *amqp.Error) error
}

//revive:enable

type manager struct {
openConnection *amqp.Connection
connections chan *amqp.Connection
Expand Down
2 changes: 1 addition & 1 deletion connection/connection-manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package connection

import (
"github.com/mergermarket/run-amqp/helpers"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
"testing"
"time"
)
Expand Down
2 changes: 1 addition & 1 deletion connection/server-connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package connection

import (
"fmt"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
"math"
"net/url"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion connection/server-connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package connection

import (
"github.com/mergermarket/run-amqp/helpers"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
"testing"
"time"
)
Expand Down
4 changes: 2 additions & 2 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package runamqp
import (
"fmt"
"github.com/mergermarket/run-amqp/connection"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
"sync"
)

Expand All @@ -21,7 +21,7 @@ type Consumer struct {
consumerChannels *consumerChannels
}

// MessageHandler is something that can process a Message, calling Ack, nackCalls when appropiate for your domain
// MessageHandler is something that can process a Message, calling Ack, nackCalls when appropriate for your domain
type MessageHandler interface {
// Name is a description of your handler for logging purposes
Name() string
Expand Down
9 changes: 4 additions & 5 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ const testRequeueTTL = 200
const testRequeueLimit = 5
const serviceName = "testservice"

func init() {
rand.Seed(time.Now().UnixNano())
}

func TestConsumerConsumesMessages(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -324,7 +320,10 @@ func TestRequeue_With_No_Requeue_Limit(t *testing.T) {
if actualMessage != expectedMessage {
t.Fatalf("Failed to get the requeued message: %s but got %s", expectedMessage, actualMessage)
}
msg.Requeue(fmt.Sprintf("Requing it for the %d time.", counter))
err := msg.Requeue(fmt.Sprintf("Requing it for the %d time.", counter))
if err != nil {
t.Fatal("Failed to requeue message")
}
}

if counter != 10 {
Expand Down
23 changes: 0 additions & 23 deletions docker-compose.yml

This file was deleted.

10 changes: 7 additions & 3 deletions example_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package runamqp

import (
"fmt"
"io/ioutil"
"io"
"log"
"time"
)
Expand All @@ -15,7 +15,11 @@ type ExampleHandler struct {
// Handle is how you implement the MessageHandler interface, what you do with it is up to you
func (e *ExampleHandler) Handle(msg Message) {
e.calledWith = string(msg.Body())
msg.Ack()
err := msg.Ack()
if err != nil {
// Handle error.
return
}
}

func (e *ExampleHandler) Name() string {
Expand All @@ -30,7 +34,7 @@ func ExampleConsumer() {
"test-example-exchange",
Fanout,
noPatterns,
&SimpleLogger{ioutil.Discard},
&SimpleLogger{io.Discard},
testRequeueTTL,
testRequeueLimit,
serviceName,
Expand Down
18 changes: 15 additions & 3 deletions example_stubmessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,31 @@ func (e *ExampleHandlerToTest) Handle(msg Message) {
message := string(msg.Body())

if message == "AckMessage" {
msg.Ack()
err := msg.Ack()
if err != nil {
// Handle error
return
}
return
}

if message == "NackMessage" {
reasonForNach := "nackCalls demo!"
msg.Nack(reasonForNach)
err := msg.Nack(reasonForNach)
if err != nil {
// Handle error.
return
}
return
}

if message == "RequeueMessage" {
reasonForRequeue := "requeueCalls demo!"
msg.Requeue(reasonForRequeue)
err := msg.Requeue(reasonForRequeue)
if err != nil {
// Handle error.
return
}
return
}

Expand Down
2 changes: 1 addition & 1 deletion exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package runamqp

import (
"fmt"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
"strings"
)

Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/mergermarket/run-amqp

go 1.23.1

require github.com/rabbitmq/amqp091-go v1.10.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
12 changes: 6 additions & 6 deletions helpers/test-logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package helpers

import "testing"

type testLogger struct {
type TestLogger struct {
t *testing.T
}

func NewTestLogger(t *testing.T) *testLogger {
return &testLogger{
func NewTestLogger(t *testing.T) *TestLogger {
return &TestLogger{
t: t,
}
}

func (t *testLogger) Info(items ...interface{}) {
func (t *TestLogger) Info(items ...interface{}) {
t.t.Log("INFO", items)
}

func (t *testLogger) Error(items ...interface{}) {
func (t *TestLogger) Error(items ...interface{}) {
t.t.Log("ERROR", items)
}

func (t *testLogger) Debug(items ...interface{}) {
func (t *TestLogger) Debug(items ...interface{}) {
t.t.Log("DEBUG", items)
}
2 changes: 1 addition & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package runamqp

import (
"fmt"
"github.com/streadway/amqp"
amqp "github.com/rabbitmq/amqp091-go"
"time"
)

Expand Down
2 changes: 1 addition & 1 deletion process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type alwaysAckingHandler struct {

func (a *alwaysAckingHandler) Handle(msg Message) {
a.messageRecieved = string(msg.Body())
msg.Ack()
_ = msg.Ack()
}

func (a *alwaysAckingHandler) Name() string {
Expand Down
Loading

0 comments on commit f377eda

Please sign in to comment.