Spaces:
Build error
Build error
File size: 8,463 Bytes
4b1daed | 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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | package workers_test
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/AmaniQuery/amaniquery/internal/workers"
"go.uber.org/zap"
)
// TestDefaultConfig tests default configuration
func TestDefaultConfig(t *testing.T) {
cfg := workers.DefaultConfig()
if cfg.MaxWorkers <= 0 {
t.Error("Expected positive MaxWorkers")
}
if cfg.QueueSize <= 0 {
t.Error("Expected positive QueueSize")
}
}
// TestNewPool tests pool creation
func TestNewPool(t *testing.T) {
logger, _ := zap.NewDevelopment()
cfg := workers.Config{
MaxWorkers: 4,
QueueSize: 100,
Logger: logger,
}
pool := workers.NewPool(cfg)
if pool == nil {
t.Fatal("Expected non-nil pool")
}
// Clean shutdown
err := pool.Shutdown(5 * time.Second)
if err != nil {
t.Fatalf("Shutdown failed: %v", err)
}
}
// TestPool_Submit tests basic task submission
func TestPool_Submit(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 2,
QueueSize: 10,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
var executed int32
task := workers.Task{
ID: "test-task",
Execute: func(ctx context.Context) error {
atomic.AddInt32(&executed, 1)
return nil
},
}
err := pool.Submit(task)
if err != nil {
t.Fatalf("Submit failed: %v", err)
}
// Wait for task to execute
time.Sleep(100 * time.Millisecond)
if atomic.LoadInt32(&executed) != 1 {
t.Error("Expected task to be executed")
}
}
// TestPool_SubmitWait tests synchronous task submission
func TestPool_SubmitWait(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 2,
QueueSize: 10,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
ctx := context.Background()
var executed bool
err := pool.SubmitWait(ctx, func(ctx context.Context) error {
executed = true
return nil
})
if err != nil {
t.Fatalf("SubmitWait failed: %v", err)
}
if !executed {
t.Error("Expected task to be executed")
}
}
// TestPool_SubmitWait_Error tests error handling in SubmitWait
func TestPool_SubmitWait_Error(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 2,
QueueSize: 10,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
ctx := context.Background()
expectedErr := errors.New("task error")
err := pool.SubmitWait(ctx, func(ctx context.Context) error {
return expectedErr
})
if err == nil {
t.Fatal("Expected error from task")
}
if err.Error() != expectedErr.Error() {
t.Errorf("Expected error '%v', got '%v'", expectedErr, err)
}
}
// TestPool_SubmitWait_ContextCancellation tests context cancellation
func TestPool_SubmitWait_ContextCancellation(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 1,
QueueSize: 10,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := pool.SubmitWait(ctx, func(ctx context.Context) error {
time.Sleep(200 * time.Millisecond)
return nil
})
if err == nil {
t.Fatal("Expected context cancellation error")
}
}
// TestPool_SubmitBatch tests batch task submission
func TestPool_SubmitBatch(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 4,
QueueSize: 20,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
var counter int32
tasks := make([]workers.Task, 5)
for i := range tasks {
tasks[i] = workers.Task{
ID: string(rune('0' + i)),
Execute: func(ctx context.Context) error {
atomic.AddInt32(&counter, 1)
return nil
},
}
}
results := pool.SubmitBatch(tasks)
// Collect results
var errorCount int
for err := range results {
if err != nil {
errorCount++
}
}
if errorCount > 0 {
t.Errorf("Expected no errors, got %d", errorCount)
}
if atomic.LoadInt32(&counter) != 5 {
t.Errorf("Expected 5 tasks executed, got %d", counter)
}
}
// TestPool_Metrics tests metrics tracking
func TestPool_Metrics(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 2,
QueueSize: 10,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
// Submit some tasks
for i := 0; i < 5; i++ {
pool.Submit(workers.Task{
ID: string(rune('0' + i)),
Execute: func(ctx context.Context) error {
return nil
},
})
}
// Wait for tasks to complete
time.Sleep(200 * time.Millisecond)
metrics := pool.Metrics()
if metrics.MaxWorkers != 2 {
t.Errorf("Expected MaxWorkers 2, got %d", metrics.MaxWorkers)
}
if metrics.QueueCapacity != 10 {
t.Errorf("Expected QueueCapacity 10, got %d", metrics.QueueCapacity)
}
if metrics.CompletedTasks < 5 {
t.Errorf("Expected at least 5 completed tasks, got %d", metrics.CompletedTasks)
}
}
// TestPool_OnError tests error callback
func TestPool_OnError(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 2,
QueueSize: 10,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
var capturedErr error
expectedErr := errors.New("task error")
task := workers.Task{
ID: "error-task",
Execute: func(ctx context.Context) error {
return expectedErr
},
OnError: func(err error) {
capturedErr = err
},
}
pool.Submit(task)
// Wait for task to execute
time.Sleep(100 * time.Millisecond)
if capturedErr == nil {
t.Fatal("Expected error to be captured")
}
if capturedErr.Error() != expectedErr.Error() {
t.Errorf("Expected error '%v', got '%v'", expectedErr, capturedErr)
}
}
// TestPool_PanicRecovery tests panic recovery in tasks
func TestPool_PanicRecovery(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 2,
QueueSize: 10,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
var capturedErr error
task := workers.Task{
ID: "panic-task",
Execute: func(ctx context.Context) error {
panic("intentional panic")
},
OnError: func(err error) {
capturedErr = err
},
}
pool.Submit(task)
// Wait for task to execute
time.Sleep(100 * time.Millisecond)
if capturedErr == nil {
t.Fatal("Expected panic to be recovered and converted to error")
}
}
// TestPool_Shutdown tests graceful shutdown
func TestPool_Shutdown(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 2,
QueueSize: 10,
Logger: logger,
})
// Submit some work before shutdown
var executed int32
pool.Submit(workers.Task{
ID: "pre-shutdown",
Execute: func(ctx context.Context) error {
atomic.AddInt32(&executed, 1)
return nil
},
})
// Wait briefly for task to execute
time.Sleep(50 * time.Millisecond)
err := pool.Shutdown(5 * time.Second)
if err != nil {
t.Fatalf("Shutdown failed: %v", err)
}
if atomic.LoadInt32(&executed) != 1 {
t.Error("Expected task to execute before shutdown")
}
}
// TestPool_Resize tests dynamic resizing
func TestPool_Resize(t *testing.T) {
logger, _ := zap.NewDevelopment()
pool := workers.NewPool(workers.Config{
MaxWorkers: 2,
QueueSize: 10,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
initialMetrics := pool.Metrics()
if initialMetrics.MaxWorkers != 2 {
t.Errorf("Expected initial MaxWorkers 2, got %d", initialMetrics.MaxWorkers)
}
// Resize to larger
pool.Resize(4)
metrics := pool.Metrics()
if metrics.MaxWorkers != 4 {
t.Errorf("Expected MaxWorkers 4 after resize, got %d", metrics.MaxWorkers)
}
}
// TestPoolError tests error type
func TestPoolError(t *testing.T) {
err := workers.ErrPoolShutdown
if err.Error() != "worker pool is shutdown" {
t.Errorf("Expected 'worker pool is shutdown', got '%s'", err.Error())
}
err = workers.ErrQueueFull
if err.Error() != "task queue is full" {
t.Errorf("Expected 'task queue is full', got '%s'", err.Error())
}
}
// BenchmarkPool_Submit benchmarks task submission
func BenchmarkPool_Submit(b *testing.B) {
logger, _ := zap.NewProduction()
pool := workers.NewPool(workers.Config{
MaxWorkers: 8,
QueueSize: 10000,
Logger: logger,
})
defer pool.Shutdown(5 * time.Second)
task := workers.Task{
Execute: func(ctx context.Context) error {
return nil
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
pool.Submit(task)
}
}
|