|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Sockets; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Avalonia.Headless.XUnit; |
| 10 | +using Avalonia.Threading; |
| 11 | +using Downloader.Desktop.Models; |
| 12 | +using Downloader.Desktop.Services; |
| 13 | +using Downloader.Desktop.ViewModels; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace Downloader.Desktop.Tests.Integration; |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// The reported leak (task #11): a completed download's <c>DownloadService</c> (with its package + buffers) |
| 20 | +/// was never released, so thousands of finished rows accumulated GBs that only a restart cleared. These |
| 21 | +/// tests download many small files through the real manager/engine over a loopback server and assert the |
| 22 | +/// engine handle is released once a row reaches a terminal state — and that a released row can still be |
| 23 | +/// retried (a fresh engine is built). |
| 24 | +/// </summary> |
| 25 | +public class MemoryReleaseTests |
| 26 | +{ |
| 27 | + [AvaloniaFact(Timeout = TestTimeouts.SlowMs)] |
| 28 | + public async Task Completed_downloads_release_their_engine() |
| 29 | + { |
| 30 | + var payload = new byte[16 * 1024]; |
| 31 | + new Random(7).NextBytes(payload); |
| 32 | + using var server = new Loopback(payload); |
| 33 | + |
| 34 | + var dir = Path.Combine(Path.GetTempPath(), "dldesktop_mem_" + Guid.NewGuid().ToString("N")); |
| 35 | + Directory.CreateDirectory(dir); |
| 36 | + try |
| 37 | + { |
| 38 | + var cfg = Config.New(); |
| 39 | + cfg.Settings.DefaultSavePath = dir; |
| 40 | + cfg.Settings.EnableNotifications = false; |
| 41 | + cfg.Settings.ChunkCount = 2; |
| 42 | + cfg.DefaultQueue.MaxConcurrent = 4; |
| 43 | + |
| 44 | + var manager = new DownloadManager(); |
| 45 | + manager.Initialize(cfg); |
| 46 | + |
| 47 | + const int count = 30; |
| 48 | + var vms = new List<DownloadItemViewModel>(); |
| 49 | + for (var i = 0; i < count; i++) |
| 50 | + vms.Add(manager.Add(new DownloadItem { Urls = new() { server.Url + $"file{i}.bin" }, SaveFolder = dir }, autoStart: true)); |
| 51 | + |
| 52 | + await PumpUntil(() => vms.All(v => v.Status == DownloadStatus.Completed), (int)TestTimeouts.SlowMs - 5000); |
| 53 | + |
| 54 | + Assert.All(vms, v => Assert.Equal(DownloadStatus.Completed, v.Status)); |
| 55 | + // The core assertion: no completed row still holds a live engine handle (or its package). |
| 56 | + Assert.All(vms, v => Assert.Null(v.Download)); |
| 57 | + |
| 58 | + // Display state the grid/resume rely on must survive the release. |
| 59 | + Assert.All(vms, v => |
| 60 | + { |
| 61 | + Assert.False(string.IsNullOrEmpty(v.FileName)); |
| 62 | + Assert.Equal(100, (int)v.Progress); |
| 63 | + }); |
| 64 | + } |
| 65 | + finally |
| 66 | + { |
| 67 | + try { Directory.Delete(dir, recursive: true); } catch { /* best-effort */ } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [AvaloniaFact(Timeout = TestTimeouts.SlowMs)] |
| 72 | + public async Task A_released_stopped_row_can_be_retried_to_completion() |
| 73 | + { |
| 74 | + var payload = new byte[64 * 1024]; |
| 75 | + new Random(11).NextBytes(payload); |
| 76 | + using var server = new Loopback(payload); |
| 77 | + |
| 78 | + var dir = Path.Combine(Path.GetTempPath(), "dldesktop_mem_retry_" + Guid.NewGuid().ToString("N")); |
| 79 | + Directory.CreateDirectory(dir); |
| 80 | + try |
| 81 | + { |
| 82 | + var cfg = Config.New(); |
| 83 | + cfg.Settings.DefaultSavePath = dir; |
| 84 | + cfg.Settings.EnableNotifications = false; |
| 85 | + |
| 86 | + var manager = new DownloadManager(); |
| 87 | + manager.Initialize(cfg); |
| 88 | + |
| 89 | + var vm = manager.Add(new DownloadItem { Urls = new() { server.Url + "retry.bin" }, SaveFolder = dir }, autoStart: true); |
| 90 | + |
| 91 | + // Stop it → terminal (Stopped) → engine released. |
| 92 | + manager.Cancel(vm); |
| 93 | + await PumpUntil(() => vm.Status == DownloadStatus.Stopped && vm.Download == null, 10000); |
| 94 | + Assert.Equal(DownloadStatus.Stopped, vm.Status); |
| 95 | + Assert.Null(vm.Download); // released on stop |
| 96 | + |
| 97 | + // Retry must rebuild a fresh engine and complete. |
| 98 | + manager.Retry(vm); |
| 99 | + await PumpUntil(() => vm.Status == DownloadStatus.Completed, 20000); |
| 100 | + Assert.Equal(DownloadStatus.Completed, vm.Status); |
| 101 | + Assert.Null(vm.Download); // released again after completing |
| 102 | + } |
| 103 | + finally |
| 104 | + { |
| 105 | + try { Directory.Delete(dir, recursive: true); } catch { /* best-effort */ } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private static async Task PumpUntil(Func<bool> condition, int timeoutMs) |
| 110 | + { |
| 111 | + var sw = Stopwatch.StartNew(); |
| 112 | + while (!condition() && sw.ElapsedMilliseconds < timeoutMs) |
| 113 | + { |
| 114 | + Dispatcher.UIThread.RunJobs(); |
| 115 | + await Task.Delay(25); |
| 116 | + } |
| 117 | + Dispatcher.UIThread.RunJobs(); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary>Minimal loopback HTTP server with Range support (same shape as IntegrationTests).</summary> |
| 121 | + private sealed class Loopback : IDisposable |
| 122 | + { |
| 123 | + private readonly HttpListener _listener; |
| 124 | + private readonly byte[] _data; |
| 125 | + public string Url { get; } |
| 126 | + |
| 127 | + public Loopback(byte[] data) |
| 128 | + { |
| 129 | + _data = data; |
| 130 | + var l = new TcpListener(IPAddress.Loopback, 0); |
| 131 | + l.Start(); |
| 132 | + var port = ((IPEndPoint)l.LocalEndpoint).Port; |
| 133 | + l.Stop(); |
| 134 | + Url = $"http://127.0.0.1:{port}/"; |
| 135 | + _listener = new HttpListener(); |
| 136 | + _listener.Prefixes.Add(Url); |
| 137 | + _listener.Start(); |
| 138 | + _ = Task.Run(LoopAsync); |
| 139 | + } |
| 140 | + |
| 141 | + private async Task LoopAsync() |
| 142 | + { |
| 143 | + while (_listener.IsListening) |
| 144 | + { |
| 145 | + HttpListenerContext ctx; |
| 146 | + try { ctx = await _listener.GetContextAsync(); } |
| 147 | + catch { break; } |
| 148 | + _ = Task.Run(() => Handle(ctx)); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private void Handle(HttpListenerContext ctx) |
| 153 | + { |
| 154 | + try |
| 155 | + { |
| 156 | + var resp = ctx.Response; |
| 157 | + resp.Headers["Accept-Ranges"] = "bytes"; |
| 158 | + if (ctx.Request.HttpMethod == "HEAD") |
| 159 | + { |
| 160 | + resp.ContentLength64 = _data.Length; |
| 161 | + resp.OutputStream.Close(); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + var range = ctx.Request.Headers["Range"]; |
| 166 | + int start = 0, end = _data.Length - 1; |
| 167 | + if (!string.IsNullOrEmpty(range) && range.StartsWith("bytes=", StringComparison.OrdinalIgnoreCase)) |
| 168 | + { |
| 169 | + var parts = range.Substring(6).Split('-'); |
| 170 | + if (parts.Length == 2) |
| 171 | + { |
| 172 | + if (int.TryParse(parts[0], out var s)) start = s; |
| 173 | + if (int.TryParse(parts[1], out var e)) end = e; |
| 174 | + } |
| 175 | + end = Math.Min(end, _data.Length - 1); |
| 176 | + start = Math.Max(0, Math.Min(start, end)); |
| 177 | + resp.StatusCode = 206; |
| 178 | + resp.AddHeader("Content-Range", $"bytes {start}-{end}/{_data.Length}"); |
| 179 | + } |
| 180 | + |
| 181 | + var len = end - start + 1; |
| 182 | + resp.ContentLength64 = len; |
| 183 | + resp.OutputStream.Write(_data, start, len); |
| 184 | + resp.OutputStream.Close(); |
| 185 | + } |
| 186 | + catch |
| 187 | + { |
| 188 | + try { ctx.Response.Abort(); } catch { /* ignore */ } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + public void Dispose() |
| 193 | + { |
| 194 | + try { _listener.Stop(); } catch { /* ignore */ } |
| 195 | + try { _listener.Close(); } catch { /* ignore */ } |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments