Debian Codegraph @

api/v1/lib/extras/executor/callrules/callrules_generated_test.go

raw symbols SCIP
1 package callrules
2
3 // go generate -import github.com/mesos/mesos-go/api/v1/lib -import github.com/mesos/mesos-go/api/v1/lib/executor -type E:*executor.Call:&executor.Call{} -type Z:mesos.Response:&mesos.ResponseWrapper{}
4 // GENERATED CODE FOLLOWS; DO NOT EDIT.
5
6 import (
7 "context"
8 "errors"
9 "reflect"
10 "testing"
11
12 "github.com/mesos/mesos-go/api/v1/lib"
13 "github.com/mesos/mesos-go/api/v1/lib/executor"
14 )
15
16 func prototype() *executor.Call { return &executor.Call{} }
17
18 func counter(i *int) Rule {
19 return func(ctx context.Context, e *executor.Call, z mesos.Response, err error, ch Chain) (context.Context, *executor.Call, mesos.Response, error) {
20 *i++
21 return ch(ctx, e, z, err)
22 }
23 }
24
25 func tracer(r Rule, name string, t *testing.T) Rule {
26 return func(ctx context.Context, e *executor.Call, z mesos.Response, err error, ch Chain) (context.Context, *executor.Call, mesos.Response, error) {
27 t.Log("executing", name)
28 return r(ctx, e, z, err, ch)
29 }
30 }
31
32 func returnError(re error) Rule {
33 return func(ctx context.Context, e *executor.Call, z mesos.Response, err error, ch Chain) (context.Context, *executor.Call, mesos.Response, error) {
34 return ch(ctx, e, z, Error2(err, re))
35 }
36 }
37
38 func chainCounter(i *int, ch Chain) Chain {
39 return func(ctx context.Context, e *executor.Call, z mesos.Response, err error) (context.Context, *executor.Call, mesos.Response, error) {
40 *i++
41 return ch(ctx, e, z, err)
42 }
43 }
44
45 func chainPanic(x interface{}) Chain {
46 return func(_ context.Context, _ *executor.Call, _ mesos.Response, _ error) (context.Context, *executor.Call, mesos.Response, error) {
47 panic(x)
48 }
49 }
50
51 func TestChainIdentity(t *testing.T) {
52 var i int
53 counterRule := counter(&i)
54
55 var z0 mesos.Response
56
57 _, e, _, err := Rules{counterRule}.Eval(context.Background(), nil, z0, nil, ChainIdentity)
58 if e != nil {
59 t.Error("expected nil event instead of", e)
60 }
61 if err != nil {
62 t.Error("expected nil error instead of", err)
63 }
64 if i != 1 {
65 t.Error("expected 1 rule execution instead of", i)
66 }
67 }
68
69 func TestRules(t *testing.T) {
70 var (
71 p = prototype()
72 a = errors.New("a")
73 ctx = context.Background()
74 )
75
76 var z0 mesos.Response
77 var zp = &mesos.ResponseWrapper{}
78 // multiple rules in Rules should execute, dropping nil rules along the way
79 for _, tc := range []struct {
80 e *executor.Call
81 z mesos.Response
82 err error
83 }{
84 {nil, z0, nil},
85 {nil, z0, a},
86 {p, z0, nil},
87 {p, z0, a},
88
89 {nil, zp, nil},
90 {nil, zp, a},
91 {p, zp, nil},
92 {p, zp, a},
93 } {
94 var (
95 i int
96 rule = New(
97 nil,
98 tracer(counter(&i), "counter1", t),
99 nil,
100 tracer(counter(&i), "counter2", t),
101 nil,
102 )
103 _, e, zz, err = rule(ctx, tc.e, tc.z, tc.err, ChainIdentity)
104 )
105 if e != tc.e {
106 t.Errorf("expected prototype event %q instead of %q", tc.e, e)
107 }
108 if zz != tc.z {
109 t.Errorf("expected return object %q instead of %q", tc.z, zz)
110 }
111 if err != tc.err {
112 t.Errorf("expected %q error instead of %q", tc.err, err)
113 }
114 if i != 2 {
115 t.Error("expected 2 rule executions instead of", i)
116 }
117
118 // empty Rules should not change event, z, err
119 _, e, zz, err = Rules{}.Eval(ctx, tc.e, tc.z, tc.err, ChainIdentity)
120 if e != tc.e {
121 t.Errorf("expected prototype event %q instead of %q", tc.e, e)
122 }
123 if zz != tc.z {
124 t.Errorf("expected return object %q instead of %q", tc.z, zz)
125 }
126 if err != tc.err {
127 t.Errorf("expected %q error instead of %q", tc.err, err)
128 }
129 }
130 }
131
132 func TestError(t *testing.T) {
133 a := errors.New("a")
134 list := ErrorList{a}
135
136 msg := list.Error()
137 if msg != a.Error() {
138 t.Errorf("expected %q instead of %q", a.Error(), msg)
139 }
140
141 msg = ErrorList{}.Error()
142 if msg != msgNoErrors {
143 t.Errorf("expected %q instead of %q", msgNoErrors, msg)
144 }
145
146 msg = ErrorList(nil).Error()
147 if msg != msgNoErrors {
148 t.Errorf("expected %q instead of %q", msgNoErrors, msg)
149 }
150 }
151
152 func TestError2(t *testing.T) {
153 var (
154 a = errors.New("a")
155 b = errors.New("b")
156 )
157 for i, tc := range []struct {
158 a error
159 b error
160 wants error
161 wantsMessage string
162 }{
163 {nil, nil, nil, ""},
164 {nil, ErrorList{nil}, nil, ""},
165 {ErrorList{nil}, ErrorList{nil}, nil, ""},
166 {ErrorList{ErrorList{nil}}, ErrorList{nil}, nil, ""},
167 {a, nil, a, "a"},
168 {ErrorList{a}, nil, a, "a"},
169 {ErrorList{nil, a, ErrorList{}}, nil, a, "a"},
170 {nil, b, b, "b"},
171 {nil, ErrorList{b}, b, "b"},
172 {a, b, ErrorList{a, b}, "a (and 1 more errors)"},
173 {a, ErrorList{b}, ErrorList{a, b}, "a (and 1 more errors)"},
174 {a, ErrorList{nil, ErrorList{b, ErrorList{}, nil}}, ErrorList{a, b}, "a (and 1 more errors)"},
175 } {
176 var (
177 sameError bool
178 result = Error2(tc.a, tc.b)
179 )
180 // jump through hoops because we can't directly compare two errors with == if
181 // they're both ErrorList.
182 if IsErrorList(result) == IsErrorList(tc.wants) { // both are lists or neither
183 sameError = (!IsErrorList(result) && result == tc.wants) ||
184 (IsErrorList(result) && reflect.DeepEqual(result, tc.wants))
185 }
186 if !sameError {
187 t.Fatalf("test case %d failed, expected %v instead of %v", i, tc.wants, result)
188 }
189 if result != nil && tc.wantsMessage != result.Error() {
190 t.Fatalf("test case %d failed, expected message %q instead of %q",
191 i, tc.wantsMessage, result.Error())
192 }
193 }
194 }
195
196 func TestUnlessDone(t *testing.T) {
197 var (
198 p = prototype()
199 ctx = context.Background()
200 fin = func() context.Context {
201 c, cancel := context.WithCancel(context.Background())
202 cancel()
203 return c
204 }()
205 )
206 var zp = &mesos.ResponseWrapper{}
207 for ti, tc := range []struct {
208 ctx context.Context
209 wantsError []error
210 wantsRuleCount []int
211 wantsChainCount []int
212 }{
213 {ctx, []error{nil, nil}, []int{1, 2}, []int{1, 2}},
214 {fin, []error{nil, context.Canceled}, []int{1, 1}, []int{1, 1}},
215 } {
216 var (
217 i, j int
218 r1 = counter(&i)
219 r2 = r1.UnlessDone()
220 )
221 for k, r := range []Rule{r1, r2} {
222 _, e, zz, err := r(tc.ctx, p, zp, nil, chainCounter(&j, ChainIdentity))
223 if e != p {
224 t.Errorf("test case %d failed: expected event %q instead of %q", ti, p, e)
225 }
226 if zz != zp {
227 t.Errorf("test case %d failed: expected return object %q instead of %q", ti, zp, zz)
228 }
229 if err != tc.wantsError[k] {
230 t.Errorf("test case %d failed: unexpected error %v", ti, err)
231 }
232 if i != tc.wantsRuleCount[k] {
233 t.Errorf("test case %d failed: expected count of %d instead of %d", ti, tc.wantsRuleCount[k], i)
234 }
235 if j != tc.wantsChainCount[k] {
236 t.Errorf("test case %d failed: expected chain count of %d instead of %d", ti, tc.wantsRuleCount[k], j)
237 }
238 }
239 }
240 r := Rule(nil).UnlessDone()
241 if r != nil {
242 t.Error("expected nil result from UnlessDone")
243 }
244 }
245
246 func TestAndThen(t *testing.T) {
247 var (
248 i, j int
249 p = prototype()
250 ctx = context.Background()
251 r1 = counter(&i)
252 r2 = Rule(nil).AndThen(counter(&i))
253 a = errors.New("a")
254 )
255 var zp = &mesos.ResponseWrapper{}
256 for k, r := range []Rule{r1, r2} {
257 _, e, zz, err := r(ctx, p, zp, a, chainCounter(&j, ChainIdentity))
258 if e != p {
259 t.Errorf("expected event %q instead of %q", p, e)
260 }
261 if zz != zp {
262 t.Errorf("expected return object %q instead of %q", zp, zz)
263 }
264 if err != a {
265 t.Error("unexpected error", err)
266 }
267 if i != 1 {
268 t.Errorf("expected count of 1 instead of %d", i)
269 }
270 if j != (k + 1) {
271 t.Errorf("expected chain count of %d instead of %d", (k + 1), j)
272 }
273 }
274 }
275
276 func TestOnFailure(t *testing.T) {
277 var (
278 i, j int
279 p = prototype()
280 ctx = context.Background()
281 a = errors.New("a")
282 r1 = counter(&i)
283 r2 = Fail(a).OnFailure(counter(&i))
284 )
285 var zp = &mesos.ResponseWrapper{}
286 for k, tc := range []struct {
287 r Rule
288 initialError error
289 }{
290 {r1, a},
291 {r2, nil},
292 } {
293 _, e, zz, err := tc.r(ctx, p, zp, tc.initialError, chainCounter(&j, ChainIdentity))
294 if e != p {
295 t.Errorf("expected event %q instead of %q", p, e)
296 }
297 if zz != zp {
298 t.Errorf("expected return object %q instead of %q", zp, zz)
299 }
300 if err != a {
301 t.Error("unexpected error", err)
302 }
303 if i != (k + 1) {
304 t.Errorf("expected count of %d instead of %d", (k + 1), i)
305 }
306 if j != (k + 1) {
307 t.Errorf("expected chain count of %d instead of %d", (k + 1), j)
308 }
309 }
310 }
311
312 func TestDropOnError(t *testing.T) {
313 var (
314 i, j int
315 p = prototype()
316 ctx = context.Background()
317 r1 = counter(&i)
318 r2 = counter(&i).DropOnError()
319 a = errors.New("a")
320 )
321 var zp = &mesos.ResponseWrapper{}
322 // r1 should execute the counter rule
323 // r2 should NOT exexute the counter rule
324 for _, r := range []Rule{r1, r2} {
325 _, e, zz, err := r(ctx, p, zp, a, chainCounter(&j, ChainIdentity))
326 if e != p {
327 t.Errorf("expected event %q instead of %q", p, e)
328 }
329 if zz != zp {
330 t.Errorf("expected return object %q instead of %q", zp, zz)
331 }
332 if err != a {
333 t.Error("unexpected error", err)
334 }
335 if i != 1 {
336 t.Errorf("expected count of 1 instead of %d", i)
337 }
338 if j != 1 {
339 t.Errorf("expected chain count of 1 instead of %d", j)
340 }
341 }
342 _, e, zz, err := r2(ctx, p, zp, nil, chainCounter(&j, ChainIdentity))
343 if e != p {
344 t.Errorf("expected event %q instead of %q", p, e)
345 }
346 if zz != zp {
347 t.Errorf("expected return object %q instead of %q", zp, zz)
348 }
349 if err != nil {
350 t.Error("unexpected error", err)
351 }
352 if j != 2 {
353 t.Errorf("expected chain count of 2 instead of %d", j)
354 }
355 }
356
357 func TestDropOnSuccess(t *testing.T) {
358 var (
359 i, j int
360 p = prototype()
361 ctx = context.Background()
362 r1 = counter(&i)
363 r2 = counter(&i).DropOnSuccess()
364 )
365 var zp = &mesos.ResponseWrapper{}
366 // r1 should execute the counter rule
367 // r2 should NOT exexute the counter rule
368 for _, r := range []Rule{r1, r2} {
369 _, e, zz, err := r(ctx, p, zp, nil, chainCounter(&j, ChainIdentity))
370 if e != p {
371 t.Errorf("expected event %q instead of %q", p, e)
372 }
373 if zz != zp {
374 t.Errorf("expected return object %q instead of %q", zp, zz)
375 }
376 if err != nil {
377 t.Error("unexpected error", err)
378 }
379 if i != 1 {
380 t.Errorf("expected count of 1 instead of %d", i)
381 }
382 if j != 1 {
383 t.Errorf("expected chain count of 1 instead of %d", j)
384 }
385 }
386 a := errors.New("a")
387 _, e, zz, err := r2(ctx, p, zp, a, chainCounter(&j, ChainIdentity))
388 if e != p {
389 t.Errorf("expected event %q instead of %q", p, e)
390 }
391 if zz != zp {
392 t.Errorf("expected return object %q instead of %q", zp, zz)
393 }
394 if err != a {
395 t.Error("unexpected error", err)
396 }
397 if i != 2 {
398 t.Errorf("expected count of 2 instead of %d", i)
399 }
400 if j != 2 {
401 t.Errorf("expected chain count of 2 instead of %d", j)
402 }
403
404 r3 := Rules{DropOnSuccess(), r1}.Eval
405 _, e, zz, err = r3(ctx, p, zp, nil, chainCounter(&j, ChainIdentity))
406 if e != p {
407 t.Errorf("expected event %q instead of %q", p, e)
408 }
409 if zz != zp {
410 t.Errorf("expected return object %q instead of %q", zp, zz)
411 }
412 if err != nil {
413 t.Error("unexpected error", err)
414 }
415 if i != 2 {
416 t.Errorf("expected count of 2 instead of %d", i)
417 }
418 if j != 3 {
419 t.Errorf("expected chain count of 3 instead of %d", j)
420 }
421 }
422
423 func TestThenDrop(t *testing.T) {
424 for _, anErr := range []error{nil, errors.New("a")} {
425 var (
426 i, j int
427 p = prototype()
428 ctx = context.Background()
429 r1 = counter(&i)
430 r2 = counter(&i).ThenDrop()
431 )
432 var zp = &mesos.ResponseWrapper{}
433 // r1 and r2 should execute the counter rule
434 for k, r := range []Rule{r1, r2} {
435 _, e, zz, err := r(ctx, p, zp, anErr, chainCounter(&j, ChainIdentity))
436 if e != p {
437 t.Errorf("expected event %q instead of %q", p, e)
438 }
439 if zz != zp {
440 t.Errorf("expected return object %q instead of %q", zp, zz)
441 }
442 if err != anErr {
443 t.Errorf("expected %v instead of error %v", anErr, err)
444 }
445 if i != (k + 1) {
446 t.Errorf("expected count of %d instead of %d", (k + 1), i)
447 }
448 if j != 1 {
449 t.Errorf("expected chain count of 1 instead of %d", j)
450 }
451 }
452 }
453 }
454
455 func TestDrop(t *testing.T) {
456 for _, anErr := range []error{nil, errors.New("a")} {
457 var (
458 i, j int
459 p = prototype()
460 ctx = context.Background()
461 r1 = counter(&i)
462 r2 = Rules{Drop(), counter(&i)}.Eval
463 )
464 var zp = &mesos.ResponseWrapper{}
465 // r1 should execute the counter rule
466 // r2 should NOT exexute the counter rule
467 for k, r := range []Rule{r1, r2} {
468 _, e, zz, err := r(ctx, p, zp, anErr, chainCounter(&j, ChainIdentity))
469 if e != p {
470 t.Errorf("expected event %q instead of %q", p, e)
471 }
472 if zz != zp {
473 t.Errorf("expected return object %q instead of %q", zp, zz)
474 }
475 if err != anErr {
476 t.Errorf("expected %v instead of error %v", anErr, err)
477 }
478 if i != 1 {
479 t.Errorf("expected count of 1 instead of %d", i)
480 }
481 if j != (k + 1) {
482 t.Errorf("expected chain count of %d instead of %d with error %v", (k + 1), j, anErr)
483 }
484 }
485 }
486 }
487
488 func TestIf(t *testing.T) {
489 var (
490 i, j int
491 p = prototype()
492 ctx = context.Background()
493 r1 = counter(&i).If(true).Eval
494 r2 = counter(&i).If(false).Eval
495 )
496 var zp = &mesos.ResponseWrapper{}
497 // r1 should execute the counter rule
498 // r2 should NOT exexute the counter rule
499 for k, r := range []Rule{r1, r2} {
500 _, e, zz, err := r(ctx, p, zp, nil, chainCounter(&j, ChainIdentity))
501 if e != p {
502 t.Errorf("expected event %q instead of %q", p, e)
503 }
504 if zz != zp {
505 t.Errorf("expected return object %q instead of %q", zp, zz)
506 }
507 if err != nil {
508 t.Error("unexpected error", err)
509 }
510 if i != 1 {
511 t.Errorf("expected count of 1 instead of %d", i)
512 }
513 if j != (k + 1) {
514 t.Errorf("expected chain count of %d instead of %d", (k + 1), j)
515 }
516 }
517 }
518
519 func TestUnless(t *testing.T) {
520 var (
521 i, j int
522 p = prototype()
523 ctx = context.Background()
524 r1 = counter(&i).Unless(false).Eval
525 r2 = counter(&i).Unless(true).Eval
526 )
527 var zp = &mesos.ResponseWrapper{}
528 // r1 should execute the counter rule
529 // r2 should NOT exexute the counter rule
530 for k, r := range []Rule{r1, r2} {
531 _, e, zz, err := r(ctx, p, zp, nil, chainCounter(&j, ChainIdentity))
532 if e != p {
533 t.Errorf("expected event %q instead of %q", p, e)
534 }
535 if zz != zp {
536 t.Errorf("expected return object %q instead of %q", zp, zz)
537 }
538 if err != nil {
539 t.Error("unexpected error", err)
540 }
541 if i != 1 {
542 t.Errorf("expected count of 1 instead of %d", i)
543 }
544 if j != (k + 1) {
545 t.Errorf("expected chain count of %d instead of %d", (k + 1), j)
546 }
547 }
548 }
549
550 func TestOnce(t *testing.T) {
551 var (
552 i, j int
553 p = prototype()
554 ctx = context.Background()
555 r1 = counter(&i).Once().Eval
556 r2 = Rule(nil).Once().Eval
557 )
558 var zp = &mesos.ResponseWrapper{}
559 for k, r := range []Rule{r1, r2} {
560 for x := 0; x < 5; x++ {
561 _, e, zz, err := r(ctx, p, zp, nil, chainCounter(&j, ChainIdentity))
562 if e != p {
563 t.Errorf("expected event %q instead of %q", p, e)
564 }
565 if zz != zp {
566 t.Errorf("expected return object %q instead of %q", zp, zz)
567 }
568 if err != nil {
569 t.Error("unexpected error", err)
570 }
571 if i != 1 {
572 t.Errorf("expected count of 1 instead of %d", i)
573 }
574 if y := (k * 5) + x + 1; j != y {
575 t.Errorf("expected chain count of %d instead of %d", y, j)
576 }
577 }
578 }
579 }
580
581 func TestRateLimit(t *testing.T) {
582 // non-blocking, then blocking
583 o := func() <-chan struct{} {
584 x := make(chan struct{}, 1)
585 x <- struct{}{}
586 return x
587 }
588 var (
589 ch1 <-chan struct{} // always nil, blocking
590 ch2 = make(chan struct{}) // non-nil, blocking
591 // ch3 is o()
592 ch4 = make(chan struct{}) // non-nil, closed
593 p = prototype()
594 ctx = context.Background()
595 fin = func() context.Context {
596 c, cancel := context.WithCancel(context.Background())
597 cancel()
598 return c
599 }()
600
601 errOverflow = errors.New("overflow")
602 otherwiseSkip = Rule(nil)
603 otherwiseSkipWithError = Fail(errOverflow)
604 otherwiseDiscard = Drop()
605 otherwiseDiscardWithError = Fail(errOverflow).ThenDrop()
606 )
607 var zp = &mesos.ResponseWrapper{}
608 close(ch4)
609 for ti, tc := range []struct {
610 // each set of inputs is executed 4 times: twice for r1, twice for r2
611 ctx context.Context
612 ch <-chan struct{}
613 over Overflow
614 otherwise Rule
615 wantsError int // bitmask: lower 4 bits, one for each case; first case = highest bit
616 wantsRuleCount []int
617 wantsChainCount []int
618 }{
619 {ctx, ch1, OverflowOtherwise, otherwiseSkip, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
620 {ctx, ch2, OverflowOtherwise, otherwiseSkip, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
621 {ctx, o(), OverflowOtherwise, otherwiseSkip, 0x0, []int{1, 1, 1, 1}, []int{1, 2, 3, 4}},
622 {ctx, ch4, OverflowOtherwise, otherwiseSkip, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
623
624 {fin, ch1, OverflowOtherwise, otherwiseSkip, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
625 {fin, ch2, OverflowOtherwise, otherwiseSkip, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
626 {fin, o(), OverflowOtherwise, otherwiseSkip, 0x0, []int{1, 1, 1, 1}, []int{1, 2, 3, 4}},
627 {fin, ch4, OverflowOtherwise, otherwiseSkip, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
628
629 {ctx, ch1, OverflowOtherwise, otherwiseSkipWithError, 0xC, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
630 {ctx, ch2, OverflowOtherwise, otherwiseSkipWithError, 0xC, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
631 {ctx, o(), OverflowOtherwise, otherwiseSkipWithError, 0x4, []int{1, 1, 1, 1}, []int{1, 2, 3, 4}},
632 {ctx, ch4, OverflowOtherwise, otherwiseSkipWithError, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
633
634 {fin, ch1, OverflowOtherwise, otherwiseSkipWithError, 0xC, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
635 {fin, ch2, OverflowOtherwise, otherwiseSkipWithError, 0xC, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
636 {fin, o(), OverflowOtherwise, otherwiseSkipWithError, 0x4, []int{1, 1, 1, 1}, []int{1, 2, 3, 4}},
637 {fin, ch4, OverflowOtherwise, otherwiseSkipWithError, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
638
639 {ctx, ch1, OverflowOtherwise, otherwiseDiscard, 0x0, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
640 {ctx, ch2, OverflowOtherwise, otherwiseDiscard, 0x0, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
641 {ctx, o(), OverflowOtherwise, otherwiseDiscard, 0x0, []int{1, 1, 1, 1}, []int{1, 1, 2, 3}},
642 {ctx, ch4, OverflowOtherwise, otherwiseDiscard, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
643
644 {fin, ch1, OverflowOtherwise, otherwiseDiscard, 0x0, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
645 {fin, ch2, OverflowOtherwise, otherwiseDiscard, 0x0, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
646 {fin, o(), OverflowOtherwise, otherwiseDiscard, 0x0, []int{1, 1, 1, 1}, []int{1, 1, 2, 3}},
647 {fin, ch4, OverflowOtherwise, otherwiseDiscard, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
648
649 {ctx, ch1, OverflowOtherwise, otherwiseDiscardWithError, 0xC, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
650 {ctx, ch2, OverflowOtherwise, otherwiseDiscardWithError, 0xC, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
651 {ctx, o(), OverflowOtherwise, otherwiseDiscardWithError, 0x4, []int{1, 1, 1, 1}, []int{1, 1, 2, 3}},
652 {ctx, ch4, OverflowOtherwise, otherwiseDiscardWithError, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
653
654 {fin, ch1, OverflowOtherwise, otherwiseDiscardWithError, 0xC, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
655 {fin, ch2, OverflowOtherwise, otherwiseDiscardWithError, 0xC, []int{0, 0, 0, 0}, []int{0, 0, 1, 2}},
656 {fin, o(), OverflowOtherwise, otherwiseDiscardWithError, 0x4, []int{1, 1, 1, 1}, []int{1, 1, 2, 3}},
657 {fin, ch4, OverflowOtherwise, otherwiseDiscardWithError, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
658
659 {fin, ch1, OverflowWait, nil, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
660 {fin, ch2, OverflowWait, nil, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
661 {fin, o(), OverflowWait, nil, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
662 {fin, ch4, OverflowWait, nil, 0x0, []int{0, 0, 0, 0}, []int{1, 2, 3, 4}},
663 {ctx, ch4, OverflowWait, nil, 0x0, []int{1, 2, 2, 2}, []int{1, 2, 3, 4}},
664 } {
665 var (
666 i, j int
667 r1 = counter(&i).RateLimit(tc.ch, tc.over, tc.otherwise).Eval
668 r2 = Rule(nil).RateLimit(tc.ch, tc.over, tc.otherwise).Eval // a nil rule still invokes the chain
669 )
670 for k, r := range []Rule{r1, r2} {
671 // execute each rule twice
672 for x := 0; x < 2; x++ {
673 _, e, zz, err := r(tc.ctx, p, zp, nil, chainCounter(&j, ChainIdentity))
674 if e != p {
675 t.Errorf("test case %d failed: expected event %q instead of %q", ti, p, e)
676 }
677 if zz != zp {
678 t.Errorf("expected return object %q instead of %q", zp, zz)
679 }
680 if b := 8 >> uint(k*2+x); ((b & tc.wantsError) != 0) != (err != nil) {
681 t.Errorf("test case (%d,%d,%d) failed: unexpected error %v", ti, k, x, err)
682 }
683 if y := tc.wantsRuleCount[k*2+x]; i != y {
684 t.Errorf("test case (%d,%d,%d) failed: expected count of %d instead of %d",
685 ti, k, x, y, i)
686 }
687 if y := tc.wantsChainCount[k*2+x]; j != y {
688 t.Errorf("test case (%d,%d,%d) failed: expected chain count of %d instead of %d",
689 ti, k, x, y, j)
690 }
691 }
692 }
693 }
694 // test blocking capability via rateLimit
695 blocked := false
696 r := limit(Rule(nil).Eval, func(_ context.Context, b bool) bool { blocked = b; return false }, OverflowWait, nil)
697 _, e, zz, err := r(ctx, p, zp, nil, ChainIdentity)
698 if e != p {
699 t.Errorf("expected event %q instead of %q", p, e)
700 }
701 if zz != zp {
702 t.Errorf("expected return object %q instead of %q", zp, zz)
703 }
704 if err != nil {
705 t.Errorf("unexpected error %v", err)
706 }
707 if !blocked {
708 t.Error("expected OverflowWait to block rule execution")
709 }
710 // test RateLimit deadlock detector
711 didPanic := false
712 func() {
713 defer func() { didPanic = recover() != nil }()
714 Rule(Rule(nil).Eval).RateLimit(nil, OverflowWait, nil).Eval(ctx, p, zp, nil, ChainIdentity)
715 }()
716 if !didPanic {
717 t.Error("expected panic because we configured a rule to deadlock")
718 }
719 }