Skip to content

Commit

Permalink
fix: add testsssss (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxcodec authored Jun 9, 2024
1 parent a9c6351 commit b5b88d8
Show file tree
Hide file tree
Showing 29 changed files with 1,369 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Unit Testing

on:
push:
Expand Down
35 changes: 35 additions & 0 deletions .github/workflows/integration-testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Integration Testing

on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ["oldstable", "stable"]
env:
VERBOSE: 1

steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: getong/[email protected]
with:
rabbitmq version: "3.13.3-management-alpine"
host port: 5672
rabbitmq user: "test"
rabbitmq password: "test"
rabbitmq vhost: "test"
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Run tests
run: make integration-test-ci
26 changes: 23 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,37 @@ TESTS_ARGS += -test.count 1
TESTS_ARGS += -test.failfast
TESTS_ARGS += -test.coverprofile coverage.out
TESTS_ARGS += -test.timeout 60s
TESTS_ARGS += -race
TESTS_ARGS_WITHRACE := $(TESTS_ARGS)
TESTS_ARGS_WITHRACE += -race

run-tests: $(GOTESTSUM)
@ gotestsum $(TESTS_ARGS) -short
@gotestsum $(TESTS_ARGS_WITHRACE) -short

test: run-tests $(TPARSE) ## Run Tests & parse details
@cat gotestsum.json.out | $(TPARSE) -all -notests
docker-test:
@docker-compose -f test.compose.yaml up -d --build

integration-test: docker-test
@echo "Running Integration Tests"
@gotestsum $(TESTS_ARGS)
@cat gotestsum.json.out | $(TPARSE) -all -notests

integration-test-ci: $(GOTESTSUM) $(TPARSE)
@echo "Running Integration Tests"
@gotestsum $(TESTS_ARGS)
@cat gotestsum.json.out | $(TPARSE) -all -notests

docker-clean:
@docker-compose -f test.compose.yaml down

lint: $(GOLANGCI) ## Runs golangci-lint with predefined configuration
@echo "Applying linter"
golangci-lint version
golangci-lint run -c .golangci.yaml ./...

.PHONY: lint lint-prepare clean build unittest
.PHONY: lint lint-prepare clean build unittest


go-generate: $(MOCKERY) ## Runs go generte ./...
go generate ./...
5 changes: 4 additions & 1 deletion consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package goqueue
import "context"

// Consumer represents an entity that consumes messages from a queue.
//
//go:generate mockery --name Consumer
type Consumer interface {
// Consume consumes messages from the queue and passes them to the provided handler.
// It takes a context, an InboundMessageHandler, and a map of metadata as parameters.
Expand All @@ -14,6 +16,7 @@ type Consumer interface {
Stop(ctx context.Context) (err error)
}

//go:generate mockery --name InboundMessageHandler
type InboundMessageHandler interface {
HandleMessage(ctx context.Context, m InboundMessage) (err error)
}
Expand All @@ -39,5 +42,5 @@ type InboundMessage struct {
// eg RabbitMQ: https://www.rabbitmq.com/docs/dlx
MoveToDeadLetterQueue func(ctx context.Context) (err error) `json:"-"`
// Requeue is used to put the message back to the tail of the queue after a delay.
Requeue func(ctx context.Context, delayFn DelayFn) (err error) `json:"-"`
PutToBackOfQueueWithDelay func(ctx context.Context, delayFn DelayFn) (err error) `json:"-"`
}
35 changes: 34 additions & 1 deletion consumer/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ package consumer

import "github.com/bxcodec/goqueue"

const (
DefaultMaxRetryFailedMessage = 3
DefaultBatchMessageSize = 1
)

// Option represents the configuration options for the consumer.
type Option struct {
// BatchMessageSize specifies the maximum number of messages to be processed in a single batch.
BatchMessageSize int
// QueueName specifies the name of the queue to consume messages from.
QueueName string
// Middlewares is a list of middleware functions to be applied to the inbound message handler.
Middlewares []goqueue.InboundMessageHandlerMiddlewareFunc
Middlewares []goqueue.InboundMessageHandlerMiddlewareFunc
ActionsPatternSubscribed []string
TopicName string
MaxRetryFailedMessage int64
}

// OptionFunc is a function type that takes an `opt` parameter of type `*Option`.
Expand Down Expand Up @@ -40,3 +48,28 @@ func WithMiddlewares(middlewares ...goqueue.InboundMessageHandlerMiddlewareFunc)
opt.Middlewares = middlewares
}
}

// WithActionsPatternSubscribed sets the actions that the consumer will subscribe to.
// It takes a variadic parameter `actions` which represents the actions to be subscribed.
// The actions are stored in the `ActionsPatternSubscribed` field of the `Option` struct.
func WithActionsPatternSubscribed(actions ...string) OptionFunc {
return func(opt *Option) {
opt.ActionsPatternSubscribed = actions
}
}

// WithTopicName sets the topic name for the consumer option.
func WithTopicName(name string) OptionFunc {
return func(opt *Option) {
opt.TopicName = name
}
}

// WithMaxRetryFailedMessage sets the maximum number of retries for failed messages.
// It takes an integer parameter 'n' and returns an OptionFunc.
// The OptionFunc updates the 'MaxRetryFailedMessage' field of the Option struct.
func WithMaxRetryFailedMessage(n int64) OptionFunc {
return func(opt *Option) {
opt.MaxRetryFailedMessage = n
}
}
Loading

0 comments on commit b5b88d8

Please sign in to comment.