proxy-pool/internal/gateway/outcome/queue_test.go
youfak 6766097ea7
Some checks are pending
ci / proto (push) Waiting to run
ci / test (ubuntu-latest) (push) Waiting to run
ci / test (windows-latest) (push) Waiting to run
ci / race (push) Waiting to run
ci / integration (push) Waiting to run
feat: report gateway proxy outcomes
2026-07-31 17:36:17 +08:00

42 lines
1.2 KiB
Go

package outcome
import (
"context"
"errors"
"testing"
domain "proxy-pool/internal/domain/outcome"
)
func TestQueueBatchesWithoutBlockingAndCountsOverflow(t *testing.T) {
queue, err := NewQueue(QueueOptions{Capacity: 2, MaxBatch: 2})
if err != nil {
t.Fatalf("NewQueue() = %v", err)
}
queue.Record(domain.Event{ProxyID: "proxy-a"})
queue.Record(domain.Event{ProxyID: "proxy-b"})
queue.Record(domain.Event{ProxyID: "proxy-c"})
if queue.Dropped() != 1 {
t.Fatalf("Dropped() = %d, want 1", queue.Dropped())
}
batch, err := queue.Next(context.Background())
if err != nil || len(batch) != 2 || batch[0].ProxyID != "proxy-a" || batch[1].ProxyID != "proxy-b" {
t.Fatalf("Next() = %+v, %v", batch, err)
}
if batch, ok := queue.TryNext(); ok || batch != nil {
t.Fatalf("TryNext() = %+v, %t; want nil, false", batch, ok)
}
}
func TestQueueHonorsCanceledContext(t *testing.T) {
queue, err := NewQueue(QueueOptions{Capacity: 1, MaxBatch: 1})
if err != nil {
t.Fatalf("NewQueue() = %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := queue.Next(ctx); !errors.Is(err, context.Canceled) {
t.Fatalf("Next(canceled) = %v, want context canceled", err)
}
}