Debian Codegraph @

api/v1/lib/extras/gen/rules.go

raw symbols SCIP
1 // +build ignore
2
3 package main
4
5 import (
6 "os"
7 "text/template"
8 )
9
10 func main() {
11 Run(rulesTemplate, rulesTestTemplate, os.Args...)
12 }
13
14 var rulesTemplate = template.Must(template.New("").Parse(`package {{.Package}}
15
16 // go generate {{.Args}}
17 // GENERATED CODE FOLLOWS; DO NOT EDIT.
18
19 import (
20 "context"
21 "fmt"
22 "sync"
23 {{range .Imports}}
24 {{ printf "%q" . -}}
25 {{end}}
26 )
27
28 {{.RequireType "E" -}}
29 {{.RequirePrototype "E" -}}
30 {{.RequirePrototype "Z" -}}{{/* Z is an optional type, but if it's specified then it needs a prototype */ -}}
31 type (
32 evaler interface {
33 // Eval executes a filter, rule, or decorator function; if the returned event is nil then
34 // no additional rule evaluation should be processed for the event.
35 // Eval implementations should not modify the given event parameter (to avoid side effects).
36 // If changes to the event object are needed, the suggested approach is to make a copy,
37 // modify the copy, and pass the copy to the chain.
38 // Eval implementations SHOULD be safe to execute concurrently.
39 Eval(context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error, Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error)
40 }
41
42 // Rule is the functional adaptation of evaler.
43 // A nil Rule is valid: it is Eval'd as a noop.
44 Rule func(context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error, Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error)
45
46 // Chain is invoked by a Rule to continue processing an event. If the chain is not invoked,
47 // no additional rules are processed.
48 Chain func(context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error)
49
50 // Rules is a list of rules to be processed, in order.
51 Rules []Rule
52
53 // ErrorList accumulates errors that occur while processing a Chain of Rules. Accumulated
54 // errors should be appended to the end of the list. An error list should never be empty.
55 // Callers should use the package Error() func to properly accumulate (and flatten) errors.
56 ErrorList []error
57 )
58
59 var (
60 _ = evaler(Rule(nil))
61 _ = evaler(Rules{})
62 )
63
64 // ChainIdentity is a Chain that returns the arguments as its results.
65 func ChainIdentity(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
66 return ctx, e, {{.Ref "Z" "z," -}} err
67 }
68
69 // Eval is a convenience func that processes a nil Rule as a noop.
70 func (r Rule) Eval(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
71 if r != nil {
72 return r(ctx, e, {{.Ref "Z" "z," -}} err, ch)
73 }
74 return ch(ctx, e, {{.Ref "Z" "z," -}} err)
75 }
76
77 // Eval is a Rule func that processes the set of all Rules. If there are no rules in the
78 // set then control is simply passed to the Chain.
79 func (rs Rules) Eval(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
80 return ch(rs.Chain()(ctx, e, {{.Ref "Z" "z," -}} err))
81 }
82
83 // Chain returns a Chain that evaluates the given Rules, in order, propagating the (context.Context, {{.Type "E"}}, error)
84 // from Rule to Rule. Chain is safe to invoke concurrently.
85 func (rs Rules) Chain() Chain {
86 if len(rs) == 0 {
87 return ChainIdentity
88 }
89 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
90 return rs[0].Eval(ctx, e, {{.Ref "Z" "z," -}} err, rs[1:].Chain())
91 }
92 }
93
94 // New is the semantic equivalent of Rules{r1, r2, ..., rn}.Eval and exists purely for convenience.
95 func New(rs ...Rule) Rule { return Rules(rs).Eval }
96
97 const msgNoErrors = "no errors"
98
99 // Error implements error; returns the message of the first error in the list.
100 func (es ErrorList) Error() string {
101 switch len(es) {
102 case 0:
103 return msgNoErrors
104 case 1:
105 return es[0].Error()
106 default:
107 return fmt.Sprintf("%s (and %d more errors)", es[0], len(es)-1)
108 }
109 }
110
111 // Error2 aggregates the given error params, returning nil if both are nil.
112 // Use Error2 to avoid the overhead of creating a slice when aggregating only 2 errors.
113 func Error2(a, b error) error {
114 if a == nil {
115 if b == nil {
116 return nil
117 }
118 if list, ok := b.(ErrorList); ok {
119 return flatten(list).Err()
120 }
121 return b
122 }
123 if b == nil {
124 if list, ok := a.(ErrorList); ok {
125 return flatten(list).Err()
126 }
127 return a
128 }
129 return Error(a, b)
130 }
131
132 // Err reduces an empty or singleton error list
133 func (es ErrorList) Err() error {
134 switch len(es) {
135 case 0:
136 return nil
137 case 1:
138 return es[0]
139 default:
140 return es
141 }
142 }
143
144 // IsErrorList returns true if err is a non-nil error list
145 func IsErrorList(err error) bool {
146 if err != nil {
147 _, ok := err.(ErrorList)
148 return ok
149 }
150 return false
151 }
152
153 // Error aggregates, and then flattens, a list of errors accrued during rule processing.
154 // Returns nil if the given list of errors is empty or contains all nil errors.
155 func Error(es ...error) error {
156 return flatten(es).Err()
157 }
158
159 func flatten(errors []error) ErrorList {
160 if errors == nil || len(errors) == 0 {
161 return nil
162 }
163 result := make([]error, 0, len(errors))
164 for _, err := range errors {
165 if err != nil {
166 if multi, ok := err.(ErrorList); ok {
167 result = append(result, flatten(multi)...)
168 } else {
169 result = append(result, err)
170 }
171 }
172 }
173 return ErrorList(result)
174 }
175
176 // TODO(jdef): other ideas for Rule decorators: When(func() bool), WhenNot(func() bool)
177
178 // If only executes the receiving rule if b is true; otherwise, the returned rule is a noop.
179 func (r Rule) If(b bool) Rule {
180 if b {
181 return r
182 }
183 return nil
184 }
185
186 // Unless only executes the receiving rule if b is false; otherwise, the returned rule is a noop.
187 func (r Rule) Unless(b bool) Rule {
188 if !b {
189 return r
190 }
191 return nil
192 }
193
194 // Once returns a Rule that executes the receiver only once.
195 func (r Rule) Once() Rule {
196 if r == nil {
197 return nil
198 }
199 var once sync.Once
200 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
201 ruleInvoked := false
202 once.Do(func() {
203 ctx, e, {{.Ref "Z" "z," -}} err = r(ctx, e, {{.Ref "Z" "z," -}} err, ch)
204 ruleInvoked = true
205 })
206 if !ruleInvoked {
207 ctx, e, {{.Ref "Z" "z," -}} err = ch(ctx, e, {{.Ref "Z" "z," -}} err)
208 }
209 return ctx, e, {{.Ref "Z" "z," -}} err
210 }
211 }
212
213 // UnlessDone returns a decorated rule that checks context.Done: if the context has been canceled then the rule chain
214 // is aborted and the context.Err is merged with the current error state.
215 // Returns nil (noop) if the receiving Rule is nil.
216 func (r Rule) UnlessDone() Rule {
217 if r == nil {
218 return nil
219 }
220 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
221 select {
222 case <-ctx.Done():
223 return ctx, e, {{.Ref "Z" "z," -}} Error2(err, ctx.Err())
224 default:
225 return r(ctx, e, {{.Ref "Z" "z," -}} err, ch)
226 }
227 }
228 }
229
230 type Overflow int
231
232 const (
233 // OverflowWait waits until the rule may execute, or the context is canceled.
234 OverflowWait Overflow = iota
235 // OverflowOtherwise skips over the decorated rule and invoke an alternative instead.
236 OverflowOtherwise
237 )
238
239 // RateLimit invokes the receiving Rule if a read of chan "p" succeeds (closed chan = no rate limit), otherwise proceeds
240 // according to the specified Overflow policy. May be useful, for example, when rate-limiting logged events.
241 // Returns nil (noop) if the receiver is nil, otherwise a nil chan will normally trigger an overflow.
242 // Panics when OverflowWait is specified with a nil chan, in order to prevent deadlock.
243 // A cancelled context will trigger the "otherwise" rule.
244 func (r Rule) RateLimit(p <-chan struct{}, over Overflow, otherwise Rule) Rule {
245 return limit(r, acquireChan(p), over, otherwise)
246 }
247
248 // acquireChan wraps a signal chan with a func that can be used with rateLimit.
249 // should only be called by rate limiting funcs (that implement deadlock avoidance).
250 func acquireChan(tokenCh <-chan struct{}) func(context.Context, bool) bool {
251 if tokenCh == nil {
252 // always false: acquire never succeeds; panic if told to block (to avoid deadlock)
253 return func(ctx context.Context, block bool) bool {
254 if block {
255 select {
256 case <-ctx.Done():
257 default:
258 panic("deadlock detected: block should never be true when the token chan is nil")
259 }
260 }
261 return false
262 }
263 }
264 return func(ctx context.Context, block bool) bool {
265 if block {
266 select {
267 case <-tokenCh:
268 // tie breaker prefers Done
269 select {
270 case <-ctx.Done():
271 default:
272 return true
273 }
274 case <-ctx.Done():
275 }
276 return false
277 }
278 select {
279 case <-tokenCh:
280 return true
281 default:
282 return false
283 }
284 }
285 }
286
287 // limit is a generic Rule decorator that limits invocations of said Rule.
288 // The "acquire" func SHOULD NOT block if the supplied Context is Done.
289 // MUST only invoke "acquire" once per event.
290 // TODO(jdef): leaving this as internal for now because the interface still feels too messy.
291 func limit(r Rule, acquire func(_ context.Context, block bool) bool, over Overflow, otherwise Rule) Rule {
292 if r == nil {
293 return nil
294 }
295 if acquire == nil {
296 panic("acquire func is not allowed to be nil")
297 }
298 blocking := false
299 switch over {
300 case OverflowOtherwise:
301 case OverflowWait:
302 blocking = true
303 default:
304 panic(fmt.Sprintf("unexpected Overflow type: %#v", over))
305 }
306 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
307 if !acquire(ctx, blocking) {
308 return otherwise.Eval(ctx, e, {{.Ref "Z" "z," -}} err, ch)
309 }
310 return r(ctx, e, {{.Ref "Z" "z," -}} err, ch)
311 }
312 }
313
314 /* TODO(jdef) not sure that this is very useful, leaving out for now...
315
316 // EveryN invokes the receiving rule beginning with the first event seen and then every n'th
317 // time after that. If nthTime is less then 2 then the receiver is returned, undecorated.
318 // The "otherwise" Rule (may be null) is invoked for every event in between the n'th invocations.
319 // A cancelled context will trigger the "otherwise" rule.
320 func (r Rule) EveryN(nthTime int, otherwise Rule) Rule {
321 if nthTime < 2 || r == nil {
322 return r
323 }
324 return limit(r, acquireEveryN(nthTime), OverflowOtherwise, otherwise)
325 }
326
327 // acquireEveryN returns an "acquire" func (for use w/ rate-limiting) that returns true every N'th invocation.
328 // the returned func MUST NOT be used with a potentially blocking Overflow policy (or else it panics).
329 // nthTime SHOULD be greater than math.MinInt32, values less than 2 probably don't make sense in practice.
330 func acquireEveryN(nthTime int) func(context.Context, bool) bool {
331 var (
332 i = 1 // begin with the first event seen
333 m sync.Mutex
334 )
335 return func(ctx context.Context, block bool) (result bool) {
336 if block {
337 panic("acquireEveryN should never be asked to block")
338 }
339 select {
340 case <-ctx.Done():
341 default:
342 m.Lock()
343 i--
344 if i <= 0 {
345 i = nthTime
346 result = true
347 }
348 m.Unlock()
349 }
350 return
351 }
352 }
353
354 */
355
356 // Drop aborts the Chain and returns the (context.Context, {{.Type "E"}}, error) tuple as-is.
357 func Drop() Rule {
358 return Rule(nil).ThenDrop()
359 }
360
361 // ThenDrop executes the receiving rule, but aborts the Chain, and returns the (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) tuple as-is.
362 func (r Rule) ThenDrop() Rule {
363 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, _ Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
364 return r.Eval(ctx, e, {{.Ref "Z" "z," -}} err, ChainIdentity)
365 }
366 }
367
368 // Fail returns a Rule that injects the given error.
369 func Fail(injected error) Rule {
370 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
371 return ch(ctx, e, {{.Ref "Z" "z," -}} Error2(err, injected))
372 }
373 }
374
375 // DropOnError returns a Rule that generates a nil event if the error state != nil
376 func DropOnError() Rule {
377 return Rule(nil).DropOnError()
378 }
379
380 // DropOnError decorates a rule by pre-checking the error state: if the error state != nil then
381 // the receiver is not invoked and (e, err) is returned; otherwise control passes to the receiving rule.
382 func (r Rule) DropOnError() Rule {
383 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
384 if err != nil {
385 return ctx, e, {{.Ref "Z" "z," -}} err
386 }
387 return r.Eval(ctx, e, {{.Ref "Z" "z," -}} err, ch)
388 }
389 }
390
391 // AndThen returns a list of rules, beginning with the receiver, followed by DropOnError, and then
392 // all of the rules specified by the next parameter. The net effect is: execute the receiver rule
393 // and only if there is no error state, continue processing the next rules, in order.
394 func (r Rule) AndThen(next ...Rule) Rule {
395 return append(Rules{r, DropOnError()}, next...).Eval
396 }
397
398 func DropOnSuccess() Rule {
399 return Rule(nil).DropOnSuccess()
400 }
401
402 func (r Rule) DropOnSuccess() Rule {
403 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
404 if err == nil {
405 // bypass remainder of chain
406 return ctx, e, {{.Ref "Z" "z," -}} err
407 }
408 return r.Eval(ctx, e, {{.Ref "Z" "z," -}} err, ch)
409 }
410 }
411
412 func (r Rule) OnFailure(next ...Rule) Rule {
413 return append(Rules{r, DropOnSuccess()}, next...).Eval
414 }
415 `))
416
417 var rulesTestTemplate = template.Must(template.New("").Parse(`package {{.Package}}
418
419 // go generate {{.Args}}
420 // GENERATED CODE FOLLOWS; DO NOT EDIT.
421
422 import (
423 "context"
424 "errors"
425 "reflect"
426 "testing"
427 {{range .Imports}}
428 {{ printf "%q" . -}}
429 {{end}}
430 )
431
432 func prototype() {{.Type "E"}} { return {{.Prototype "E"}} }
433
434 func counter(i *int) Rule {
435 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
436 *i++
437 return ch(ctx, e, {{.Ref "Z" "z," -}} err)
438 }
439 }
440
441 func tracer(r Rule, name string, t *testing.T) Rule {
442 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
443 t.Log("executing", name)
444 return r(ctx, e, {{.Ref "Z" "z," -}} err, ch)
445 }
446 }
447
448 func returnError(re error) Rule {
449 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error, ch Chain) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
450 return ch(ctx, e, {{.Ref "Z" "z," -}} Error2(err, re))
451 }
452 }
453
454 func chainCounter(i *int, ch Chain) Chain {
455 return func(ctx context.Context, e {{.Type "E"}}, {{.Arg "Z" "z," -}} err error) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
456 *i++
457 return ch(ctx, e, {{.Ref "Z" "z," -}} err)
458 }
459 }
460
461 func chainPanic(x interface{}) Chain {
462 return func(_ context.Context, _ {{.Type "E"}}, {{.Arg "Z" "_," -}} _ error) (context.Context, {{.Type "E"}}, {{.Arg "Z" "," -}} error) {
463 panic(x)
464 }
465 }
466
467 func TestChainIdentity(t *testing.T) {
468 var i int
469 counterRule := counter(&i)
470 {{if .Type "Z"}}
471 {{.Var "Z" "z0"}}
472 {{end}}
473 _, e, {{.Ref "Z" "_," -}} err := Rules{counterRule}.Eval(context.Background(), nil, {{.Ref "Z" "z0," -}} nil, ChainIdentity)
474 if e != nil {
475 t.Error("expected nil event instead of", e)
476 }
477 if err != nil {
478 t.Error("expected nil error instead of", err)
479 }
480 if i != 1 {
481 t.Error("expected 1 rule execution instead of", i)
482 }
483 }
484
485 func TestRules(t *testing.T) {
486 var (
487 p = prototype()
488 a = errors.New("a")
489 ctx = context.Background()
490 )
491
492 {{if .Type "Z" -}}
493 {{.Var "Z" "z0"}}
494 var zp = {{.Prototype "Z"}}
495 {{end -}}
496
497 // multiple rules in Rules should execute, dropping nil rules along the way
498 for _, tc := range []struct {
499 e {{.Type "E"}}
500 {{if .Type "Z"}}
501 {{- .Arg "Z" "z "}}
502 {{end -}}
503 err error
504 }{
505 {nil, {{.Ref "Z" "z0," -}} nil},
506 {nil, {{.Ref "Z" "z0," -}} a},
507 {p, {{.Ref "Z" "z0," -}} nil},
508 {p, {{.Ref "Z" "z0," -}} a},
509 {{if .Type "Z"}}
510 {nil, {{.Ref "Z" "zp," -}} nil},
511 {nil, {{.Ref "Z" "zp," -}} a},
512 {p, {{.Ref "Z" "zp," -}} nil},
513 {p, {{.Ref "Z" "zp," -}} a},
514 {{end}} } {
515 var (
516 i int
517 rule = New(
518 nil,
519 tracer(counter(&i), "counter1", t),
520 nil,
521 tracer(counter(&i), "counter2", t),
522 nil,
523 )
524 _, e, {{.Ref "Z" "zz," -}} err = rule(ctx, tc.e, {{.Ref "Z" "tc.z," -}} tc.err, ChainIdentity)
525 )
526 if e != tc.e {
527 t.Errorf("expected prototype event %q instead of %q", tc.e, e)
528 }
529 {{if .Type "Z" -}}
530 if zz != tc.z {
531 t.Errorf("expected return object %q instead of %q", tc.z, zz)
532 }
533 {{end -}}
534 if err != tc.err {
535 t.Errorf("expected %q error instead of %q", tc.err, err)
536 }
537 if i != 2 {
538 t.Error("expected 2 rule executions instead of", i)
539 }
540
541 // empty Rules should not change event, {{.Ref "Z" "z," -}} err
542 _, e, {{.Ref "Z" "zz," -}} err = Rules{}.Eval(ctx, tc.e, {{.Ref "Z" "tc.z," -}} tc.err, ChainIdentity)
543 if e != tc.e {
544 t.Errorf("expected prototype event %q instead of %q", tc.e, e)
545 }
546 {{if .Type "Z" -}}
547 if zz != tc.z {
548 t.Errorf("expected return object %q instead of %q", tc.z, zz)
549 }
550 {{end -}}
551 if err != tc.err {
552 t.Errorf("expected %q error instead of %q", tc.err, err)
553 }
554 }
555 }
556
557 func TestError(t *testing.T) {
558 a := errors.New("a")
559 list := ErrorList{a}
560
561 msg := list.Error()
562 if msg != a.Error() {
563 t.Errorf("expected %q instead of %q", a.Error(), msg)
564 }
565
566 msg = ErrorList{}.Error()
567 if msg != msgNoErrors {
568 t.Errorf("expected %q instead of %q", msgNoErrors, msg)
569 }
570
571 msg = ErrorList(nil).Error()
572 if msg != msgNoErrors {
573 t.Errorf("expected %q instead of %q", msgNoErrors, msg)
574 }
575 }
576
577 func TestError2(t *testing.T) {
578 var (
579 a = errors.New("a")
580 b = errors.New("b")
581 )
582 for i, tc := range []struct {
583 a error
584 b error
585 wants error
586 wantsMessage string
587 }{
588 {nil, nil, nil, ""},
589 {nil, ErrorList{nil}, nil, ""},
590 {ErrorList{nil}, ErrorList{nil}, nil, ""},
591 {ErrorList{ErrorList{nil}}, ErrorList{nil}, nil, ""},
592 {a, nil, a, "a"},
593 {ErrorList{a}, nil, a, "a"},
594 {ErrorList{nil, a, ErrorList{}}, nil, a, "a"},
595 {nil, b, b, "b"},
596 {nil, ErrorList{b}, b, "b"},
597 {a, b, ErrorList{a, b}, "a (and 1 more errors)"},
598 {a, ErrorList{b}, ErrorList{a, b}, "a (and 1 more errors)"},
599 {a, ErrorList{nil, ErrorList{b, ErrorList{}, nil}}, ErrorList{a, b}, "a (and 1 more errors)"},
600 } {
601 var (
602 sameError bool
603 result = Error2(tc.a, tc.b)
604 )
605 // jump through hoops because we can't directly compare two errors with == if
606 // they're both ErrorList.
607 if IsErrorList(result) == IsErrorList(tc.wants) { // both are lists or neither
608 sameError = (!IsErrorList(result) && result == tc.wants) ||
609 (IsErrorList(result) && reflect.DeepEqual(result, tc.wants))
610 }
611 if !sameError {
612 t.Fatalf("test case %d failed, expected %v instead of %v", i, tc.wants, result)
613 }
614 if result != nil && tc.wantsMessage != result.Error() {
615 t.Fatalf("test case %d failed, expected message %q instead of %q",
616 i, tc.wantsMessage, result.Error())
617 }
618 }
619 }
620
621 func TestUnlessDone(t *testing.T) {
622 var (
623 p = prototype()
624 ctx = context.Background()
625 fin = func() context.Context {
626 c, cancel := context.WithCancel(context.Background())
627 cancel()
628 return c
629 }()
630 )
631 {{if .Type "Z" -}}
632 var zp = {{.Prototype "Z"}}
633 {{end -}}
634 for ti, tc := range []struct {
635 ctx context.Context
636 wantsError []error
637 wantsRuleCount []int
638 wantsChainCount []int
639 }{
640 {ctx, []error{nil, nil}, []int{1, 2}, []int{1, 2}},
641 {fin, []error{nil, context.Canceled}, []int{1, 1}, []int{1, 1}},
642 } {
643 var (
644 i, j int
645 r1 = counter(&i)
646 r2 = r1.UnlessDone()
647 )
648 for k, r := range []Rule{r1, r2} {
649 _, e, {{.Ref "Z" "zz," -}} err := r(tc.ctx, p, {{.Ref "Z" "zp," -}} nil, chainCounter(&j, ChainIdentity))
650 if e != p {
651 t.Errorf("test case %d failed: expected event %q instead of %q", ti, p, e)
652 }
653 {{if .Type "Z" -}}
654 if zz != zp {
655 t.Errorf("test case %d failed: expected return object %q instead of %q", ti, zp, zz)
656 }
657 {{end -}}
658 if err != tc.wantsError[k] {
659 t.Errorf("test case %d failed: unexpected error %v", ti, err)
660 }
661 if i != tc.wantsRuleCount[k] {
662 t.Errorf("test case %d failed: expected count of %d instead of %d", ti, tc.wantsRuleCount[k], i)
663 }
664 if j != tc.wantsChainCount[k] {
665 t.Errorf("test case %d failed: expected chain count of %d instead of %d", ti, tc.wantsRuleCount[k], j)
666 }
667 }
668 }
669 r := Rule(nil).UnlessDone()
670 if r != nil {
671 t.Error("expected nil result from UnlessDone")
672 }
673 }
674
675 func TestAndThen(t *testing.T) {
676 var (
677 i, j int
678 p = prototype()
679 ctx = context.Background()
680 r1 = counter(&i)
681 r2 = Rule(nil).AndThen(counter(&i))
682 a = errors.New("a")
683 )
684 {{if .Type "Z" -}}
685 var zp = {{.Prototype "Z"}}
686 {{end -}}
687 for k, r := range []Rule{r1, r2} {
688 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} a, chainCounter(&j, ChainIdentity))
689 if e != p {
690 t.Errorf("expected event %q instead of %q", p, e)
691 }
692 {{if .Type "Z" -}}
693 if zz != zp {
694 t.Errorf("expected return object %q instead of %q", zp, zz)
695 }
696 {{end -}}
697 if err != a {
698 t.Error("unexpected error", err)
699 }
700 if i != 1 {
701 t.Errorf("expected count of 1 instead of %d", i)
702 }
703 if j != (k + 1) {
704 t.Errorf("expected chain count of %d instead of %d", (k + 1), j)
705 }
706 }
707 }
708
709 func TestOnFailure(t *testing.T) {
710 var (
711 i, j int
712 p = prototype()
713 ctx = context.Background()
714 a = errors.New("a")
715 r1 = counter(&i)
716 r2 = Fail(a).OnFailure(counter(&i))
717 )
718 {{if .Type "Z" -}}
719 var zp = {{.Prototype "Z"}}
720 {{end -}}
721 for k, tc := range []struct {
722 r Rule
723 initialError error
724 }{
725 {r1, a},
726 {r2, nil},
727 } {
728 _, e, {{.Ref "Z" "zz," -}} err := tc.r(ctx, p, {{.Ref "Z" "zp," -}} tc.initialError, chainCounter(&j, ChainIdentity))
729 if e != p {
730 t.Errorf("expected event %q instead of %q", p, e)
731 }
732 {{if .Type "Z" -}}
733 if zz != zp {
734 t.Errorf("expected return object %q instead of %q", zp, zz)
735 }
736 {{end -}}
737 if err != a {
738 t.Error("unexpected error", err)
739 }
740 if i != (k + 1) {
741 t.Errorf("expected count of %d instead of %d", (k + 1), i)
742 }
743 if j != (k + 1) {
744 t.Errorf("expected chain count of %d instead of %d", (k + 1), j)
745 }
746 }
747 }
748
749 func TestDropOnError(t *testing.T) {
750 var (
751 i, j int
752 p = prototype()
753 ctx = context.Background()
754 r1 = counter(&i)
755 r2 = counter(&i).DropOnError()
756 a = errors.New("a")
757 )
758 {{if .Type "Z" -}}
759 var zp = {{.Prototype "Z"}}
760 {{end -}}
761 // r1 should execute the counter rule
762 // r2 should NOT exexute the counter rule
763 for _, r := range []Rule{r1, r2} {
764 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} a, chainCounter(&j, ChainIdentity))
765 if e != p {
766 t.Errorf("expected event %q instead of %q", p, e)
767 }
768 {{if .Type "Z" -}}
769 if zz != zp {
770 t.Errorf("expected return object %q instead of %q", zp, zz)
771 }
772 {{end -}}
773 if err != a {
774 t.Error("unexpected error", err)
775 }
776 if i != 1 {
777 t.Errorf("expected count of 1 instead of %d", i)
778 }
779 if j != 1 {
780 t.Errorf("expected chain count of 1 instead of %d", j)
781 }
782 }
783 _, e, {{.Ref "Z" "zz," -}} err := r2(ctx, p, {{.Ref "Z" "zp," -}} nil, chainCounter(&j, ChainIdentity))
784 if e != p {
785 t.Errorf("expected event %q instead of %q", p, e)
786 }
787 {{if .Type "Z" -}}
788 if zz != zp {
789 t.Errorf("expected return object %q instead of %q", zp, zz)
790 }
791 {{end -}}
792 if err != nil {
793 t.Error("unexpected error", err)
794 }
795 if j != 2 {
796 t.Errorf("expected chain count of 2 instead of %d", j)
797 }
798 }
799
800 func TestDropOnSuccess(t *testing.T) {
801 var (
802 i, j int
803 p = prototype()
804 ctx = context.Background()
805 r1 = counter(&i)
806 r2 = counter(&i).DropOnSuccess()
807 )
808 {{if .Type "Z" -}}
809 var zp = {{.Prototype "Z"}}
810 {{end -}}
811 // r1 should execute the counter rule
812 // r2 should NOT exexute the counter rule
813 for _, r := range []Rule{r1, r2} {
814 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} nil, chainCounter(&j, ChainIdentity))
815 if e != p {
816 t.Errorf("expected event %q instead of %q", p, e)
817 }
818 {{if .Type "Z" -}}
819 if zz != zp {
820 t.Errorf("expected return object %q instead of %q", zp, zz)
821 }
822 {{end -}}
823 if err != nil {
824 t.Error("unexpected error", err)
825 }
826 if i != 1 {
827 t.Errorf("expected count of 1 instead of %d", i)
828 }
829 if j != 1 {
830 t.Errorf("expected chain count of 1 instead of %d", j)
831 }
832 }
833 a := errors.New("a")
834 _, e, {{.Ref "Z" "zz," -}} err := r2(ctx, p, {{.Ref "Z" "zp," -}} a, chainCounter(&j, ChainIdentity))
835 if e != p {
836 t.Errorf("expected event %q instead of %q", p, e)
837 }
838 {{if .Type "Z" -}}
839 if zz != zp {
840 t.Errorf("expected return object %q instead of %q", zp, zz)
841 }
842 {{end -}}
843 if err != a {
844 t.Error("unexpected error", err)
845 }
846 if i != 2 {
847 t.Errorf("expected count of 2 instead of %d", i)
848 }
849 if j != 2 {
850 t.Errorf("expected chain count of 2 instead of %d", j)
851 }
852
853 r3 := Rules{DropOnSuccess(), r1}.Eval
854 _, e, {{.Ref "Z" "zz," -}} err = r3(ctx, p, {{.Ref "Z" "zp," -}} nil, chainCounter(&j, ChainIdentity))
855 if e != p {
856 t.Errorf("expected event %q instead of %q", p, e)
857 }
858 {{if .Type "Z" -}}
859 if zz != zp {
860 t.Errorf("expected return object %q instead of %q", zp, zz)
861 }
862 {{end -}}
863 if err != nil {
864 t.Error("unexpected error", err)
865 }
866 if i != 2 {
867 t.Errorf("expected count of 2 instead of %d", i)
868 }
869 if j != 3 {
870 t.Errorf("expected chain count of 3 instead of %d", j)
871 }
872 }
873
874 func TestThenDrop(t *testing.T) {
875 for _, anErr := range []error{nil, errors.New("a")} {
876 var (
877 i, j int
878 p = prototype()
879 ctx = context.Background()
880 r1 = counter(&i)
881 r2 = counter(&i).ThenDrop()
882 )
883 {{if .Type "Z" -}}
884 var zp = {{.Prototype "Z"}}
885 {{end -}}
886 // r1 and r2 should execute the counter rule
887 for k, r := range []Rule{r1, r2} {
888 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} anErr, chainCounter(&j, ChainIdentity))
889 if e != p {
890 t.Errorf("expected event %q instead of %q", p, e)
891 }
892 {{if .Type "Z" -}}
893 if zz != zp {
894 t.Errorf("expected return object %q instead of %q", zp, zz)
895 }
896 {{end -}}
897 if err != anErr {
898 t.Errorf("expected %v instead of error %v", anErr, err)
899 }
900 if i != (k + 1) {
901 t.Errorf("expected count of %d instead of %d", (k + 1), i)
902 }
903 if j != 1 {
904 t.Errorf("expected chain count of 1 instead of %d", j)
905 }
906 }
907 }
908 }
909
910 func TestDrop(t *testing.T) {
911 for _, anErr := range []error{nil, errors.New("a")} {
912 var (
913 i, j int
914 p = prototype()
915 ctx = context.Background()
916 r1 = counter(&i)
917 r2 = Rules{Drop(), counter(&i)}.Eval
918 )
919 {{if .Type "Z" -}}
920 var zp = {{.Prototype "Z"}}
921 {{end -}}
922 // r1 should execute the counter rule
923 // r2 should NOT exexute the counter rule
924 for k, r := range []Rule{r1, r2} {
925 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} anErr, chainCounter(&j, ChainIdentity))
926 if e != p {
927 t.Errorf("expected event %q instead of %q", p, e)
928 }
929 {{if .Type "Z" -}}
930 if zz != zp {
931 t.Errorf("expected return object %q instead of %q", zp, zz)
932 }
933 {{end -}}
934 if err != anErr {
935 t.Errorf("expected %v instead of error %v", anErr, err)
936 }
937 if i != 1 {
938 t.Errorf("expected count of 1 instead of %d", i)
939 }
940 if j != (k + 1) {
941 t.Errorf("expected chain count of %d instead of %d with error %v", (k + 1), j, anErr)
942 }
943 }
944 }
945 }
946
947 func TestIf(t *testing.T) {
948 var (
949 i, j int
950 p = prototype()
951 ctx = context.Background()
952 r1 = counter(&i).If(true).Eval
953 r2 = counter(&i).If(false).Eval
954 )
955 {{if .Type "Z" -}}
956 var zp = {{.Prototype "Z"}}
957 {{end -}}
958 // r1 should execute the counter rule
959 // r2 should NOT exexute the counter rule
960 for k, r := range []Rule{r1, r2} {
961 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} nil, chainCounter(&j, ChainIdentity))
962 if e != p {
963 t.Errorf("expected event %q instead of %q", p, e)
964 }
965 {{if .Type "Z" -}}
966 if zz != zp {
967 t.Errorf("expected return object %q instead of %q", zp, zz)
968 }
969 {{end -}}
970 if err != nil {
971 t.Error("unexpected error", err)
972 }
973 if i != 1 {
974 t.Errorf("expected count of 1 instead of %d", i)
975 }
976 if j != (k + 1) {
977 t.Errorf("expected chain count of %d instead of %d", (k + 1), j)
978 }
979 }
980 }
981
982 func TestUnless(t *testing.T) {
983 var (
984 i, j int
985 p = prototype()
986 ctx = context.Background()
987 r1 = counter(&i).Unless(false).Eval
988 r2 = counter(&i).Unless(true).Eval
989 )
990 {{if .Type "Z" -}}
991 var zp = {{.Prototype "Z"}}
992 {{end -}}
993 // r1 should execute the counter rule
994 // r2 should NOT exexute the counter rule
995 for k, r := range []Rule{r1, r2} {
996 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} nil, chainCounter(&j, ChainIdentity))
997 if e != p {
998 t.Errorf("expected event %q instead of %q", p, e)
999 }
1000 {{if .Type "Z" -}}
1001 if zz != zp {
1002 t.Errorf("expected return object %q instead of %q", zp, zz)
1003 }
1004 {{end -}}
1005 if err != nil {
1006 t.Error("unexpected error", err)
1007 }
1008 if i != 1 {
1009 t.Errorf("expected count of 1 instead of %d", i)
1010 }
1011 if j != (k + 1) {
1012 t.Errorf("expected chain count of %d instead of %d", (k + 1), j)
1013 }
1014 }
1015 }
1016
1017 func TestOnce(t *testing.T) {
1018 var (
1019 i, j int
1020 p = prototype()
1021 ctx = context.Background()
1022 r1 = counter(&i).Once().Eval
1023 r2 = Rule(nil).Once().Eval
1024 )
1025 {{if .Type "Z" -}}
1026 var zp = {{.Prototype "Z"}}
1027 {{end -}}
1028 for k, r := range []Rule{r1, r2} {
1029 for x := 0; x < 5; x++ {
1030 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} nil, chainCounter(&j, ChainIdentity))
1031 if e != p {
1032 t.Errorf("expected event %q instead of %q", p, e)
1033 }
1034 {{if .Type "Z" -}}
1035 if zz != zp {
1036 t.Errorf("expected return object %q instead of %q", zp, zz)
1037 }
1038 {{end -}}
1039 if err != nil {
1040 t.Error("unexpected error", err)
1041 }
1042 if i != 1 {
1043 t.Errorf("expected count of 1 instead of %d", i)
1044 }
1045 if y := (k * 5) + x + 1; j != y {
1046 t.Errorf("expected chain count of %d instead of %d", y, j)
1047 }
1048 }
1049 }
1050 }
1051
1052 func TestRateLimit(t *testing.T) {
1053 // non-blocking, then blocking
1054 o := func() <-chan struct{} {
1055 x := make(chan struct{}, 1)
1056 x <- struct{}{}
1057 return x
1058 }
1059 var (
1060 ch1 <-chan struct{} // always nil, blocking
1061 ch2 = make(chan struct{}) // non-nil, blocking
1062 // ch3 is o()
1063 ch4 = make(chan struct{}) // non-nil, closed
1064 p = prototype()
1065 ctx = context.Background()
1066 fin = func() context.Context {
1067 c, cancel := context.WithCancel(context.Background())
1068 cancel()
1069 return c
1070 }()
1071
1072 errOverflow = errors.New("overflow")
1073 otherwiseSkip = Rule(nil)
1074 otherwiseSkipWithError = Fail(errOverflow)
1075 otherwiseDiscard = Drop()
1076 otherwiseDiscardWithError = Fail(errOverflow).ThenDrop()
1077 )
1078 {{if .Type "Z" -}}
1079 var zp = {{.Prototype "Z"}}
1080 {{end -}}
1081 close(ch4)
1082 for ti, tc := range []struct {
1083 // each set of inputs is executed 4 times: twice for r1, twice for r2
1084 ctx context.Context
1085 ch <-chan struct{}
1086 over Overflow
1087 otherwise Rule
1088 wantsError int // bitmask: lower 4 bits, one for each case; first case = highest bit
1089 wantsRuleCount []int
1090 wantsChainCount []int
1091 }{
1092 {ctx, ch1, OverflowOtherwise, otherwiseSkip, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1093 {ctx, ch2, OverflowOtherwise, otherwiseSkip, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1094 {ctx, o(), OverflowOtherwise, otherwiseSkip, 0x0, []int{1, 1, 1, 1}, []int{1, 2, 3, 4}},
1095 {ctx, ch4, OverflowOtherwise, otherwiseSkip, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1096
1097 {fin, ch1, OverflowOtherwise, otherwiseSkip, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1098 {fin, ch2, OverflowOtherwise, otherwiseSkip, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1099 {fin, o(), OverflowOtherwise, otherwiseSkip, 0x0, []int{1, 1, 1, 1}, []int{1, 2, 3, 4}},
1100 {fin, ch4, OverflowOtherwise, otherwiseSkip, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1101
1102 {ctx, ch1, OverflowOtherwise, otherwiseSkipWithError, 0xC, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1103 {ctx, ch2, OverflowOtherwise, otherwiseSkipWithError, 0xC, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1104 {ctx, o(), OverflowOtherwise, otherwiseSkipWithError, 0x4, []int{1, 1, 1, 1}, []int{1, 2, 3, 4}},
1105 {ctx, ch4, OverflowOtherwise, otherwiseSkipWithError, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1106
1107 {fin, ch1, OverflowOtherwise, otherwiseSkipWithError, 0xC, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1108 {fin, ch2, OverflowOtherwise, otherwiseSkipWithError, 0xC, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1109 {fin, o(), OverflowOtherwise, otherwiseSkipWithError, 0x4, []int{1, 1, 1, 1}, []int{1, 2, 3, 4}},
1110 {fin, ch4, OverflowOtherwise, otherwiseSkipWithError, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1111
1112 {ctx, ch1, OverflowOtherwise, otherwiseDiscard, 0x0, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
1113 {ctx, ch2, OverflowOtherwise, otherwiseDiscard, 0x0, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
1114 {ctx, o(), OverflowOtherwise, otherwiseDiscard, 0x0, []int{1, 1, 1, 1}, []int{1, 1, 2, 3}},
1115 {ctx, ch4, OverflowOtherwise, otherwiseDiscard, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1116
1117 {fin, ch1, OverflowOtherwise, otherwiseDiscard, 0x0, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
1118 {fin, ch2, OverflowOtherwise, otherwiseDiscard, 0x0, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
1119 {fin, o(), OverflowOtherwise, otherwiseDiscard, 0x0, []int{1, 1, 1, 1}, []int{1, 1, 2, 3}},
1120 {fin, ch4, OverflowOtherwise, otherwiseDiscard, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1121
1122 {ctx, ch1, OverflowOtherwise, otherwiseDiscardWithError, 0xC, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
1123 {ctx, ch2, OverflowOtherwise, otherwiseDiscardWithError, 0xC, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
1124 {ctx, o(), OverflowOtherwise, otherwiseDiscardWithError, 0x4, []int{1, 1, 1, 1}, []int{1, 1, 2, 3}},
1125 {ctx, ch4, OverflowOtherwise, otherwiseDiscardWithError, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1126
1127 {fin, ch1, OverflowOtherwise, otherwiseDiscardWithError, 0xC, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
1128 {fin, ch2, OverflowOtherwise, otherwiseDiscardWithError, 0xC, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
1129 {fin, o(), OverflowOtherwise, otherwiseDiscardWithError, 0x4, []int{1, 1, 1, 1}, []int{1, 1, 2, 3}},
1130 {fin, ch4, OverflowOtherwise, otherwiseDiscardWithError, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1131
1132 {fin, ch1, OverflowWait, nil, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1133 {fin, ch2, OverflowWait, nil, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1134 {fin, o(), OverflowWait, nil, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1135 {fin, ch4, OverflowWait, nil, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
1136 {ctx, ch4, OverflowWait, nil, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
1137 } {
1138 var (
1139 i, j int
1140 r1 = counter(&i).RateLimit(tc.ch, tc.over, tc.otherwise).Eval
1141 r2 = Rule(nil).RateLimit(tc.ch, tc.over, tc.otherwise).Eval // a nil rule still invokes the chain
1142 )
1143 for k, r := range []Rule{r1, r2} {
1144 // execute each rule twice
1145 for x := 0; x < 2; x++ {
1146 _, e, {{.Ref "Z" "zz," -}} err := r(tc.ctx, p, {{.Ref "Z" "zp," -}} nil, chainCounter(&j, ChainIdentity))
1147 if e != p {
1148 t.Errorf("test case %d failed: expected event %q instead of %q", ti, p, e)
1149 }
1150 {{if .Type "Z" -}}
1151 if zz != zp {
1152 t.Errorf("expected return object %q instead of %q", zp, zz)
1153 }
1154 {{end -}}
1155 if b := 8 >> uint(k*2+x); ((b & tc.wantsError) != 0) != (err != nil) {
1156 t.Errorf("test case (%d,%d,%d) failed: unexpected error %v", ti, k, x, err)
1157 }
1158 if y := tc.wantsRuleCount[k*2+x]; i != y {
1159 t.Errorf("test case (%d,%d,%d) failed: expected count of %d instead of %d",
1160 ti, k, x, y, i)
1161 }
1162 if y := tc.wantsChainCount[k*2+x]; j != y {
1163 t.Errorf("test case (%d,%d,%d) failed: expected chain count of %d instead of %d",
1164 ti, k, x, y, j)
1165 }
1166 }
1167 }
1168 }
1169 // test blocking capability via rateLimit
1170 blocked := false
1171 r := limit(Rule(nil).Eval, func(_ context.Context, b bool) bool { blocked = b; return false }, OverflowWait, nil)
1172 _, e, {{.Ref "Z" "zz," -}} err := r(ctx, p, {{.Ref "Z" "zp," -}} nil, ChainIdentity)
1173 if e != p {
1174 t.Errorf("expected event %q instead of %q", p, e)
1175 }
1176 {{if .Type "Z" -}}
1177 if zz != zp {
1178 t.Errorf("expected return object %q instead of %q", zp, zz)
1179 }
1180 {{end -}}
1181 if err != nil {
1182 t.Errorf("unexpected error %v", err)
1183 }
1184 if !blocked {
1185 t.Error("expected OverflowWait to block rule execution")
1186 }
1187 // test RateLimit deadlock detector
1188 didPanic := false
1189 func() {
1190 defer func() { didPanic = recover() != nil }()
1191 Rule(Rule(nil).Eval).RateLimit(nil, OverflowWait, nil).Eval(ctx, p, {{.Ref "Z" "zp," -}} nil, ChainIdentity)
1192 }()
1193 if !didPanic {
1194 t.Error("expected panic because we configured a rule to deadlock")
1195 }
1196 }
1197 `))