|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using EliteAPI.Utils; |
| 6 | +using FluentAssertions; |
| 7 | + |
| 8 | +namespace EliteAPI.Tests; |
| 9 | + |
| 10 | +public class FileUtilsTests |
| 11 | +{ |
| 12 | + private string _testDir = null!; |
| 13 | + |
| 14 | + [Before(Test)] |
| 15 | + public void Setup() |
| 16 | + { |
| 17 | + _testDir = Path.Combine(Path.GetTempPath(), $"EliteAPI.Tests.{Guid.NewGuid()}"); |
| 18 | + Directory.CreateDirectory(_testDir); |
| 19 | + } |
| 20 | + |
| 21 | + [After(Test)] |
| 22 | + public void Cleanup() |
| 23 | + { |
| 24 | + if (Directory.Exists(_testDir)) |
| 25 | + Directory.Delete(_testDir, true); |
| 26 | + } |
| 27 | + |
| 28 | + [Test] |
| 29 | + public void WriteWithRetry_WritesContentToFile() |
| 30 | + { |
| 31 | + var path = Path.Combine(_testDir, "test.txt"); |
| 32 | + var content = "Hello, World!"; |
| 33 | + |
| 34 | + FileUtils.WriteWithRetry(path, content); |
| 35 | + |
| 36 | + File.Exists(path).Should().BeTrue(); |
| 37 | + File.ReadAllText(path).Should().Be(content); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public void WriteWithRetry_OverwritesExistingFile() |
| 42 | + { |
| 43 | + var path = Path.Combine(_testDir, "test.txt"); |
| 44 | + File.WriteAllText(path, "Old content"); |
| 45 | + |
| 46 | + FileUtils.WriteWithRetry(path, "New content"); |
| 47 | + |
| 48 | + File.ReadAllText(path).Should().Be("New content"); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public void WriteWithRetry_AllowsConcurrentReads() |
| 53 | + { |
| 54 | + var path = Path.Combine(_testDir, "test.txt"); |
| 55 | + FileUtils.WriteWithRetry(path, "Initial content"); |
| 56 | + |
| 57 | + // Open file for reading with share |
| 58 | + using var readStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 59 | + using var reader = new StreamReader(readStream); |
| 60 | + |
| 61 | + // Should be able to write while file is open for reading |
| 62 | + var writeAction = () => FileUtils.WriteWithRetry(path, "Updated content"); |
| 63 | + writeAction.Should().NotThrow(); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public async Task WriteWithRetry_RetriesOnTemporaryLock() |
| 68 | + { |
| 69 | + var path = Path.Combine(_testDir, "test.txt"); |
| 70 | + FileUtils.WriteWithRetry(path, "Initial"); |
| 71 | + |
| 72 | + // Lock the file temporarily |
| 73 | + var lockReleased = false; |
| 74 | + var writeCompleted = false; |
| 75 | + |
| 76 | + var lockTask = Task.Run(() => |
| 77 | + { |
| 78 | + using var lockStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None); |
| 79 | + Thread.Sleep(80); // Hold lock for 80ms (retry happens at 50ms intervals) |
| 80 | + lockReleased = true; |
| 81 | + }); |
| 82 | + |
| 83 | + // Start write after a small delay to ensure lock is acquired |
| 84 | + await Task.Delay(10); |
| 85 | + |
| 86 | + var writeTask = Task.Run(() => |
| 87 | + { |
| 88 | + FileUtils.WriteWithRetry(path, "After retry", maxRetries: 3, retryDelayMs: 50); |
| 89 | + writeCompleted = true; |
| 90 | + }); |
| 91 | + |
| 92 | + await Task.WhenAll(lockTask, writeTask); |
| 93 | + |
| 94 | + lockReleased.Should().BeTrue(); |
| 95 | + writeCompleted.Should().BeTrue(); |
| 96 | + File.ReadAllText(path).Should().Be("After retry"); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void WriteWithRetry_ThrowsAfterMaxRetries() |
| 101 | + { |
| 102 | + var path = Path.Combine(_testDir, "test.txt"); |
| 103 | + FileUtils.WriteWithRetry(path, "Initial"); |
| 104 | + |
| 105 | + // Hold exclusive lock on the file |
| 106 | + using var lockStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None); |
| 107 | + |
| 108 | + // Should throw after retries are exhausted |
| 109 | + var writeAction = () => FileUtils.WriteWithRetry(path, "Should fail", maxRetries: 2, retryDelayMs: 10); |
| 110 | + writeAction.Should().Throw<IOException>(); |
| 111 | + } |
| 112 | + |
| 113 | + [Test] |
| 114 | + public async Task WriteWithRetry_HandlesMultipleConcurrentWriters() |
| 115 | + { |
| 116 | + var path = Path.Combine(_testDir, "test.txt"); |
| 117 | + var writeCount = 10; |
| 118 | + var tasks = new Task[writeCount]; |
| 119 | + |
| 120 | + for (var i = 0; i < writeCount; i++) |
| 121 | + { |
| 122 | + var content = $"Content {i}"; |
| 123 | + tasks[i] = Task.Run(() => FileUtils.WriteWithRetry(path, content)); |
| 124 | + } |
| 125 | + |
| 126 | + var allTasks = () => Task.WhenAll(tasks); |
| 127 | + await allTasks.Should().NotThrowAsync(); |
| 128 | + |
| 129 | + // File should exist and have content from one of the writes |
| 130 | + File.Exists(path).Should().BeTrue(); |
| 131 | + File.ReadAllText(path).Should().StartWith("Content "); |
| 132 | + } |
| 133 | + |
| 134 | + [Test] |
| 135 | + public void AppendWithRetry_AppendsContentToFile() |
| 136 | + { |
| 137 | + var path = Path.Combine(_testDir, "test.txt"); |
| 138 | + File.WriteAllText(path, "Line 1\n"); |
| 139 | + |
| 140 | + FileUtils.AppendWithRetry(path, "Line 2\n"); |
| 141 | + |
| 142 | + File.ReadAllText(path).Should().Be("Line 1\nLine 2\n"); |
| 143 | + } |
| 144 | + |
| 145 | + [Test] |
| 146 | + public void AppendWithRetry_CreatesFileIfNotExists() |
| 147 | + { |
| 148 | + var path = Path.Combine(_testDir, "new.txt"); |
| 149 | + |
| 150 | + FileUtils.AppendWithRetry(path, "First line"); |
| 151 | + |
| 152 | + File.Exists(path).Should().BeTrue(); |
| 153 | + File.ReadAllText(path).Should().Be("First line"); |
| 154 | + } |
| 155 | + |
| 156 | + [Test] |
| 157 | + public async Task AppendWithRetry_HandlesMultipleConcurrentAppends() |
| 158 | + { |
| 159 | + var path = Path.Combine(_testDir, "test.txt"); |
| 160 | + File.WriteAllText(path, ""); |
| 161 | + var appendCount = 10; |
| 162 | + var tasks = new Task[appendCount]; |
| 163 | + |
| 164 | + for (var i = 0; i < appendCount; i++) |
| 165 | + { |
| 166 | + var content = $"Line {i}\n"; |
| 167 | + tasks[i] = Task.Run(() => FileUtils.AppendWithRetry(path, content)); |
| 168 | + } |
| 169 | + |
| 170 | + var allTasks = () => Task.WhenAll(tasks); |
| 171 | + await allTasks.Should().NotThrowAsync(); |
| 172 | + |
| 173 | + // File should have all lines (order may vary due to concurrency) |
| 174 | + var lines = File.ReadAllLines(path); |
| 175 | + lines.Should().HaveCount(appendCount); |
| 176 | + } |
| 177 | +} |
0 commit comments