File size: 5,845 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package scheduler

import (
	"testing"
	"time"
)

func TestQueueEnqueueDequeue(t *testing.T) {
	q := NewTaskQueue(10, 5)

	t1 := &Task{ID: "t1", AgentID: "a1", Priority: 5, CreatedAt: time.Now()}
	t2 := &Task{ID: "t2", AgentID: "a1", Priority: 1, CreatedAt: time.Now()}

	if _, err := q.Enqueue(t1); err != nil {
		t.Fatal(err)
	}
	if _, err := q.Enqueue(t2); err != nil {
		t.Fatal(err)
	}

	// t2 has higher priority (lower number), should come first.
	got := q.Dequeue(10, 20)
	if got == nil {
		t.Fatal("expected a task")
		return
	}
	if got.ID != "t2" {
		t.Errorf("expected t2 (priority 1), got %s (priority %d)", got.ID, got.Priority)
	}

	got2 := q.Dequeue(10, 20)
	if got2 == nil || got2.ID != "t1" {
		t.Error("expected t1 next")
	}
}

func TestQueueGlobalLimit(t *testing.T) {
	q := NewTaskQueue(2, 10)

	if _, err := q.Enqueue(&Task{ID: "t1", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	if _, err := q.Enqueue(&Task{ID: "t2", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	_, err := q.Enqueue(&Task{ID: "t3", AgentID: "a1", CreatedAt: time.Now()})
	if err == nil {
		t.Error("should reject when global limit reached")
	}
}

func TestQueuePerAgentLimit(t *testing.T) {
	q := NewTaskQueue(100, 2)

	if _, err := q.Enqueue(&Task{ID: "t1", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	if _, err := q.Enqueue(&Task{ID: "t2", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	_, err := q.Enqueue(&Task{ID: "t3", AgentID: "a1", CreatedAt: time.Now()})
	if err == nil {
		t.Error("should reject when per-agent limit reached")
	}

	// Different agent should still work.
	if _, err := q.Enqueue(&Task{ID: "t4", AgentID: "a2", CreatedAt: time.Now()}); err != nil {
		t.Errorf("different agent should succeed: %v", err)
	}
}

func TestQueueFairness(t *testing.T) {
	q := NewTaskQueue(100, 100)

	// Agent a1 has 3 tasks, a2 has 1 task.
	for i := range 3 {
		if _, err := q.Enqueue(&Task{ID: "a1-" + string(rune('0'+i)), AgentID: "a1", CreatedAt: time.Now()}); err != nil {
			t.Fatalf("enqueue failed: %v", err)
		}
	}
	if _, err := q.Enqueue(&Task{ID: "a2-0", AgentID: "a2", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}

	// First dequeue: both have 0 inflight, either could be picked.
	got1 := q.Dequeue(10, 20)
	if got1 == nil {
		t.Fatal("expected a task")
		return
	}

	// Now one agent has 1 inflight. The agent with 0 should be preferred.
	got2 := q.Dequeue(10, 20)
	if got2 == nil {
		t.Fatal("expected a task")
		return
	}

	// The two dequeued tasks should be from different agents (fairness).
	if got1.AgentID == got2.AgentID {
		t.Errorf("fairness: expected different agents, got %s and %s", got1.AgentID, got2.AgentID)
	}
}

func TestQueueInflightLimit(t *testing.T) {
	q := NewTaskQueue(100, 100)

	if _, err := q.Enqueue(&Task{ID: "t1", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	if _, err := q.Enqueue(&Task{ID: "t2", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}

	// Dequeue with max inflight = 1
	got := q.Dequeue(1, 2)
	if got == nil {
		t.Fatal("first dequeue should work")
	}

	// Second dequeue should be blocked (per-agent limit).
	got2 := q.Dequeue(1, 2)
	if got2 != nil {
		t.Error("should be blocked by per-agent inflight limit")
	}

	// Complete the first, now second should work.
	q.Complete("a1")
	got3 := q.Dequeue(1, 2)
	if got3 == nil {
		t.Error("should dequeue after completing")
	}
}

func TestQueueRemove(t *testing.T) {
	q := NewTaskQueue(10, 10)
	if _, err := q.Enqueue(&Task{ID: "t1", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	if _, err := q.Enqueue(&Task{ID: "t2", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}

	if !q.Remove("t1", "a1") {
		t.Error("should find and remove t1")
	}
	if q.Remove("t1", "a1") {
		t.Error("removing again should return false")
	}

	// Only t2 should remain.
	got := q.Dequeue(10, 20)
	if got == nil || got.ID != "t2" {
		t.Error("expected t2 remaining")
	}
}

func TestQueueExpireDeadlined(t *testing.T) {
	old := timeNow
	defer func() { timeNow = old }()

	now := time.Date(2025, 1, 1, 12, 0, 0, 0, time.UTC)
	timeNow = func() time.Time { return now }

	q := NewTaskQueue(10, 10)

	pastDeadline := &Task{
		ID: "expired", AgentID: "a1", CreatedAt: now,
		Deadline: now.Add(-1 * time.Second),
	}
	futureDeadline := &Task{
		ID: "valid", AgentID: "a1", CreatedAt: now,
		Deadline: now.Add(1 * time.Hour),
	}
	if _, err := q.Enqueue(pastDeadline); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	if _, err := q.Enqueue(futureDeadline); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}

	expired := q.ExpireDeadlined()
	if len(expired) != 1 || expired[0].ID != "expired" {
		t.Errorf("expected 1 expired task, got %d", len(expired))
	}

	stats := q.Stats()
	if stats.TotalQueued != 1 {
		t.Errorf("expected 1 remaining, got %d", stats.TotalQueued)
	}
}

func TestQueueStats(t *testing.T) {
	q := NewTaskQueue(100, 100)
	if _, err := q.Enqueue(&Task{ID: "t1", AgentID: "a1", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	if _, err := q.Enqueue(&Task{ID: "t2", AgentID: "a2", CreatedAt: time.Now()}); err != nil {
		t.Fatalf("enqueue failed: %v", err)
	}
	q.Dequeue(10, 20) // a1 or a2 gets 1 inflight

	stats := q.Stats()
	if stats.TotalQueued != 1 {
		t.Errorf("expected 1 queued, got %d", stats.TotalQueued)
	}
	if stats.TotalInflight != 1 {
		t.Errorf("expected 1 inflight, got %d", stats.TotalInflight)
	}
	if len(stats.Agents) != 2 {
		t.Errorf("expected 2 agents, got %d", len(stats.Agents))
	}
}