- Add API server with WebSocket support and REST endpoints - Implement authentication system with API keys and permissions - Add task queue system with Redis backend and error handling - Include storage layer with database migrations and schemas - Add comprehensive logging, metrics, and telemetry - Implement security middleware and network utilities - Add experiment management and container orchestration - Include configuration management with smart defaults
152 lines
3.5 KiB
Go
152 lines
3.5 KiB
Go
package queue
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestTask_UserFields(t *testing.T) {
|
|
task := &Task{
|
|
UserID: "testuser",
|
|
Username: "testuser",
|
|
CreatedBy: "testuser",
|
|
}
|
|
|
|
if task.UserID != "testuser" {
|
|
t.Errorf("Expected UserID to be 'testuser', got '%s'", task.UserID)
|
|
}
|
|
|
|
if task.Username != "testuser" {
|
|
t.Errorf("Expected Username to be 'testuser', got '%s'", task.Username)
|
|
}
|
|
|
|
if task.CreatedBy != "testuser" {
|
|
t.Errorf("Expected CreatedBy to be 'testuser', got '%s'", task.CreatedBy)
|
|
}
|
|
}
|
|
|
|
func TestTaskQueue_UserFiltering(t *testing.T) {
|
|
// Setup test Redis configuration
|
|
queueCfg := Config{
|
|
RedisAddr: "localhost:6379",
|
|
RedisDB: 15, // Use dedicated test DB
|
|
}
|
|
|
|
// Create task queue
|
|
taskQueue, err := NewTaskQueue(queueCfg)
|
|
if err != nil {
|
|
t.Skip("Redis not available for integration testing")
|
|
return
|
|
}
|
|
defer taskQueue.Close()
|
|
|
|
// Clear test database
|
|
taskQueue.client.FlushDB(taskQueue.ctx)
|
|
|
|
// Create test tasks with different users
|
|
tasks := []*Task{
|
|
{
|
|
ID: "task1",
|
|
JobName: "user1_job1",
|
|
Status: "queued",
|
|
UserID: "user1",
|
|
CreatedBy: "user1",
|
|
CreatedAt: time.Now(),
|
|
},
|
|
{
|
|
ID: "task2",
|
|
JobName: "user1_job2",
|
|
Status: "running",
|
|
UserID: "user1",
|
|
CreatedBy: "user1",
|
|
CreatedAt: time.Now(),
|
|
},
|
|
{
|
|
ID: "task3",
|
|
JobName: "user2_job1",
|
|
Status: "queued",
|
|
UserID: "user2",
|
|
CreatedBy: "user2",
|
|
CreatedAt: time.Now(),
|
|
},
|
|
{
|
|
ID: "task4",
|
|
JobName: "admin_job",
|
|
Status: "completed",
|
|
UserID: "admin",
|
|
CreatedBy: "admin",
|
|
CreatedAt: time.Now(),
|
|
},
|
|
}
|
|
|
|
// Add tasks to queue
|
|
for _, task := range tasks {
|
|
err := taskQueue.AddTask(task)
|
|
if err != nil {
|
|
t.Fatalf("Failed to add task %s: %v", task.ID, err)
|
|
}
|
|
}
|
|
|
|
// Test GetAllTasks
|
|
allTasks, err := taskQueue.GetAllTasks()
|
|
if err != nil {
|
|
t.Fatalf("Failed to get all tasks: %v", err)
|
|
}
|
|
|
|
if len(allTasks) != len(tasks) {
|
|
t.Errorf("Expected %d tasks, got %d", len(tasks), len(allTasks))
|
|
}
|
|
|
|
// Test user filtering logic
|
|
filterTasksForUser := func(tasks []*Task, userID string) []*Task {
|
|
var filtered []*Task
|
|
for _, task := range tasks {
|
|
if task.UserID == userID || task.CreatedBy == userID {
|
|
filtered = append(filtered, task)
|
|
}
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
// Test filtering for user1 (should get 2 tasks)
|
|
user1Tasks := filterTasksForUser(allTasks, "user1")
|
|
if len(user1Tasks) != 2 {
|
|
t.Errorf("Expected 2 tasks for user1, got %d", len(user1Tasks))
|
|
}
|
|
|
|
// Test filtering for user2 (should get 1 task)
|
|
user2Tasks := filterTasksForUser(allTasks, "user2")
|
|
if len(user2Tasks) != 1 {
|
|
t.Errorf("Expected 1 task for user2, got %d", len(user2Tasks))
|
|
}
|
|
|
|
// Test filtering for admin (should get 1 task)
|
|
adminTasks := filterTasksForUser(allTasks, "admin")
|
|
if len(adminTasks) != 1 {
|
|
t.Errorf("Expected 1 task for admin, got %d", len(adminTasks))
|
|
}
|
|
|
|
// Test GetTaskByName
|
|
task, err := taskQueue.GetTaskByName("user1_job1")
|
|
if err != nil {
|
|
t.Errorf("Failed to get task by name: %v", err)
|
|
}
|
|
if task == nil || task.UserID != "user1" {
|
|
t.Error("Got wrong task or nil task")
|
|
}
|
|
|
|
// Test CancelTask
|
|
err = taskQueue.CancelTask("task1")
|
|
if err != nil {
|
|
t.Errorf("Failed to cancel task: %v", err)
|
|
}
|
|
|
|
// Verify task was cancelled
|
|
cancelledTask, err := taskQueue.GetTask("task1")
|
|
if err != nil {
|
|
t.Errorf("Failed to get cancelled task: %v", err)
|
|
}
|
|
if cancelledTask.Status != "cancelled" {
|
|
t.Errorf("Expected status 'cancelled', got '%s'", cancelledTask.Status)
|
|
}
|
|
}
|