|
| 1 | +package extend |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// noopDispatch is a Dispatcher that echoes args back with no error. |
| 12 | +func noopDispatch(_ context.Context, _, _ string, a HookArgs) (HookArgs, error) { |
| 13 | + return a, nil |
| 14 | +} |
| 15 | + |
| 16 | +// TestRun_RateLimitsPerApp asserts that once an app's burst budget is |
| 17 | +// spent, Run refuses further hook dispatch with ErrRateLimited, and that |
| 18 | +// the budget refills as the (injected) clock advances. |
| 19 | +func TestRun_RateLimitsPerApp(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + r := NewRegistry(noopDispatch) |
| 22 | + if err := r.Register(Extension{AppID: "io.spammer", Primitive: PreSendMessage, Method: "h"}); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + // 1 token/sec, burst 3. |
| 26 | + r.SetRateLimit(1, 3) |
| 27 | + |
| 28 | + // Pin the clock so refill is deterministic. |
| 29 | + base := time.Unix(1_000_000, 0) |
| 30 | + now := base |
| 31 | + r.mu.Lock() |
| 32 | + r.limiter.now = func() time.Time { return now } |
| 33 | + r.mu.Unlock() |
| 34 | + |
| 35 | + ctx := context.Background() |
| 36 | + // First 3 calls consume the burst. |
| 37 | + for i := 0; i < 3; i++ { |
| 38 | + if _, err := r.Run(ctx, PreSendMessage, HookArgs{}); err != nil { |
| 39 | + t.Fatalf("call %d should pass, got %v", i, err) |
| 40 | + } |
| 41 | + } |
| 42 | + // 4th call (no time elapsed) is rate-limited. |
| 43 | + if _, err := r.Run(ctx, PreSendMessage, HookArgs{}); !errors.Is(err, ErrRateLimited) { |
| 44 | + t.Fatalf("4th call err = %v, want ErrRateLimited", err) |
| 45 | + } |
| 46 | + // Advance 1s → exactly one token refills → one call passes, next fails. |
| 47 | + now = base.Add(time.Second) |
| 48 | + if _, err := r.Run(ctx, PreSendMessage, HookArgs{}); err != nil { |
| 49 | + t.Fatalf("post-refill call should pass, got %v", err) |
| 50 | + } |
| 51 | + if _, err := r.Run(ctx, PreSendMessage, HookArgs{}); !errors.Is(err, ErrRateLimited) { |
| 52 | + t.Fatalf("call after refill drained err = %v, want ErrRateLimited", err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// TestRun_RateLimitIsPerApp confirms one app exhausting its budget does |
| 57 | +// not block a different app's hooks. |
| 58 | +func TestRun_RateLimitIsPerApp(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + r := NewRegistry(noopDispatch) |
| 61 | + _ = r.Register(Extension{AppID: "a", Primitive: PreSendMessage, Method: "h", Order: 1}) |
| 62 | + _ = r.Register(Extension{AppID: "b", Primitive: PostRecvMessage, Method: "h", Order: 1}) |
| 63 | + r.SetRateLimit(1, 1) |
| 64 | + now := time.Unix(2_000_000, 0) |
| 65 | + r.mu.Lock() |
| 66 | + r.limiter.now = func() time.Time { return now } |
| 67 | + r.mu.Unlock() |
| 68 | + |
| 69 | + ctx := context.Background() |
| 70 | + // Drain app "a". |
| 71 | + if _, err := r.Run(ctx, PreSendMessage, HookArgs{}); err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + if _, err := r.Run(ctx, PreSendMessage, HookArgs{}); !errors.Is(err, ErrRateLimited) { |
| 75 | + t.Fatalf("app a should be limited, got %v", err) |
| 76 | + } |
| 77 | + // App "b" still has its own full budget. |
| 78 | + if _, err := r.Run(ctx, PostRecvMessage, HookArgs{}); err != nil { |
| 79 | + t.Fatalf("app b should not be limited, got %v", err) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// TestRun_NoLimiterByDefault confirms back-compat: without SetRateLimit, |
| 84 | +// many invocations all pass. |
| 85 | +func TestRun_NoLimiterByDefault(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + r := NewRegistry(noopDispatch) |
| 88 | + _ = r.Register(Extension{AppID: "a", Primitive: PreSendMessage, Method: "h"}) |
| 89 | + for i := 0; i < 1000; i++ { |
| 90 | + if _, err := r.Run(context.Background(), PreSendMessage, HookArgs{}); err != nil { |
| 91 | + t.Fatalf("unlimited registry should never rate-limit, got %v at %d", err, i) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// TestDaemonHandler_CapsDynamicRegistrations asserts an app cannot exceed |
| 97 | +// maxDynamicRegistrationsPerApp dynamic hook registrations. |
| 98 | +func TestDaemonHandler_CapsDynamicRegistrations(t *testing.T) { |
| 99 | + t.Parallel() |
| 100 | + reg := NewRegistry(noopDispatch) |
| 101 | + h := NewDaemonHandler(reg, AllowAll) |
| 102 | + |
| 103 | + for i := 0; i < maxDynamicRegistrationsPerApp; i++ { |
| 104 | + err := h.Register("io.greedy", Extension{ |
| 105 | + Primitive: PreSendMessage, |
| 106 | + Method: fmt.Sprintf("m%d", i), |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("registration %d should succeed, got %v", i, err) |
| 110 | + } |
| 111 | + } |
| 112 | + // One past the cap must be refused. |
| 113 | + err := h.Register("io.greedy", Extension{Primitive: PreSendMessage, Method: "overflow"}) |
| 114 | + if !errors.Is(err, ErrTooManyRegistrations) { |
| 115 | + t.Fatalf("over-cap registration err = %v, want ErrTooManyRegistrations", err) |
| 116 | + } |
| 117 | + // A different app is unaffected. |
| 118 | + if err := h.Register("io.modest", Extension{Primitive: PreSendMessage, Method: "m"}); err != nil { |
| 119 | + t.Fatalf("other app should still register, got %v", err) |
| 120 | + } |
| 121 | + // After unregistering one of greedy's hooks, it can register again. |
| 122 | + reg.UnregisterOne("io.greedy", PreSendMessage, "m0") |
| 123 | + if err := h.Register("io.greedy", Extension{Primitive: PreSendMessage, Method: "again"}); err != nil { |
| 124 | + t.Fatalf("after freeing a slot, registration should succeed, got %v", err) |
| 125 | + } |
| 126 | +} |
0 commit comments