135 lines
3.2 KiB
Go
135 lines
3.2 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"git.threesix.ai/jordan/sp4-verify-1770325799/pkg/logging"
|
|
"git.threesix.ai/jordan/sp4-verify-1770325799/pkg/queue"
|
|
)
|
|
|
|
func TestQueueClient_PushTask(t *testing.T) {
|
|
// Skip if REDIS_URL not set (integration test)
|
|
redisURL := os.Getenv("REDIS_URL")
|
|
if redisURL == "" {
|
|
t.Skip("REDIS_URL not set, skipping integration test")
|
|
}
|
|
|
|
logger := logging.New(logging.Config{Level: logging.LevelDebug})
|
|
|
|
client, err := NewQueueClient(logger)
|
|
if err != nil {
|
|
t.Fatalf("failed to create queue client: %v", err)
|
|
}
|
|
defer client.Close()
|
|
|
|
// Push a task
|
|
jobID, err := client.PushTask(context.Background(), "test_task", map[string]any{
|
|
"message": "hello",
|
|
"count": 42,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("failed to push task: %v", err)
|
|
}
|
|
|
|
if jobID == "" {
|
|
t.Error("expected non-empty job ID")
|
|
}
|
|
|
|
t.Logf("pushed task with ID: %s", jobID)
|
|
}
|
|
|
|
func TestQueueClient_PushTaskWithPriority(t *testing.T) {
|
|
redisURL := os.Getenv("REDIS_URL")
|
|
if redisURL == "" {
|
|
t.Skip("REDIS_URL not set, skipping integration test")
|
|
}
|
|
|
|
logger := logging.New(logging.Config{Level: logging.LevelDebug})
|
|
|
|
client, err := NewQueueClient(logger)
|
|
if err != nil {
|
|
t.Fatalf("failed to create queue client: %v", err)
|
|
}
|
|
defer client.Close()
|
|
|
|
// Push tasks with different priorities
|
|
_, err = client.PushTaskWithPriority(context.Background(), "low_priority", map[string]any{"level": "low"}, 0)
|
|
if err != nil {
|
|
t.Fatalf("failed to push low priority task: %v", err)
|
|
}
|
|
|
|
_, err = client.PushTaskWithPriority(context.Background(), "high_priority", map[string]any{"level": "high"}, 10)
|
|
if err != nil {
|
|
t.Fatalf("failed to push high priority task: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNewQueueClient_MissingURL(t *testing.T) {
|
|
os.Unsetenv("REDIS_URL")
|
|
|
|
logger := logging.New(logging.Config{Level: logging.LevelDebug})
|
|
_, err := NewQueueClient(logger)
|
|
if err == nil {
|
|
t.Error("expected error when REDIS_URL is not set")
|
|
}
|
|
}
|
|
|
|
func TestRedisQueue_Integration(t *testing.T) {
|
|
redisURL := os.Getenv("REDIS_URL")
|
|
if redisURL == "" {
|
|
t.Skip("REDIS_URL not set, skipping integration test")
|
|
}
|
|
|
|
opts, err := redis.ParseURL(redisURL)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse REDIS_URL: %v", err)
|
|
}
|
|
|
|
client := redis.NewClient(opts)
|
|
defer client.Close()
|
|
|
|
logger := logging.New(logging.Config{Level: logging.LevelDebug})
|
|
q := queue.NewRedisQueue(client, logger)
|
|
|
|
ctx := context.Background()
|
|
|
|
// Test enqueue
|
|
jobID, err := q.Enqueue(ctx, "test_job", map[string]any{"key": "value"})
|
|
if err != nil {
|
|
t.Fatalf("failed to enqueue: %v", err)
|
|
}
|
|
|
|
// Test dequeue
|
|
job, err := q.Dequeue(ctx, "test-worker")
|
|
if err != nil {
|
|
t.Fatalf("failed to dequeue: %v", err)
|
|
}
|
|
|
|
if job.ID != jobID {
|
|
t.Errorf("job ID = %s, want %s", job.ID, jobID)
|
|
}
|
|
|
|
if job.Type != "test_job" {
|
|
t.Errorf("job type = %s, want test_job", job.Type)
|
|
}
|
|
|
|
// Test ack
|
|
if err := q.Ack(ctx, jobID); err != nil {
|
|
t.Fatalf("failed to ack: %v", err)
|
|
}
|
|
|
|
// Verify job is completed
|
|
completedJob, err := q.GetJob(ctx, jobID)
|
|
if err != nil {
|
|
t.Fatalf("failed to get job: %v", err)
|
|
}
|
|
|
|
if completedJob.Status != queue.StatusCompleted {
|
|
t.Errorf("job status = %s, want %s", completedJob.Status, queue.StatusCompleted)
|
|
}
|
|
}
|