|
7 | 7 | using FluentAssertions;
|
8 | 8 | using Halibut;
|
9 | 9 | using Halibut.Exceptions;
|
| 10 | +using Halibut.Transport; |
10 | 11 | using NUnit.Framework;
|
11 | 12 | using Octopus.Tentacle.Client.Retries;
|
| 13 | +using Octopus.Tentacle.CommonTestUtils.Assertions; |
12 | 14 | using Polly.Timeout;
|
13 | 15 |
|
14 | 16 | namespace Octopus.Tentacle.Client.Tests
|
@@ -687,5 +689,184 @@ private TimeSpan GetMinTimeoutDuration(RpcCallRetryHandler handler)
|
687 | 689 | {
|
688 | 690 | return handler.RetryTimeout - handler.RetryIfRemainingDurationAtLeast - retryBackoffBuffer;
|
689 | 691 | }
|
| 692 | + |
| 693 | + [Test] |
| 694 | + public async Task MinimumAttempts_WhenSetTo1_DoesNotRetryAfterTimeoutExceeded() |
| 695 | + { |
| 696 | + var expectedResult = Guid.NewGuid(); |
| 697 | + var callCount = 0; |
| 698 | + var onRetryActionCalled = false; |
| 699 | + var onTimeoutActionCalled = false; |
| 700 | + |
| 701 | + // Short timeout to ensure it's exceeded, but minimumAttemptsForInterruptedLongRunningCalls = 1 |
| 702 | + var handler = new RpcCallRetryHandler(TimeSpan.FromSeconds(1), minimumAttemptsForInterruptedLongRunningCalls: 1); |
| 703 | + |
| 704 | + var result = await handler.ExecuteWithRetries( |
| 705 | + async ct => |
| 706 | + { |
| 707 | + callCount++; |
| 708 | + |
| 709 | + // Sleep for 3 seconds to simulate a long-running operation |
| 710 | + // must exceed retry duration |
| 711 | + await Task.Delay(TimeSpan.FromSeconds(3), ct); |
| 712 | + |
| 713 | + // Succeed on the first (and only) attempt since minimumAttemptsForInterruptedLongRunningCalls = 1 |
| 714 | + if (callCount == 1) |
| 715 | + { |
| 716 | + return expectedResult; |
| 717 | + } |
| 718 | + |
| 719 | + throw new HalibutClientException($"An error has occurred on attempt {callCount}"); |
| 720 | + }, |
| 721 | + onRetryAction: async (_, _, _, _, _, _) => |
| 722 | + { |
| 723 | + await Task.CompletedTask; |
| 724 | + onRetryActionCalled = true; |
| 725 | + }, |
| 726 | + onTimeoutAction: async (_, _, _, _) => |
| 727 | + { |
| 728 | + await Task.CompletedTask; |
| 729 | + onTimeoutActionCalled = true; |
| 730 | + }, |
| 731 | + CancellationToken.None); |
| 732 | + |
| 733 | + // With minimumAttemptsForInterruptedLongRunningCalls = 1, we should only make one attempt (no retries) |
| 734 | + result.Should().Be(expectedResult); |
| 735 | + callCount.Should().Be(1); |
| 736 | + onRetryActionCalled.Should().BeFalse(); |
| 737 | + onTimeoutActionCalled.Should().BeFalse(); |
| 738 | + } |
| 739 | + |
| 740 | + [Test] |
| 741 | + public async Task MinimumAttempts_WhenSetTo2_MakesOneRetryEvenAfterTimeoutExceeded() |
| 742 | + { |
| 743 | + var expectedResult = Guid.NewGuid(); |
| 744 | + var callCount = 0; |
| 745 | + var onRetryActionCalled = false; |
| 746 | + var onTimeoutActionCalled = false; |
| 747 | + |
| 748 | + // Short timeout to ensure it's exceeded, but minimumAttemptsForInterruptedLongRunningCalls = 2 |
| 749 | + var handler = new RpcCallRetryHandler(TimeSpan.FromSeconds(5), minimumAttemptsForInterruptedLongRunningCalls: 2); |
| 750 | + |
| 751 | + var result = await handler.ExecuteWithRetries( |
| 752 | + async ct => |
| 753 | + { |
| 754 | + callCount++; |
| 755 | + |
| 756 | + // Delay 2 seconds to ensure the ct doesn't get canceled. |
| 757 | + await Task.Delay(TimeSpan.FromSeconds(2), ct); |
| 758 | + |
| 759 | + // Fail on first attempt, succeed on second |
| 760 | + if (callCount == 1) |
| 761 | + { |
| 762 | + throw new HalibutClientException($"An error has occurred on attempt {callCount}"); |
| 763 | + } |
| 764 | + |
| 765 | + return expectedResult; |
| 766 | + }, |
| 767 | + onRetryAction: async (_, _, _, _, _, _) => |
| 768 | + { |
| 769 | + await Task.CompletedTask; |
| 770 | + onRetryActionCalled = true; |
| 771 | + }, |
| 772 | + onTimeoutAction: async (_, _, _, _) => |
| 773 | + { |
| 774 | + await Task.CompletedTask; |
| 775 | + onTimeoutActionCalled = true; |
| 776 | + }, |
| 777 | + CancellationToken.None); |
| 778 | + |
| 779 | + // With minimumAttemptsForInterruptedLongRunningCalls = 2, we should make 2 attempts (1 initial + 1 retry) |
| 780 | + result.Should().Be(expectedResult); |
| 781 | + callCount.Should().Be(2); |
| 782 | + onRetryActionCalled.Should().BeTrue(); |
| 783 | + onTimeoutActionCalled.Should().BeFalse(); // Should succeed before timeout |
| 784 | + } |
| 785 | + |
| 786 | + [Test] |
| 787 | + public async Task MinimumAttempts_WhenSetTo3_MakesTwoRetriesEvenAfterTimeoutExceeded() |
| 788 | + { |
| 789 | + var expectedResult = Guid.NewGuid(); |
| 790 | + var callCount = 0; |
| 791 | + var retryCount = 0; |
| 792 | + var onTimeoutActionCalled = false; |
| 793 | + |
| 794 | + // Short timeout to ensure it's exceeded, but minimumAttemptsForInterruptedLongRunningCalls = 3 |
| 795 | + var handler = new RpcCallRetryHandler(TimeSpan.FromSeconds(8), minimumAttemptsForInterruptedLongRunningCalls: 3); |
| 796 | + |
| 797 | + var result = await handler.ExecuteWithRetries( |
| 798 | + async ct => |
| 799 | + { |
| 800 | + callCount++; |
| 801 | + |
| 802 | + // Always add 2 second delay as requested |
| 803 | + await Task.Delay(TimeSpan.FromSeconds(2), ct); |
| 804 | + |
| 805 | + // Fail on first two attempts, succeed on third |
| 806 | + if (callCount <= 2) |
| 807 | + { |
| 808 | + throw new HalibutClientException($"An error has occurred on attempt {callCount}"); |
| 809 | + } |
| 810 | + |
| 811 | + return expectedResult; |
| 812 | + }, |
| 813 | + onRetryAction: async (_, _, _, _, _, _) => |
| 814 | + { |
| 815 | + await Task.CompletedTask; |
| 816 | + retryCount++; |
| 817 | + }, |
| 818 | + onTimeoutAction: async (_, _, _, _) => |
| 819 | + { |
| 820 | + await Task.CompletedTask; |
| 821 | + onTimeoutActionCalled = true; |
| 822 | + }, |
| 823 | + CancellationToken.None); |
| 824 | + |
| 825 | + // With minimumAttemptsForInterruptedLongRunningCalls = 3, we should make 3 attempts (1 initial + 2 retries) |
| 826 | + result.Should().Be(expectedResult); |
| 827 | + callCount.Should().Be(3); |
| 828 | + retryCount.Should().Be(2); |
| 829 | + onTimeoutActionCalled.Should().BeFalse(); // Should succeed before timeout |
| 830 | + } |
| 831 | + |
| 832 | + /// <summary> |
| 833 | + /// Connection error means the tentacle did not get the request, which means the tentacle is considered to be offline. |
| 834 | + /// This shows that we won't exceed the retry duration attempting to connect to an offline tentacle to meet the |
| 835 | + /// minimumAttemptsForInterruptedLongRunningCalls count. |
| 836 | + /// </summary> |
| 837 | + [Test] |
| 838 | + public async Task WhenConfiguredToMakeAMinimumNumberOfAttempts_AndTheFirstAttemptExceedsTheRetryDuration_AndTheFailureIsAConnectingFailure_ARetryIsNotMade() |
| 839 | + { |
| 840 | + var callCount = 0; |
| 841 | + |
| 842 | + // Short timeout to ensure it's exceeded, but minimumAttemptsForInterruptedLongRunningCalls = 2 |
| 843 | + var handler = new RpcCallRetryHandler(TimeSpan.FromSeconds(2), minimumAttemptsForInterruptedLongRunningCalls: 9999); |
| 844 | + |
| 845 | + var exception = await AssertThrowsAny.Exception(async () => |
| 846 | + await handler.ExecuteWithRetries( |
| 847 | + async ct => |
| 848 | + { |
| 849 | + callCount++; |
| 850 | + |
| 851 | + // Must exceed retry duration |
| 852 | + await Task.Delay(TimeSpan.FromSeconds(5), ct); |
| 853 | + |
| 854 | + if(callCount == -1) return Guid.NewGuid(); // Never called used to make the typing work. |
| 855 | + |
| 856 | + throw new HalibutClientException("", new Exception(), ConnectionState.Connecting); |
| 857 | + }, |
| 858 | + onRetryAction: async (_, _, _, _, _, _) => |
| 859 | + { |
| 860 | + await Task.CompletedTask; |
| 861 | + }, |
| 862 | + onTimeoutAction: async (_, _, _, _) => |
| 863 | + { |
| 864 | + await Task.CompletedTask; |
| 865 | + }, |
| 866 | + CancellationToken.None)); |
| 867 | + exception.Should().BeAssignableTo<HalibutClientException>(); |
| 868 | + |
| 869 | + callCount.Should().Be(1); |
| 870 | + } |
690 | 871 | }
|
691 | 872 | }
|
0 commit comments