From 11e7ccfa1f67b379b6e14adb2bb8fbe8f5d78271 Mon Sep 17 00:00:00 2001 From: Thiago Machado Date: Fri, 24 Jun 2022 21:43:48 -0300 Subject: [PATCH 1/2] WIP Implementing integration tests --- Makefile | 4 +++ docker-compose.yaml | 7 +++++ test/integration_test.go | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 docker-compose.yaml create mode 100644 test/integration_test.go diff --git a/Makefile b/Makefile index c82fcca..5727732 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,10 @@ tests: unit-tests unit-tests: go test github.com/Henrod/task-queue/taskqueue -v -tags=unit +.PHONY: integration-tests +integration-tests: + go test -timeout 5s -tags unit github.com/Henrod/task-queue/test + .PHONY: setup setup: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..978a405 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,7 @@ +version: '3.7' + +services: + redis: + image: redis:7-alpine + ports: + - 6379:6379 \ No newline at end of file diff --git a/test/integration_test.go b/test/integration_test.go new file mode 100644 index 0000000..3d3d40a --- /dev/null +++ b/test/integration_test.go @@ -0,0 +1,61 @@ +package test + +import ( + "context" + "fmt" + "sync" + "testing" + "time" + + "github.com/Henrod/task-queue/taskqueue" + "github.com/google/uuid" +) + +func TestTaskQueueMainFlow(t *testing.T) { + options := &taskqueue.Options{ + QueueKey: "integration-test", + Namespace: "simple", + StorageAddress: "localhost:6379", + WorkerID: "worker1", + MaxRetries: 0, + OperationTimeout: time.Minute, + } + + ctx, cancel := context.WithCancel(context.Background()) + taskQueue, err := taskqueue.NewTaskQueue(ctx, taskqueue.NewDefaultRedis(options), options) + if err != nil { + panic(err) + } + + StartProducer(ctx, taskQueue) + var waitGroup sync.WaitGroup + waitGroup.Add(1) + go StartConsumer(ctx, cancel, taskQueue, waitGroup) + waitGroup.Wait() +} + +type Payload struct { + Body string +} + +func StartProducer(ctx context.Context, taskQueue *taskqueue.TaskQueue) error { + id := 1234 + _, err := taskQueue.ProduceAt(ctx, &Payload{Body: fmt.Sprintf("%d", id)}, time.Now()) + if err != nil { + return err + } + return nil +} + +func StartConsumer(ctx context.Context, cancel context.CancelFunc, taskQueue *taskqueue.TaskQueue, waitGroup sync.WaitGroup) error { + taskQueue.Consume( + ctx, + func(ctx context.Context, taskID uuid.UUID, payload interface{}) error { + defer waitGroup.Done() + cancel() + ctx.Done() + return nil + }, + ) + return nil +} \ No newline at end of file From fae54e77fae52421e2d08f9bd3b37f6ad15cac87 Mon Sep 17 00:00:00 2001 From: Thiago Machado Date: Sat, 25 Jun 2022 22:16:59 -0300 Subject: [PATCH 2/2] Fix integration test. --- Makefile | 2 +- test/integration_test.go | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 5727732..758fdd1 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ unit-tests: .PHONY: integration-tests integration-tests: - go test -timeout 5s -tags unit github.com/Henrod/task-queue/test + go test -timeout 3s -tags unit -v github.com/Henrod/task-queue/test .PHONY: setup setup: diff --git a/test/integration_test.go b/test/integration_test.go index 3d3d40a..031dc3c 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -30,8 +30,7 @@ func TestTaskQueueMainFlow(t *testing.T) { StartProducer(ctx, taskQueue) var waitGroup sync.WaitGroup waitGroup.Add(1) - go StartConsumer(ctx, cancel, taskQueue, waitGroup) - waitGroup.Wait() + StartConsumer(ctx, cancel, taskQueue, waitGroup) } type Payload struct { @@ -42,7 +41,7 @@ func StartProducer(ctx context.Context, taskQueue *taskqueue.TaskQueue) error { id := 1234 _, err := taskQueue.ProduceAt(ctx, &Payload{Body: fmt.Sprintf("%d", id)}, time.Now()) if err != nil { - return err + return err } return nil } @@ -51,11 +50,11 @@ func StartConsumer(ctx context.Context, cancel context.CancelFunc, taskQueue *ta taskQueue.Consume( ctx, func(ctx context.Context, taskID uuid.UUID, payload interface{}) error { - defer waitGroup.Done() + waitGroup.Done() + waitGroup.Wait() cancel() - ctx.Done() return nil }, ) return nil -} \ No newline at end of file +}