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