|
9 | 9 | "fmt" |
10 | 10 | "net" |
11 | 11 | "net/http" |
| 12 | + "strings" |
12 | 13 | "testing" |
13 | 14 | "time" |
14 | 15 |
|
@@ -664,17 +665,223 @@ func TestClientWrapper_GetBackoffDuration(t *testing.T) { |
664 | 665 |
|
665 | 666 | // First failure should return minimum backoff duration |
666 | 667 | client.failedAttempts = 1 |
667 | | - duration := client.getBackoffDuration() |
| 668 | + duration := calculateBackoffDuration(client.failedAttempts) |
668 | 669 | assert.True(t, duration >= minBackoffDuration) |
669 | 670 | assert.True(t, duration <= minBackoffDuration*2) // Account for jitter |
670 | 671 |
|
671 | 672 | // Multiple failures should increase duration exponentially |
672 | 673 | client.failedAttempts = 3 |
673 | | - duration3 := client.getBackoffDuration() |
| 674 | + duration3 := calculateBackoffDuration(client.failedAttempts) |
674 | 675 | assert.True(t, duration3 > duration) |
675 | 676 |
|
676 | 677 | // Very high failure count should be capped at max duration |
677 | 678 | client.failedAttempts = 100 |
678 | | - durationMax := client.getBackoffDuration() |
| 679 | + durationMax := calculateBackoffDuration(client.failedAttempts) |
679 | 680 | assert.True(t, durationMax <= maxBackoffDuration*2) // Account for jitter |
680 | 681 | } |
| 682 | + |
| 683 | +// Test startupWithRetry with successful operation on first attempt |
| 684 | +func TestStartupWithRetry_Success_FirstAttempt(t *testing.T) { |
| 685 | + mockClientManager := new(mockClientManager) |
| 686 | + |
| 687 | + azappcfg := &AzureAppConfiguration{ |
| 688 | + clientManager: mockClientManager, |
| 689 | + keyValues: make(map[string]any), |
| 690 | + featureFlags: make(map[string]any), |
| 691 | + kvSelectors: []Selector{{KeyFilter: "*", LabelFilter: "\x00"}}, |
| 692 | + } |
| 693 | + |
| 694 | + // Create a successful operation that simply returns nil |
| 695 | + operation := func(ctx context.Context) error { |
| 696 | + return nil // Success on first attempt |
| 697 | + } |
| 698 | + |
| 699 | + ctx := context.Background() |
| 700 | + err := azappcfg.startupWithRetry(ctx, 10*time.Second, operation) |
| 701 | + |
| 702 | + assert.NoError(t, err) |
| 703 | +} |
| 704 | + |
| 705 | +// Test startupWithRetry with retry after retriable error |
| 706 | +func TestStartupWithRetry_Success_AfterRetriableError(t *testing.T) { |
| 707 | + mockClientManager := new(mockClientManager) |
| 708 | + |
| 709 | + azappcfg := &AzureAppConfiguration{ |
| 710 | + clientManager: mockClientManager, |
| 711 | + keyValues: make(map[string]any), |
| 712 | + featureFlags: make(map[string]any), |
| 713 | + kvSelectors: []Selector{{KeyFilter: "*", LabelFilter: "\x00"}}, |
| 714 | + } |
| 715 | + |
| 716 | + callCount := 0 |
| 717 | + retriableError := &azcore.ResponseError{StatusCode: http.StatusServiceUnavailable} |
| 718 | + |
| 719 | + // Create an operation that fails first, then succeeds |
| 720 | + operation := func(ctx context.Context) error { |
| 721 | + callCount++ |
| 722 | + if callCount == 1 { |
| 723 | + return retriableError // Fail on first attempt |
| 724 | + } |
| 725 | + return nil // Success on second attempt |
| 726 | + } |
| 727 | + |
| 728 | + ctx := context.Background() |
| 729 | + err := azappcfg.startupWithRetry(ctx, 10*time.Second, operation) |
| 730 | + |
| 731 | + assert.NoError(t, err) |
| 732 | + assert.Equal(t, 2, callCount, "Operation should be called twice") |
| 733 | +} |
| 734 | + |
| 735 | +// Test startupWithRetry with non-retriable error |
| 736 | +func TestStartupWithRetry_NonRetriableError(t *testing.T) { |
| 737 | + mockClientManager := new(mockClientManager) |
| 738 | + |
| 739 | + azappcfg := &AzureAppConfiguration{ |
| 740 | + clientManager: mockClientManager, |
| 741 | + keyValues: make(map[string]any), |
| 742 | + featureFlags: make(map[string]any), |
| 743 | + kvSelectors: []Selector{{KeyFilter: "*", LabelFilter: "\x00"}}, |
| 744 | + } |
| 745 | + |
| 746 | + callCount := 0 |
| 747 | + nonRetriableError := &azcore.ResponseError{StatusCode: http.StatusBadRequest} |
| 748 | + |
| 749 | + // Create an operation that fails with non-retriable error |
| 750 | + operation := func(ctx context.Context) error { |
| 751 | + callCount++ |
| 752 | + return nonRetriableError // Non-retriable error |
| 753 | + } |
| 754 | + |
| 755 | + ctx := context.Background() |
| 756 | + err := azappcfg.startupWithRetry(ctx, 10*time.Second, operation) |
| 757 | + |
| 758 | + assert.Error(t, err) |
| 759 | + assert.Contains(t, err.Error(), "load from Azure App Configuration failed with non-retriable error") |
| 760 | + assert.Equal(t, 1, callCount, "Operation should be called only once for non-retriable error") |
| 761 | +} |
| 762 | + |
| 763 | +// Test startupWithRetry with timeout |
| 764 | +func TestStartupWithRetry_Timeout(t *testing.T) { |
| 765 | + mockClientManager := new(mockClientManager) |
| 766 | + |
| 767 | + azappcfg := &AzureAppConfiguration{ |
| 768 | + clientManager: mockClientManager, |
| 769 | + keyValues: make(map[string]any), |
| 770 | + featureFlags: make(map[string]any), |
| 771 | + kvSelectors: []Selector{{KeyFilter: "*", LabelFilter: "\x00"}}, |
| 772 | + } |
| 773 | + |
| 774 | + callCount := 0 |
| 775 | + retriableError := &azcore.ResponseError{StatusCode: http.StatusServiceUnavailable} |
| 776 | + |
| 777 | + // Create an operation that always fails with retriable error |
| 778 | + operation := func(ctx context.Context) error { |
| 779 | + callCount++ |
| 780 | + return retriableError // Always fail |
| 781 | + } |
| 782 | + |
| 783 | + ctx := context.Background() |
| 784 | + // Use a very short timeout to trigger timeout quickly |
| 785 | + err := azappcfg.startupWithRetry(ctx, 100*time.Millisecond, operation) |
| 786 | + |
| 787 | + assert.Error(t, err) |
| 788 | + assert.True(t, |
| 789 | + err.Error() == "startup timeout reached after 100ms" || |
| 790 | + err.Error() == fmt.Sprintf("load from Azure App Configuration failed after %d attempts within timeout 100ms: %v", callCount, retriableError), |
| 791 | + "Error should indicate timeout or max attempts reached: %v", err) |
| 792 | + assert.True(t, callCount >= 1, "Operation should be called at least once") |
| 793 | +} |
| 794 | + |
| 795 | +// Test startupWithRetry with context cancellation during backoff |
| 796 | +func TestStartupWithRetry_ContextCancelledDuringBackoff(t *testing.T) { |
| 797 | + mockClientManager := new(mockClientManager) |
| 798 | + |
| 799 | + azappcfg := &AzureAppConfiguration{ |
| 800 | + clientManager: mockClientManager, |
| 801 | + keyValues: make(map[string]any), |
| 802 | + featureFlags: make(map[string]any), |
| 803 | + kvSelectors: []Selector{{KeyFilter: "*", LabelFilter: "\x00"}}, |
| 804 | + } |
| 805 | + |
| 806 | + callCount := 0 |
| 807 | + retriableError := &azcore.ResponseError{StatusCode: http.StatusServiceUnavailable} |
| 808 | + |
| 809 | + // Create an operation that fails with retriable error |
| 810 | + operation := func(ctx context.Context) error { |
| 811 | + callCount++ |
| 812 | + return retriableError |
| 813 | + } |
| 814 | + |
| 815 | + ctx, cancel := context.WithCancel(context.Background()) |
| 816 | + |
| 817 | + // Cancel the context after a short delay to simulate cancellation during backoff |
| 818 | + go func() { |
| 819 | + time.Sleep(50 * time.Millisecond) |
| 820 | + cancel() |
| 821 | + }() |
| 822 | + |
| 823 | + err := azappcfg.startupWithRetry(ctx, 10*time.Second, operation) |
| 824 | + |
| 825 | + assert.Error(t, err) |
| 826 | + assert.Contains(t, err.Error(), "load from Azure App Configuration timed out: context canceled") |
| 827 | +} |
| 828 | + |
| 829 | +// Test startupWithRetry with default timeout when zero timeout provided |
| 830 | +func TestStartupWithRetry_DefaultTimeout(t *testing.T) { |
| 831 | + mockClientManager := new(mockClientManager) |
| 832 | + |
| 833 | + azappcfg := &AzureAppConfiguration{ |
| 834 | + clientManager: mockClientManager, |
| 835 | + keyValues: make(map[string]any), |
| 836 | + featureFlags: make(map[string]any), |
| 837 | + kvSelectors: []Selector{{KeyFilter: "*", LabelFilter: "\x00"}}, |
| 838 | + } |
| 839 | + |
| 840 | + callCount := 0 |
| 841 | + // Create an operation that succeeds |
| 842 | + operation := func(ctx context.Context) error { |
| 843 | + callCount++ |
| 844 | + return nil |
| 845 | + } |
| 846 | + |
| 847 | + ctx := context.Background() |
| 848 | + // Pass zero timeout to test default timeout usage |
| 849 | + err := azappcfg.startupWithRetry(ctx, 0, operation) |
| 850 | + |
| 851 | + assert.NoError(t, err) |
| 852 | + assert.Equal(t, 1, callCount, "Operation should be called once") |
| 853 | +} |
| 854 | + |
| 855 | +// Test startupWithRetry with insufficient time remaining for retry |
| 856 | +func TestStartupWithRetry_InsufficientTimeForRetry(t *testing.T) { |
| 857 | + mockClientManager := new(mockClientManager) |
| 858 | + |
| 859 | + azappcfg := &AzureAppConfiguration{ |
| 860 | + clientManager: mockClientManager, |
| 861 | + keyValues: make(map[string]any), |
| 862 | + featureFlags: make(map[string]any), |
| 863 | + kvSelectors: []Selector{{KeyFilter: "*", LabelFilter: "\x00"}}, |
| 864 | + } |
| 865 | + |
| 866 | + callCount := 0 |
| 867 | + retriableError := &azcore.ResponseError{StatusCode: http.StatusServiceUnavailable} |
| 868 | + |
| 869 | + // Create an operation that always fails |
| 870 | + operation := func(ctx context.Context) error { |
| 871 | + callCount++ |
| 872 | + // Add some delay to consume time |
| 873 | + time.Sleep(50 * time.Millisecond) |
| 874 | + return retriableError |
| 875 | + } |
| 876 | + |
| 877 | + ctx := context.Background() |
| 878 | + // Use a short timeout that will be consumed by the first failure and not allow retry |
| 879 | + err := azappcfg.startupWithRetry(ctx, 80*time.Millisecond, operation) |
| 880 | + |
| 881 | + assert.Error(t, err) |
| 882 | + assert.True(t, |
| 883 | + err.Error() == "startup timeout reached after 80ms" || |
| 884 | + strings.Contains(err.Error(), "load from Azure App Configuration failed after") && strings.Contains(err.Error(), "attempts within timeout"), |
| 885 | + "Error should indicate timeout or insufficient time: %v", err) |
| 886 | + assert.True(t, callCount >= 1, "Operation should be called at least once") |
| 887 | +} |
0 commit comments