diff --git a/docs/engine/front-end.md b/docs/engine/front-end.md index 2e7f0511..848b0439 100644 --- a/docs/engine/front-end.md +++ b/docs/engine/front-end.md @@ -523,7 +523,7 @@ Each change is written to `starlancer.ini` at once, in the section `[OpenReliant - GET MODS in white, centred on (320, 95). - The framed list from (45, 136), with the catalogue's mods grouped by their top category, the first folder of their category path: a heading row for each group, in the large font and blue as the controls tab's headings are, with the group's name in capitals (`- SHIPS`), then the group's mods sorted by their full path and their name, indented by 14 pixels, each as its name and version from the index. A click on a heading folds the group, which hides its mods and turns the heading's `-` into a `+`; another click unfolds it. Mods without a category come last, under OTHER MODS. A mod that needs a newer OpenReliant is red; otherwise the selected mod is white, a mod installed in an older version is gold, and an installed mod is dimmed. While the repositories are read, the list says READING THE CATALOG. If none can be read, the list says CAN'T READ THE CATALOG and why, in red. If one of several can't be read, its name and the reason are in red above the list, ending at the panel's right edge where the mods screen's RESTART TO APPLY is, and the others' mods are listed. An empty catalogue says THE CATALOG OFFERS NO MODS. The screen lists at most 127 mods, since each mod and each group takes one of the list's 255 rows. -- The framed panel from (401, 136), which says CHOOSE A MOD while nothing is selected, and otherwise shows the selected mod's thumbnail, downloaded when the mod is selected and fitted into the same box as on the mods screen; its name; right under it, its status line, in gold: INSTALLED with the installed version, BY HAND where the settings file doesn't record the install, and UPDATE TO with the catalogue's version where it is newer, INSTALLED BY HAND AS A FOLDER: INSTALL WON'T REPLACE IT where a folder mod of the name is in the `mods` folder, DOWNLOADING with the percentage, CANCELING once CANCEL was pressed, or INSTALLED: RESTART TO APPLY; in red: NEEDS OPENRELIANT with the version, or why the install failed. Then its version and author; its full category, as IN SHIPS / FIGHTERS / ALLIANCE; the repository that lists it, as FROM openreliant-mods; the OpenReliant it needs, in red when it is newer than this one; its size and its description, which takes the lines left. During a download, a progress bar at the bottom of the panel, measured against the size the catalogue gives. +- The framed panel from (401, 136), which says CHOOSE A MOD while nothing is selected, and otherwise shows the selected mod's thumbnail, downloaded when the mod is selected and fitted into the same box as on the mods screen; its name; right under it, its status line, in gold: INSTALLED with the installed version, BY HAND where the settings file doesn't record the install, and UPDATE TO with the catalogue's version where it is newer, INSTALLED BY HAND AS A FOLDER: INSTALL WON'T REPLACE IT where a folder mod of the name is in the `mods` folder, DOWNLOADING with the percentage, CANCELING once CANCEL was pressed, or INSTALLED: RESTART TO APPLY; in red: NEEDS OPENRELIANT with the version, or why the install failed. Then its version and author; its full category, as IN SHIPS / FIGHTERS / ALLIANCE; the repository that lists it, as FROM openreliant-mods; the OpenReliant it needs, in red when it is newer than this one; its size and its description, which takes the lines left. During a download, a progress bar at the bottom of the panel, measured against the size the catalogue gives, and under the panel, above the buttons, two lines in blue: how much has arrived of the size, as 64.3 MB OF 153.7 MB, then the speed over the last 3 seconds and the time left at that speed, as 6.1 MB/S, 2 MIN LEFT, once the download has run for a second. - The settings screen's buttons: OK and MAIN MENU in the left column. In the right column, INSTALL (or UPDATE when an older version is installed) where RESET DEFAULTS is, dimmed when there is nothing to do, and RELOAD where CANCEL CHANGES is, which reads the repositories again and tries again the thumbnails that failed. While a mod downloads, one download runs at a time and the catalogue can't change under it, so RELOAD is dimmed, and INSTALL reads CANCEL while the downloading mod is chosen and is dimmed while another is. CANCEL stops the download, which deletes its part file; the panel says CANCELING until the download's thread has stopped, then shows the mod as it was before. OK and Escape go back to the mods screen, which rereads the `mods` folder if a mod was installed. The repositories `starlancer.ini` names ([Repositories](../guide/modding.md#repositories)) are read one after another into one catalogue, where the first repository to list an id wins, and the catalogue is kept until RELOAD or until the front end closes. A mod without the keys it needs (`id`, `archive`, `sha256`, `size`), whose id can't name a mod, or whose size is over 4294967295 bytes, the most a HOG archive's 32-bit offsets reach, is skipped with a line in the log, which escapes the id; an index in a newer format fails with THE CATALOG NEEDS A NEWER OPENRELIANT. Every download goes through [`web.zig`](../../src/engine/web.zig), on a thread of its own with its own HTTP client, and over HTTPS: `http` is changed to `https` before each request and each redirect, which the download follows itself, five at most; any other scheme is refused. Each request sends `OpenReliant` as its user agent. The player's own machine (`localhost` and the names under it, `127.0.0.0/8` and `0.0.0.0` in every form the system's resolver reads, such as `127.1`, and `::1`, `::` and the IPv4 addresses mapped into IPv6) is read only for a repository whose URL names it, and for the thumbnails and archives its index links, over plain HTTP. Anywhere else, a link to the player's machine, or a redirect to it from another host, fails with THE CATALOG POINTS AT THIS COMPUTER, so that a repository on the web can't make OpenReliant send requests to the services on the player's machine. A host name that the system's resolver turns into the player's own address isn't caught, since only the link itself is checked, but such a link is read over HTTPS, so a service there gets the start of a secure connection that it can't complete, never a request. A failure says what happened: the server's name can't be found, a secure connection can't be made, the system's certificates can't be loaded, too many redirects, a bad redirect, and so on. An index or a thumbnail gives up after 60 seconds, or after 15 seconds without anything arriving, and an archive after 30 seconds without anything arriving, with THE SERVER DOESN'T ANSWER. An index is read up to 4 MiB and a thumbnail up to 2 MiB; an archive is read up to the size the catalogue gives, and a download that goes past it, or that the server announces at another size, stops at once. INSTALL downloads the archive into `.hog.part` in the `mods` folder, hashing the bytes on their way to the file. Once the archive is complete and matches the catalogue's digest, the digest is written as `.hog.sha256.part`, the archive's part is renamed to `.hog`, replacing an older copy, and the checksum file's part is renamed to `.hog.sha256`; then the install is recorded in `[OpenReliantInstalledMods]` with the repository's name, so that the loader refuses the archive without its checksum file from then on. A download that fails or doesn't match leaves nothing behind; if the checksum file can't be put in place, the archive is removed again, with the checksum file of the copy it replaced. Leaving the screen lets a download continue. CANCEL stops it: its thread cancels the request, deletes the archive's part file and ends, and only then does the screen let the install go, so that an install of the same mod started right after can't run into the part file being deleted. Closing the front end lets go of a download without waiting, so that quitting is never held up, not even by a lookup of a host's address, which nothing can interrupt: the download's thread cancels it and ends on its own, deleting an archive's part file, unless OpenReliant has ended first, in which case the next install of the mod replaces the part file. The mods screen uses the catalogue too, to say which of its mods have an update: with CHECK FOR UPDATES on ([Newer releases](../guide/configuration.md#newer-releases)), entering it starts this reading in the background, once per run, and each of its passes takes in what has arrived, as this screen's passes do; with it off, nothing is read, but a catalogue this screen has read serves all the same. Opened from the mods screen, GET MODS chooses the mod the mods screen had chosen, unfolding its group, where the catalogue lists it. The GET MODS button on the mods screen uses the settings screen's shapes, at (45, 116) with its label to its right, and is shown only when the settings file names a repository. diff --git a/docs/guide/modding.md b/docs/guide/modding.md index c08dfbac..a6562bd2 100644 --- a/docs/guide/modding.md +++ b/docs/guide/modding.md @@ -220,8 +220,9 @@ leaves nothing in the `mods` folder, and the panel says CANCELING for a moment w stops. Leaving the screen lets a download go on. OK goes back to the mods screen, which now lists the installed mods. Like every change on the mods screen, they take effect at the next start. -The catalogue and the downloads run in the background, so the menus keep working, and the panel -shows the download's progress. Besides the check for a newer release ([Newer +The catalogue and the downloads run in the background, so the menus keep working. During a +download, the panel shows how much has arrived, the speed over the last few seconds and the time +left, and the log gives the average speed once the mod is installed. Besides the check for a newer release ([Newer releases](configuration.md#newer-releases)), OpenReliant connects to the web only for this screen: to read the repositories, and to download the mods you install. Everything is downloaded over HTTPS: a plain `http` link is changed to `https` before it is requested, and so is every redirect, so diff --git a/src/engine/game/bigfile/catalogue.zig b/src/engine/game/bigfile/catalogue.zig index 8e2f4fa0..27eaf9cb 100644 --- a/src/engine/game/bigfile/catalogue.zig +++ b/src/engine/game/bigfile/catalogue.zig @@ -687,11 +687,21 @@ pub const Install = struct { install.id = id; } - /// The download's state. Takes its answer in once it has arrived, logging how it ended. + /// The download's state. Takes its answer in once it has arrived, logging how it ended, with + /// the average speed of a download that succeeded, which helps compare it with a browser's. pub fn state(install: *Install) State { + // The progress goes with the answer, so it is read first. + const received, const elapsed = if (install.download.progress()) |measured| + .{ measured.received.load(.monotonic), measured.elapsed.load(.monotonic) } + else + .{ 0, 0 }; if (install.download.take()) |answer| { if (answer) |_| { - log.info("installed the mod {s} in the mods folder", .{install.id.?}); + const megabytes = @as(f64, @floatFromInt(received)) / (1024 * 1024); + const seconds = @as(f64, @floatFromInt(elapsed)) / std.time.ns_per_s; + log.info("installed the mod {s} in the mods folder: {d:.1} MB in {d:.1} s, {d:.1} MB/s", .{ + install.id.?, megabytes, seconds, if (seconds > 0) megabytes / seconds else 0, + }); install.done = true; } else |err| if (err == error.Canceled) { log.info("the download of the mod {s} was cancelled", .{install.id.?}); @@ -708,10 +718,10 @@ pub const Install = struct { return .idle; } - /// The share of the archive received so far, while it downloads. - pub fn fraction(install: Install) ?f32 { - const progress = install.download.progress() orelse return null; - return progress.fraction(); + /// How far the download has got, while it runs: the bytes received, the archive's size, the + /// recent speed and the time left. + pub fn transfer(install: Install) ?*const web.Progress { + return install.download.progress(); } /// Asks the download to stop (`web.Download.cancel`), while it runs. The install stays diff --git a/src/engine/game/interface/mod_catalogue.zig b/src/engine/game/interface/mod_catalogue.zig index fe5667f9..09cc3d77 100644 --- a/src/engine/game/interface/mod_catalogue.zig +++ b/src/engine/game/interface/mod_catalogue.zig @@ -4,7 +4,8 @@ //! MISSIONS, ...) under headings that fold and unfold, with the chosen mod's thumbnail, version, //! author, category, repository, size and description in the panel next to it. INSTALL downloads //! the chosen mod's archive into the `mods` folder, checks it against the catalogue's digest and -//! records in `starlancer.ini` which repository it came from, showing the progress in the panel. +//! records in `starlancer.ini` which repository it came from. The panel shows the download's +//! progress, speed and time left. //! UPDATE does the same for a mod that is installed in an older version. RELOAD reads the //! repositories again. The indexes and the downloads run on other threads, so the screen keeps //! running while they come in. The repositories are read one after another, and one download @@ -120,6 +121,15 @@ const bar_height = 8; const bar_inside = 2; const bar_at: [2]i32 = .{ details_frame.at[0] + details_inside, details_frame.at[1] + frame_height - details_inside - bar_height }; const bar_width = details_lines.width; +/// Where the download's two lines start, under the panel, so that they never cover the mod's +/// details: how much has arrived, then the speed and the time left. They end above the buttons. +const transfer_at: [2]i32 = .{ details_frame.at[0], details_frame.at[1] + frame_height + transfer_gap }; +const transfer_gap = 4; + +comptime { + const transfer_end = transfer_at[1] + 2 * details_lines.height; + std.debug.assert(transfer_end <= settings.Button.reset_defaults.rect().y); +} /// The buttons, in the settings screen's places: INSTALL (or UPDATE, or CANCEL while the chosen /// mod downloads) where RESET DEFAULTS is, and RELOAD where CANCEL CHANGES is. @@ -905,7 +915,25 @@ pub const ModCatalogue = struct { shown.most = @min(description_lines.most, @as(usize, @intCast(@max(room, 1)))); try canvas.wrapped(font, .{ x, y }, description, canvas_module.blue, .left, shown); } - if (downloading) screen.drawBar(canvas); + if (downloading) { + try screen.drawTransfer(canvas); + screen.drawBar(canvas); + } + } + + /// Draws the download's lines under the panel: how much has arrived of how much, then the + /// recent speed and the time left, once they are known. + fn drawTransfer(screen: ModCatalogue, canvas: Canvas) canvas_module.Error!void { + if (screen.install.cancelling()) return; + const progress = screen.install.transfer() orelse return; + const font = canvas.fonts.small; + var buffer: [name_buffer]u8 = undefined; + const received = progress.received.load(.monotonic); + try canvas.wrapped(font, transfer_at, receivedText(&buffer, received, progress.total.load(.monotonic)), canvas_module.blue, .left, details_lines); + const speed = progress.speed.load(.monotonic); + if (speed == 0) return; + const below: [2]i32 = .{ transfer_at[0], transfer_at[1] + details_lines.height }; + try canvas.wrapped(font, below, speedText(&buffer, speed, progress.secondsLeft()), canvas_module.blue, .left, details_lines); } /// The status line of the mod at `index` in the catalogue, written into `buffer`: the download's @@ -918,8 +946,9 @@ pub const ModCatalogue = struct { const entry = screen.entries()[index]; if (screen.installing == index) { if (screen.install.cancelling()) return .{ .text = "CANCELING", .colour = canvas_module.gold }; - const text = if (screen.install.fraction()) |fraction| - std.mem.print(buffer, "DOWNLOADING {d}%", .{@as(u32, @intFromFloat(fraction * 100))}) catch "DOWNLOADING" + const fraction = if (screen.install.transfer()) |progress| progress.fraction() else null; + const text = if (fraction) |share| + std.mem.print(buffer, "DOWNLOADING {d}%", .{@as(u32, @intFromFloat(share * 100))}) catch "DOWNLOADING" else "DOWNLOADING"; return .{ .text = text, .colour = canvas_module.gold }; @@ -953,7 +982,8 @@ pub const ModCatalogue = struct { /// Draws the progress bar at the bottom of the panel, filled to the download's progress. fn drawBar(screen: ModCatalogue, canvas: Canvas) void { canvas.box(bar_at, .{ bar_width, bar_height }); - const fraction = screen.install.fraction() orelse return; + const progress = screen.install.transfer() orelse return; + const fraction = progress.fraction() orelse return; const filled: i32 = @intFromFloat(@round(fraction * @as(f32, @floatFromInt(bar_width - 2 * bar_inside)))); if (filled <= 0) return; canvas.wipe(.{ bar_at[0] + bar_inside, bar_at[1] + bar_inside }, .{ bar_at[0] + bar_inside + filled - 1, bar_at[1] + bar_height - bar_inside - 1 }, canvas_module.gold); @@ -1019,6 +1049,35 @@ fn sizeText(buffer: []u8, bytes: u64) []const u8 { return std.mem.print(buffer, "{d} KB", .{(bytes + kilobyte / 2) / kilobyte}) catch ""; } +/// How much of a download has arrived, written into `buffer`: `64.3 MB OF 153.7 MB`, or only +/// what has arrived where the size is unknown (`total` is 0). +fn receivedText(buffer: *[name_buffer]u8, received: u64, total: u64) []const u8 { + var have: [name_buffer]u8 = undefined; + const arrived = sizeText(&have, received); + if (total == 0) return std.mem.print(buffer, "{s}", .{arrived}) catch arrived; + var whole: [name_buffer]u8 = undefined; + return std.mem.print(buffer, "{s} OF {s}", .{ arrived, sizeText(&whole, total) }) catch arrived; +} + +/// A download's speed in bytes per second and the seconds left, where they are known, written +/// into `buffer`: `6.1 MB/S, 2 MIN LEFT`. +fn speedText(buffer: *[name_buffer]u8, speed: u64, seconds_left: ?u64) []const u8 { + var size: [name_buffer]u8 = undefined; + const per_second = sizeText(&size, speed); + const seconds = seconds_left orelse return std.mem.print(buffer, "{s}/S", .{per_second}) catch ""; + var time: [name_buffer]u8 = undefined; + return std.mem.print(buffer, "{s}/S, {s} LEFT", .{ per_second, timeText(&time, seconds) }) catch ""; +} + +/// A time left as the panel shows it, written into `buffer`: in seconds under a minute, else in +/// minutes, rounded up, with the hours from an hour on: `45 S`, `2 MIN`, `1 H 5 MIN`. +fn timeText(buffer: []u8, seconds: u64) []const u8 { + if (seconds < std.time.s_per_min) return std.mem.print(buffer, "{d} S", .{seconds}) catch ""; + const minutes = std.math.divCeil(u64, seconds, std.time.s_per_min) catch unreachable; + if (minutes < 60) return std.mem.print(buffer, "{d} MIN", .{minutes}) catch ""; + return std.mem.print(buffer, "{d} H {d} MIN", .{ minutes / 60, minutes % 60 }) catch ""; +} + /// Where the name of the `place`th visible row starts. fn nameAt(place: usize) [2]i32 { return .{ name_x, first_row + @as(i32, @intCast(place)) * row_spacing }; @@ -1377,6 +1436,18 @@ test "the mods screen asks which version the catalogue offers for a mod, and GET try std.testing.expectEqual(1, screen.chosen.?); } +test "the download's lines" { + var buffer: [name_buffer]u8 = undefined; + try std.testing.expectEqualStrings("64.3 MB OF 153.7 MB", receivedText(&buffer, 67_423_436, 161_166_131)); + try std.testing.expectEqualStrings("512 KB", receivedText(&buffer, 512 * 1024, 0)); + try std.testing.expectEqualStrings("6.1 MB/S, 2 MIN LEFT", speedText(&buffer, 6_396_314, 95)); + try std.testing.expectEqualStrings("850 KB/S", speedText(&buffer, 850 * 1024, null)); + try std.testing.expectEqualStrings("45 S", timeText(&buffer, 45)); + try std.testing.expectEqualStrings("1 MIN", timeText(&buffer, 60)); + try std.testing.expectEqualStrings("2 MIN", timeText(&buffer, 61)); + try std.testing.expectEqualStrings("1 H 5 MIN", timeText(&buffer, 3900)); +} + test sizeText { var buffer: [32]u8 = undefined; try std.testing.expectEqualStrings("30.8 MB", sizeText(&buffer, 32297124)); diff --git a/src/engine/web.zig b/src/engine/web.zig index 94acdad1..03e25c03 100644 --- a/src/engine/web.zig +++ b/src/engine/web.zig @@ -43,7 +43,12 @@ pub const redirect_size = std.fmt.parseIntSizeSuffix("8KiB", 10) catch unreachab /// another host. const most_redirects = 5; /// How often a download's thread looks at its limits and at whether its owner has let go of it. +/// It measures the download's speed at the same time. const watch_step: Io.Duration = .fromMilliseconds(100); +/// The speed is the bytes received over the last `speed_window`, so it follows changes in the +/// connection without jumping at each read. It is shown once it covers at least `least_speed_span`. +const speed_window: Io.Duration = .fromSeconds(3); +const least_speed_span: Io.Duration = .fromSeconds(1); /// The room for a host name, which DNS limits to 253 characters. const host_size = 256; @@ -145,6 +150,10 @@ pub const Progress = struct { /// How many times something has arrived: the answer's head, and each part of its body. A /// download whose count stays the same has stalled (`Limits.quiet`). arrivals: std.atomic.Value(u32) = .init(0), + /// The recent speed in bytes per second (`speed_window`), or 0 while it isn't known yet. + speed: std.atomic.Value(u64) = .init(0), + /// The time since the download started, in nanoseconds. + elapsed: std.atomic.Value(u64) = .init(0), /// Counts something that arrived. pub fn heard(progress: *Progress) void { @@ -164,6 +173,43 @@ pub const Progress = struct { const received = progress.received.load(.monotonic); return @min(1, @as(f32, @floatFromInt(received)) / @as(f32, @floatFromInt(total))); } + + /// The seconds left at the recent speed, rounded up; null while the length or the speed is + /// unknown. + pub fn secondsLeft(progress: *const Progress) ?u64 { + const total = progress.total.load(.monotonic); + const speed = progress.speed.load(.monotonic); + if (total == 0 or speed == 0) return null; + const left = total -| progress.received.load(.monotonic); + return std.math.divCeil(u64, left, speed) catch unreachable; + } +}; + +/// Measures a download's speed from the bytes received at each step of `watch`: the bytes received +/// over the last `speed_window`, divided by the time they took. +const SpeedMeter = struct { + samples: [count]Sample = undefined, + /// How many samples are kept, and where the next one goes. + kept: usize = 0, + next: usize = 0, + + /// One more than the steps in a window, since the window runs from the oldest sample to the + /// newest. + const count: usize = @intCast(@divExact(speed_window.nanoseconds, watch_step.nanoseconds) + 1); + const least_span: u64 = @intCast(least_speed_span.nanoseconds); + const Sample = struct { at: u64, received: u64 }; + + /// Adds the bytes `received` so far, `at` nanoseconds after the download started. Returns the + /// speed in bytes per second, or 0 while the samples span less than `least_speed_span`. + fn add(meter: *SpeedMeter, at: u64, received: u64) u64 { + meter.samples[meter.next] = .{ .at = at, .received = received }; + meter.next = (meter.next + 1) % count; + meter.kept = @min(meter.kept + 1, count); + const oldest = meter.samples[if (meter.kept < count) 0 else meter.next]; + const span = at - oldest.at; + if (span < least_span) return 0; + return @intCast(std.math.mulWide(u64, received - oldest.received, std.time.ns_per_s) / span); + } }; /// How long a download may take, past which it gives up with `error.Timeout`. @@ -471,15 +517,19 @@ const Ended = enum { returned, timed_out, stopped }; /// Waits for a job to return, looking every `watch_step` at whether it is past `limits`, by the /// time since it started and since something last arrived (`Progress.arrivals`), or asked to -/// `stop`. -fn watch(io: Io, returned: *Io.Event, progress: *const Progress, stop: *const std.atomic.Value(bool), limits: Limits) Ended { +/// `stop`. Each step, it records in `progress` the time since the start and the recent speed. +fn watch(io: Io, returned: *Io.Event, progress: *Progress, stop: *const std.atomic.Value(bool), limits: Limits) Ended { const started = Io.Clock.awake.now(io); var arrivals = progress.arrivals.load(.monotonic); var heard_at = started; + var meter: SpeedMeter = .{}; while (true) { if (returned.waitTimeout(io, .{ .duration = .{ .raw = watch_step, .clock = .awake } })) |_| return .returned else |_| {} if (stop.load(.acquire)) return .stopped; const now = Io.Clock.awake.now(io); + const elapsed: u64 = @intCast(@max(0, started.durationTo(now).nanoseconds)); + progress.elapsed.store(elapsed, .monotonic); + progress.speed.store(meter.add(elapsed, progress.received.load(.monotonic)), .monotonic); if (limits.whole) |whole| if (started.durationTo(now).nanoseconds >= whole.nanoseconds) return .timed_out; const counted = progress.arrivals.load(.monotonic); if (counted != arrivals) { @@ -542,6 +592,37 @@ test ipv4 { } } +test SpeedMeter { + const step: u64 = @intCast(watch_step.nanoseconds); + var meter: SpeedMeter = .{}; + // Under a second, the speed isn't known yet. + try std.testing.expectEqual(0, meter.add(0, 0)); + for (1..10) |n| try std.testing.expectEqual(0, meter.add(n * step, n * 100_000)); + // At 100 KB a step, ten steps a second: 1 MB/s. + try std.testing.expectEqual(1_000_000, meter.add(10 * step, 1_000_000)); + // Past the window, only the last 3 seconds count: the speed drops to 0.5 MB/s once the + // last 3 seconds came at that speed. + var received: u64 = 1_000_000; + for (11..41) |n| { + received += 50_000; + _ = meter.add(n * step, received); + } + try std.testing.expectEqual(500_000, meter.add(41 * step, received + 50_000)); +} + +test "Progress.secondsLeft" { + var progress: Progress = .{}; + try std.testing.expectEqual(null, progress.secondsLeft()); + progress.total.store(10_000_000, .monotonic); + try std.testing.expectEqual(null, progress.secondsLeft()); + progress.received.store(4_000_000, .monotonic); + progress.speed.store(1_000_000, .monotonic); + try std.testing.expectEqual(6, progress.secondsLeft()); + // Rounded up. + progress.speed.store(4_000_000, .monotonic); + try std.testing.expectEqual(2, progress.secondsLeft()); +} + /// Jobs for the tests: one that answers at once, one that waits past any limit, one that ignores /// its cancelling for a while, as the system's resolver does, and one that keeps something /// arriving.