|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Azure.Mcp.Tools.AzureBackup.Services; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Azure.Mcp.Tools.AzureBackup.Tests.Services; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Unit tests for the internal <see cref="RsvBackupOperations.EnumerateTolerantAsync{TSource, TResult}"/> |
| 11 | +/// helper that backs defensive recovery-point enumeration. The RecoveryServicesBackup SDK can throw |
| 12 | +/// <see cref="UriFormatException"/>/<see cref="FormatException"/> while deserializing a single |
| 13 | +/// Azure Files (<c>FileShareRecoveryPoint</c>) item; the helper must skip the offending item and |
| 14 | +/// return the remaining valid items rather than failing the entire list. |
| 15 | +/// </summary> |
| 16 | +public class RsvBackupOperationsEnumerateTolerantTests |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// An <see cref="IAsyncEnumerable{T}"/> whose per-item factories can throw to simulate the SDK |
| 20 | + /// failing to deserialize an individual element during <see cref="IAsyncEnumerator{T}.MoveNextAsync"/>. |
| 21 | + /// The enumerator advances its index before invoking the factory, so a throwing item still lets |
| 22 | + /// the next <c>MoveNextAsync</c> yield the following element - matching the SDK's item-level |
| 23 | + /// deserialization failure behavior. |
| 24 | + /// </summary> |
| 25 | + private sealed class FaultyAsyncEnumerable(IReadOnlyList<Func<int>> factories) : IAsyncEnumerable<int> |
| 26 | + { |
| 27 | + public bool Disposed { get; private set; } |
| 28 | + |
| 29 | + public IAsyncEnumerator<int> GetAsyncEnumerator(CancellationToken cancellationToken = default) |
| 30 | + => new Enumerator(this, factories); |
| 31 | + |
| 32 | + private sealed class Enumerator(FaultyAsyncEnumerable owner, IReadOnlyList<Func<int>> factories) : IAsyncEnumerator<int> |
| 33 | + { |
| 34 | + private int _index = -1; |
| 35 | + |
| 36 | + public int Current { get; private set; } |
| 37 | + |
| 38 | + public ValueTask<bool> MoveNextAsync() |
| 39 | + { |
| 40 | + _index++; |
| 41 | + if (_index >= factories.Count) |
| 42 | + { |
| 43 | + return ValueTask.FromResult(false); |
| 44 | + } |
| 45 | + |
| 46 | + Current = factories[_index](); |
| 47 | + return ValueTask.FromResult(true); |
| 48 | + } |
| 49 | + |
| 50 | + public ValueTask DisposeAsync() |
| 51 | + { |
| 52 | + owner.Disposed = true; |
| 53 | + return ValueTask.CompletedTask; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static Func<int> Good(int value) => () => value; |
| 59 | + private static Func<int> ThrowsFormat() => () => throw new FormatException("bad duration"); |
| 60 | + private static Func<int> ThrowsUri() => () => throw new UriFormatException("bad uri"); |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task EnumerateTolerantAsync_AllValid_ReturnsAllItems() |
| 64 | + { |
| 65 | + var source = new FaultyAsyncEnumerable([Good(1), Good(2), Good(3)]); |
| 66 | + |
| 67 | + var result = await RsvBackupOperations.EnumerateTolerantAsync(source, static x => x, CancellationToken.None); |
| 68 | + |
| 69 | + Assert.Equal([1, 2, 3], result); |
| 70 | + Assert.True(source.Disposed); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public async Task EnumerateTolerantAsync_SkipsFormatExceptionItem_ReturnsValidItems() |
| 75 | + { |
| 76 | + var source = new FaultyAsyncEnumerable([Good(1), ThrowsFormat(), Good(3)]); |
| 77 | + |
| 78 | + var result = await RsvBackupOperations.EnumerateTolerantAsync(source, static x => x, CancellationToken.None); |
| 79 | + |
| 80 | + Assert.Equal([1, 3], result); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public async Task EnumerateTolerantAsync_SkipsUriFormatExceptionItem_ReturnsValidItems() |
| 85 | + { |
| 86 | + // UriFormatException derives from FormatException - the NEW-5 Azure Files case. |
| 87 | + var source = new FaultyAsyncEnumerable([Good(10), ThrowsUri(), Good(20)]); |
| 88 | + |
| 89 | + var result = await RsvBackupOperations.EnumerateTolerantAsync(source, static x => x, CancellationToken.None); |
| 90 | + |
| 91 | + Assert.Equal([10, 20], result); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task EnumerateTolerantAsync_SelectorThrowsFormatException_SkipsThatItem() |
| 96 | + { |
| 97 | + var source = new FaultyAsyncEnumerable([Good(1), Good(2), Good(3)]); |
| 98 | + |
| 99 | + var result = await RsvBackupOperations.EnumerateTolerantAsync( |
| 100 | + source, |
| 101 | + static x => x == 2 ? throw new FormatException("bad projection") : x, |
| 102 | + CancellationToken.None); |
| 103 | + |
| 104 | + Assert.Equal([1, 3], result); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public async Task EnumerateTolerantAsync_StopsAfterMaxConsecutiveFailures() |
| 109 | + { |
| 110 | + // first valid item, then three consecutive failures hit the cap and enumeration stops |
| 111 | + // before the trailing valid item is ever read. |
| 112 | + var source = new FaultyAsyncEnumerable( |
| 113 | + [Good(1), ThrowsFormat(), ThrowsFormat(), ThrowsFormat(), Good(99)]); |
| 114 | + |
| 115 | + var result = await RsvBackupOperations.EnumerateTolerantAsync( |
| 116 | + source, static x => x, CancellationToken.None, maxConsecutiveFailures: 3); |
| 117 | + |
| 118 | + Assert.Equal([1], result); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public async Task EnumerateTolerantAsync_ConsecutiveFailureCountResetsAfterSuccess() |
| 123 | + { |
| 124 | + // failures separated by successes never reach the cap, so every valid item is returned. |
| 125 | + var source = new FaultyAsyncEnumerable( |
| 126 | + [ThrowsFormat(), Good(1), ThrowsFormat(), Good(2), ThrowsFormat(), Good(3)]); |
| 127 | + |
| 128 | + var result = await RsvBackupOperations.EnumerateTolerantAsync( |
| 129 | + source, static x => x, CancellationToken.None, maxConsecutiveFailures: 3); |
| 130 | + |
| 131 | + Assert.Equal([1, 2, 3], result); |
| 132 | + } |
| 133 | + |
| 134 | + [Fact] |
| 135 | + public async Task EnumerateTolerantAsync_EmptySource_ReturnsEmpty() |
| 136 | + { |
| 137 | + var source = new FaultyAsyncEnumerable([]); |
| 138 | + |
| 139 | + var result = await RsvBackupOperations.EnumerateTolerantAsync(source, static x => x, CancellationToken.None); |
| 140 | + |
| 141 | + Assert.Empty(result); |
| 142 | + Assert.True(source.Disposed); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public async Task EnumerateTolerantAsync_NullSource_Throws() |
| 147 | + { |
| 148 | + await Assert.ThrowsAsync<ArgumentNullException>(() => |
| 149 | + RsvBackupOperations.EnumerateTolerantAsync<int, int>(null!, static x => x, CancellationToken.None)); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public async Task EnumerateTolerantAsync_NullSelector_Throws() |
| 154 | + { |
| 155 | + var source = new FaultyAsyncEnumerable([Good(1)]); |
| 156 | + |
| 157 | + await Assert.ThrowsAsync<ArgumentNullException>(() => |
| 158 | + RsvBackupOperations.EnumerateTolerantAsync<int, int>(source, null!, CancellationToken.None)); |
| 159 | + } |
| 160 | +} |
0 commit comments