diff --git a/Makefile b/Makefile index c82fcca..758fdd1 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 3s -tags unit -v 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..031dc3c --- /dev/null +++ b/test/integration_test.go @@ -0,0 +1,60 @@ +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) + StartConsumer(ctx, cancel, taskQueue, waitGroup) +} + +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 { + waitGroup.Done() + waitGroup.Wait() + cancel() + return nil + }, + ) + return nil +}