128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
// internal/collectors/collector_test.go
|
|
package collectors
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// mockCollector 用于测试的模拟采集器
|
|
type mockCollector struct {
|
|
BaseCollector
|
|
collectFunc func(ctx context.Context) (Result, error)
|
|
}
|
|
|
|
func (m *mockCollector) Collect(ctx context.Context) (Result, error) {
|
|
return m.collectFunc(ctx)
|
|
}
|
|
|
|
func TestCollectorInterface(t *testing.T) {
|
|
c := &mockCollector{
|
|
BaseCollector: NewBaseCollector("test", "0 8 * * *", 30*time.Second, 3, "v1.0"),
|
|
collectFunc: func(ctx context.Context) (Result, error) {
|
|
return Result{
|
|
Models: []ModelInfo{{ID: "test/model-1", Name: "Test Model"}},
|
|
Meta: CollectionMeta{Source: "test", Count: 1},
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
// 测试接口方法
|
|
if c.Name() != "test" {
|
|
t.Errorf("Name() = %q, want %q", c.Name(), "test")
|
|
}
|
|
if c.Schedule() != "0 8 * * *" {
|
|
t.Errorf("Schedule() = %q, want %q", c.Schedule(), "0 8 * * *")
|
|
}
|
|
if c.Timeout() != 30*time.Second {
|
|
t.Errorf("Timeout() = %v, want %v", c.Timeout(), 30*time.Second)
|
|
}
|
|
if c.RetryCount() != 3 {
|
|
t.Errorf("RetryCount() = %d, want %d", c.RetryCount(), 3)
|
|
}
|
|
|
|
// 测试 Collect
|
|
ctx := context.Background()
|
|
result, err := c.Collect(ctx)
|
|
if err != nil {
|
|
t.Fatalf("Collect() error = %v", err)
|
|
}
|
|
if len(result.Models) != 1 {
|
|
t.Errorf("len(Models) = %d, want 1", len(result.Models))
|
|
}
|
|
if result.Meta.Count != 1 {
|
|
t.Errorf("Meta.Count = %d, want 1", result.Meta.Count)
|
|
}
|
|
}
|
|
|
|
func TestCollectorRegistry(t *testing.T) {
|
|
reg := NewRegistry()
|
|
|
|
c1 := &mockCollector{
|
|
BaseCollector: NewBaseCollector("openrouter", "0 8 * * *", 30*time.Second, 3, "v1.0"),
|
|
collectFunc: func(ctx context.Context) (Result, error) { return Result{}, nil },
|
|
}
|
|
c2 := &mockCollector{
|
|
BaseCollector: NewBaseCollector("siliconflow", "0 9 * * *", 30*time.Second, 3, "v1.0"),
|
|
collectFunc: func(ctx context.Context) (Result, error) { return Result{}, nil },
|
|
}
|
|
|
|
reg.Register(c1)
|
|
reg.Register(c2)
|
|
|
|
// 测试 Get
|
|
got, ok := reg.Get("openrouter")
|
|
if !ok {
|
|
t.Fatal("Get(openrouter) not found")
|
|
}
|
|
if got.Name() != "openrouter" {
|
|
t.Errorf("Get() Name = %q, want %q", got.Name(), "openrouter")
|
|
}
|
|
|
|
// 测试 Names
|
|
names := reg.Names()
|
|
if len(names) != 2 {
|
|
t.Errorf("Names() len = %d, want 2", len(names))
|
|
}
|
|
|
|
// 测试 All
|
|
all := reg.All()
|
|
if len(all) != 2 {
|
|
t.Errorf("All() len = %d, want 2", len(all))
|
|
}
|
|
|
|
// 测试不存在的采集器
|
|
_, ok = reg.Get("nonexistent")
|
|
if ok {
|
|
t.Error("Get(nonexistent) should return false")
|
|
}
|
|
}
|
|
|
|
func TestCollectorTimeout(t *testing.T) {
|
|
c := &mockCollector{
|
|
BaseCollector: NewBaseCollector("slow", "0 8 * * *", 100*time.Millisecond, 0, "v1.0"),
|
|
collectFunc: func(ctx context.Context) (Result, error) {
|
|
// 模拟耗时操作
|
|
select {
|
|
case <-time.After(200 * time.Millisecond):
|
|
return Result{}, nil
|
|
case <-ctx.Done():
|
|
return Result{}, ctx.Err()
|
|
}
|
|
},
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
|
|
_, err := c.Collect(ctx)
|
|
if err == nil {
|
|
t.Error("Expected timeout error, got nil")
|
|
}
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Errorf("Expected DeadlineExceeded, got %v", err)
|
|
}
|
|
}
|