|
1 | 1 | /** @prettier */
|
2 | 2 | import { expect } from 'chai';
|
3 | 3 | import { retry, map, take, mergeMap, concat, multicast, refCount } from 'rxjs/operators';
|
4 |
| -import { Observable, Observer, defer, range, of, throwError, Subject } from 'rxjs'; |
| 4 | +import { Observable, Observer, defer, range, of, throwError, Subject, timer, EMPTY } from 'rxjs'; |
5 | 5 | import { TestScheduler } from 'rxjs/testing';
|
6 | 6 | import { observableMatcher } from '../helpers/observableMatcher';
|
7 | 7 |
|
@@ -411,4 +411,248 @@ describe('retry', () => {
|
411 | 411 | expectSubscriptions(source.subscriptions).toBe(subs);
|
412 | 412 | });
|
413 | 413 | });
|
| 414 | + |
| 415 | + describe('with delay config', () => { |
| 416 | + describe('of a number', () => { |
| 417 | + it('should delay the retry by a specified amount of time', () => { |
| 418 | + rxTest.run(({ cold, time, expectSubscriptions, expectObservable }) => { |
| 419 | + const source = cold('---a---b---#'); |
| 420 | + const t = time(' ----|'); |
| 421 | + const subs = [ |
| 422 | + // |
| 423 | + ' ^----------!', |
| 424 | + ' ---------------^----------!', |
| 425 | + ' ------------------------------^----------!', |
| 426 | + ' ---------------------------------------------^----!', |
| 427 | + ]; |
| 428 | + const unsub = ' ^-------------------------------------------------!'; |
| 429 | + const expected = ' ---a---b----------a---b----------a---b----------a--'; |
| 430 | + const result = source.pipe( |
| 431 | + retry({ |
| 432 | + delay: t, |
| 433 | + }) |
| 434 | + ); |
| 435 | + expectObservable(result, unsub).toBe(expected); |
| 436 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 437 | + }); |
| 438 | + }); |
| 439 | + |
| 440 | + it('should act like a normal retry if delay is set to 0', () => { |
| 441 | + rxTest.run(({ cold, expectSubscriptions, expectObservable }) => { |
| 442 | + const source = cold('---a---b---#'); |
| 443 | + const subs = [ |
| 444 | + // |
| 445 | + ' ^----------!', |
| 446 | + ' -----------^----------!', |
| 447 | + ' ----------------------^----------!', |
| 448 | + ' ---------------------------------^----!', |
| 449 | + ]; |
| 450 | + const unsub = ' ^-------------------------------------!'; |
| 451 | + const expected = ' ---a---b------a---b------a---b------a--'; |
| 452 | + const result = source.pipe( |
| 453 | + retry({ |
| 454 | + delay: 0, |
| 455 | + }) |
| 456 | + ); |
| 457 | + expectObservable(result, unsub).toBe(expected); |
| 458 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 459 | + }); |
| 460 | + }); |
| 461 | + |
| 462 | + it('should act like a normal retry if delay is less than 0', () => { |
| 463 | + rxTest.run(({ cold, expectSubscriptions, expectObservable }) => { |
| 464 | + const source = cold('---a---b---#'); |
| 465 | + const subs = [ |
| 466 | + // |
| 467 | + ' ^----------!', |
| 468 | + ' -----------^----------!', |
| 469 | + ' ----------------------^----------!', |
| 470 | + ' ---------------------------------^----!', |
| 471 | + ]; |
| 472 | + const unsub = ' ^-------------------------------------!'; |
| 473 | + const expected = ' ---a---b------a---b------a---b------a--'; |
| 474 | + const result = source.pipe( |
| 475 | + retry({ |
| 476 | + delay: -100, |
| 477 | + }) |
| 478 | + ); |
| 479 | + expectObservable(result, unsub).toBe(expected); |
| 480 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 481 | + }); |
| 482 | + }); |
| 483 | + |
| 484 | + it('should honor count as the max retries', () => { |
| 485 | + rxTest.run(({ cold, time, expectSubscriptions, expectObservable }) => { |
| 486 | + const source = cold('---a---b---#'); |
| 487 | + const t = time(' ----|'); |
| 488 | + const subs = [ |
| 489 | + // |
| 490 | + ' ^----------!', |
| 491 | + ' ---------------^----------!', |
| 492 | + ' ------------------------------^----------!', |
| 493 | + ]; |
| 494 | + const expected = ' ---a---b----------a---b----------a---b---#'; |
| 495 | + const result = source.pipe( |
| 496 | + retry({ |
| 497 | + count: 2, |
| 498 | + delay: t, |
| 499 | + }) |
| 500 | + ); |
| 501 | + expectObservable(result).toBe(expected); |
| 502 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 503 | + }); |
| 504 | + }); |
| 505 | + }); |
| 506 | + |
| 507 | + describe('of a function', () => { |
| 508 | + it('should delay the retry with a function that returns a notifier', () => { |
| 509 | + rxTest.run(({ cold, expectSubscriptions, expectObservable }) => { |
| 510 | + const source = cold('---a---b---#'); |
| 511 | + const subs = [ |
| 512 | + // |
| 513 | + ' ^----------!', |
| 514 | + ' ------------^----------!', |
| 515 | + ' -------------------------^----------!', |
| 516 | + ' ---------------------------------------^----!', |
| 517 | + ]; |
| 518 | + const unsub = ' ^-------------------------------------------!'; |
| 519 | + const expected = ' ---a---b-------a---b--------a---b---------a--'; |
| 520 | + const result = source.pipe( |
| 521 | + retry({ |
| 522 | + delay: (_err, retryCount) => { |
| 523 | + // retryCount will be 1, 2, 3, etc. |
| 524 | + return timer(retryCount); |
| 525 | + }, |
| 526 | + }) |
| 527 | + ); |
| 528 | + expectObservable(result, unsub).toBe(expected); |
| 529 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 530 | + }); |
| 531 | + }); |
| 532 | + |
| 533 | + it('should delay the retry with a function that returns a hot observable', () => { |
| 534 | + rxTest.run(({ cold, hot, expectSubscriptions, expectObservable }) => { |
| 535 | + const source = cold(' ---a---b---#'); |
| 536 | + const notifier = hot('--------------x----------------x----------------x------'); |
| 537 | + const subs = [ |
| 538 | + // |
| 539 | + ' ^----------!', |
| 540 | + ' --------------^----------!', |
| 541 | + ' -------------------------------^----------!', |
| 542 | + ]; |
| 543 | + const notifierSubs = [ |
| 544 | + // |
| 545 | + ' -----------^--!', |
| 546 | + ' -------------------------^-----!', |
| 547 | + ' ------------------------------------------^-!', |
| 548 | + ]; |
| 549 | + const unsub = ' ^-------------------------------------------!'; |
| 550 | + const expected = ' ---a---b---------a---b------------a---b------'; |
| 551 | + const result = source.pipe( |
| 552 | + retry({ |
| 553 | + delay: () => notifier, |
| 554 | + }) |
| 555 | + ); |
| 556 | + expectObservable(result, unsub).toBe(expected); |
| 557 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 558 | + expectSubscriptions(notifier.subscriptions).toBe(notifierSubs); |
| 559 | + }); |
| 560 | + }); |
| 561 | + |
| 562 | + it('should complete if the notifier completes', () => { |
| 563 | + rxTest.run(({ cold, expectSubscriptions, expectObservable }) => { |
| 564 | + const source = cold('---a---b---#'); |
| 565 | + const subs = [ |
| 566 | + // |
| 567 | + ' ^----------!', |
| 568 | + ' ------------^----------!', |
| 569 | + ' -------------------------^----------!', |
| 570 | + ' ------------------------------------!', |
| 571 | + ]; |
| 572 | + const expected = ' ---a---b-------a---b--------a---b---|'; |
| 573 | + const result = source.pipe( |
| 574 | + retry({ |
| 575 | + delay: (_err, retryCount) => { |
| 576 | + return retryCount <= 2 ? timer(retryCount) : EMPTY; |
| 577 | + }, |
| 578 | + }) |
| 579 | + ); |
| 580 | + expectObservable(result).toBe(expected); |
| 581 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 582 | + }); |
| 583 | + }); |
| 584 | + |
| 585 | + it('should error if the notifier errors', () => { |
| 586 | + rxTest.run(({ cold, expectSubscriptions, expectObservable }) => { |
| 587 | + const source = cold('---a---b---#'); |
| 588 | + const subs = [ |
| 589 | + // |
| 590 | + ' ^----------!', |
| 591 | + ' ------------^----------!', |
| 592 | + ' -------------------------^----------!', |
| 593 | + ' ------------------------------------!', |
| 594 | + ]; |
| 595 | + const expected = ' ---a---b-------a---b--------a---b---#'; |
| 596 | + const result = source.pipe( |
| 597 | + retry({ |
| 598 | + delay: (_err, retryCount) => { |
| 599 | + return retryCount <= 2 ? timer(retryCount) : throwError(() => new Error('blah')); |
| 600 | + }, |
| 601 | + }) |
| 602 | + ); |
| 603 | + expectObservable(result).toBe(expected, undefined, new Error('blah')); |
| 604 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 605 | + }); |
| 606 | + }); |
| 607 | + |
| 608 | + it('should error if the delay function throws', () => { |
| 609 | + rxTest.run(({ cold, expectSubscriptions, expectObservable }) => { |
| 610 | + const source = cold('---a---b---#'); |
| 611 | + const subs = [ |
| 612 | + // |
| 613 | + ' ^----------!', |
| 614 | + ' ------------^----------!', |
| 615 | + ' -------------------------^----------!', |
| 616 | + ' ------------------------------------!', |
| 617 | + ]; |
| 618 | + const expected = ' ---a---b-------a---b--------a---b---#'; |
| 619 | + const result = source.pipe( |
| 620 | + retry({ |
| 621 | + delay: (_err, retryCount) => { |
| 622 | + if (retryCount <= 2) { |
| 623 | + return timer(retryCount); |
| 624 | + } else { |
| 625 | + throw new Error('blah'); |
| 626 | + } |
| 627 | + }, |
| 628 | + }) |
| 629 | + ); |
| 630 | + expectObservable(result).toBe(expected, undefined, new Error('blah')); |
| 631 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 632 | + }); |
| 633 | + }); |
| 634 | + |
| 635 | + it('should be usable for exponential backoff', () => { |
| 636 | + rxTest.run(({ cold, expectObservable, expectSubscriptions }) => { |
| 637 | + const source = cold('---a---#'); |
| 638 | + const subs = [ |
| 639 | + // |
| 640 | + ' ^------!', |
| 641 | + ' ---------^------!', |
| 642 | + ' --------------------^------!', |
| 643 | + ' -----------------------------------^------!', |
| 644 | + ]; |
| 645 | + const expected = ' ---a--------a----------a--------------a---#'; |
| 646 | + const result = source.pipe( |
| 647 | + retry({ |
| 648 | + count: 3, |
| 649 | + delay: (_err, retryCount) => timer(2 ** retryCount), |
| 650 | + }) |
| 651 | + ); |
| 652 | + expectObservable(result).toBe(expected); |
| 653 | + expectSubscriptions(source.subscriptions).toBe(subs); |
| 654 | + }); |
| 655 | + }); |
| 656 | + }); |
| 657 | + }); |
414 | 658 | });
|
0 commit comments