| 1 | @[has_globals] |
| 2 | module main |
| 3 | |
| 4 | import blip.vlib.blip.machine |
| 5 | import net |
| 6 | import sync |
| 7 | import time |
| 8 | |
| 9 | // mirrors_machine is a blip machine set up the way load_config sets one up, |
| 10 | // for what a mirrors file needs |
| 11 | fn mirrors_machine() &machine.Machine { |
| 12 | mut m := machine.new_standalone(&QuietConfigHost{}) |
| 13 | m.prims['array-append!'] = prim_array_append |
| 14 | return m |
| 15 | } |
| 16 | |
| 17 | fn mirrors_read(mut m machine.Machine, var string) ![]string { |
| 18 | return m.run_str(var)!.as_vector().data.map(it.as_string()) |
| 19 | } |
| 20 | |
| 21 | fn test_mirrors_lip_round_trip() ! { |
| 22 | urls := [ |
| 23 | 'https://ok.example/' + r'$repo/os/$arch', |
| 24 | 'https://quote.example/a"b', |
| 25 | 'https://backslash.example/a\\b', |
| 26 | 'https://newline.example/a\nb', |
| 27 | 'https://tab.example/a\tb', |
| 28 | // closes its string and the form, then defines something |
| 29 | 'https://x.example/"]) (define pwned 1) (array-append! mirrors-arch ["', |
| 30 | ] |
| 31 | text := mirrors_lip('arch', urls, '2026-11-08') |
| 32 | assert text.starts_with('; mirrors for arch, ranked by vpac k -w on 2026-11-08\n') |
| 33 | mut m := mirrors_machine() |
| 34 | m.run_str(text)! |
| 35 | assert mirrors_read(mut m, 'mirrors-arch')! == urls |
| 36 | if _ := m.run_str('pwned') { |
| 37 | assert false, 'a url ran as code' |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn test_mirrors_lip_appends() ! { |
| 42 | // the file adds to a list the config may have started already |
| 43 | mut m := mirrors_machine() |
| 44 | m.run_str('(define mirrors-arch ["https://first.example/"])')! |
| 45 | m.run_str(mirrors_lip('arch', ['https://second.example/'], 'today'))! |
| 46 | assert mirrors_read(mut m, 'mirrors-arch')! == ['https://first.example/', 'https://second.example/'] |
| 47 | } |
| 48 | |
| 49 | fn test_mirrors_lip_empty() ! { |
| 50 | mut m := mirrors_machine() |
| 51 | m.run_str(mirrors_lip('artix', [], 'today'))! |
| 52 | assert mirrors_read(mut m, 'mirrors-artix')!.len == 0 |
| 53 | } |
| 54 | |
| 55 | // ProbeTally is what a fake probe records across the batch's threads: how |
| 56 | // many ran at once, at the most, and which mirrors it was handed |
| 57 | @[heap] |
| 58 | struct ProbeTally { |
| 59 | mut: |
| 60 | mu &sync.Mutex = sync.new_mutex() |
| 61 | in_flight int |
| 62 | peak int |
| 63 | seen []string |
| 64 | // stalled is set by the first probe that gave up waiting for company, |
| 65 | // so a serial batch fails the test in one wait rather than one per mirror |
| 66 | stalled bool |
| 67 | } |
| 68 | |
| 69 | fn (mut t ProbeTally) enter(url string) { |
| 70 | t.mu.lock() |
| 71 | t.in_flight++ |
| 72 | if t.in_flight > t.peak { |
| 73 | t.peak = t.in_flight |
| 74 | } |
| 75 | t.seen << url |
| 76 | t.mu.unlock() |
| 77 | } |
| 78 | |
| 79 | fn (mut t ProbeTally) leave() { |
| 80 | t.mu.lock() |
| 81 | t.in_flight-- |
| 82 | t.mu.unlock() |
| 83 | } |
| 84 | |
| 85 | // wait_for_peak holds a probe until n probes have been in flight at once, or |
| 86 | // a second has passed: probes that run one after another never get there |
| 87 | fn (mut t ProbeTally) wait_for_peak(n int) { |
| 88 | deadline := time.now().add(time.second) |
| 89 | for { |
| 90 | t.mu.lock() |
| 91 | done := t.peak >= n || t.stalled |
| 92 | t.mu.unlock() |
| 93 | if done { |
| 94 | return |
| 95 | } |
| 96 | if time.now() > deadline { |
| 97 | t.mu.lock() |
| 98 | t.stalled = true |
| 99 | t.mu.unlock() |
| 100 | return |
| 101 | } |
| 102 | time.sleep(time.millisecond) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | fn probe_mirrors(n int) []Mirror { |
| 107 | return []Mirror{len: n, init: Mirror{ |
| 108 | url: 'https://m${index}.example/' |
| 109 | }} |
| 110 | } |
| 111 | |
| 112 | // probe_batch_with runs a quiet batch with the rank parallelism set to conc, |
| 113 | // the globals it reads put back after |
| 114 | fn probe_batch_with(conc int, mirrors []Mirror, probe ProbeFn) []Probe { |
| 115 | saved_rank, saved_net := rank_parallel, parallel_downloads |
| 116 | defer { |
| 117 | rank_parallel, parallel_downloads = saved_rank, saved_net |
| 118 | } |
| 119 | rank_parallel, parallel_downloads = conc, conc |
| 120 | mut ui := new_trans_ui() |
| 121 | return probe_batch(mut ui, 'test', mirrors, probe, true) |
| 122 | } |
| 123 | |
| 124 | fn test_probe_batch_answers_once_per_mirror() { |
| 125 | mirrors := probe_mirrors(13) |
| 126 | mut tally := &ProbeTally{} |
| 127 | // every other mirror fails: a failed probe is still its mirror's answer |
| 128 | out := probe_batch_with(4, mirrors, fn [mut tally] (m Mirror) Probe { |
| 129 | tally.enter(m.url) |
| 130 | tally.leave() |
| 131 | return Probe{ |
| 132 | url: m.url |
| 133 | ok: m.url.all_after('//m').all_before('.').int() % 2 == 0 |
| 134 | } |
| 135 | }) |
| 136 | assert out.len == mirrors.len |
| 137 | mut got := out.map(it.url) |
| 138 | got.sort() |
| 139 | mut want := mirrors.map(it.url) |
| 140 | want.sort() |
| 141 | assert got == want |
| 142 | assert out.filter(it.ok).len == 7 |
| 143 | tally.seen.sort() |
| 144 | assert tally.seen == want |
| 145 | } |
| 146 | |
| 147 | fn test_probe_batch_probes_run_in_parallel() { |
| 148 | // the probes must overlap, conc of them at once and never more: a probe |
| 149 | // evaluated by the collector before its spawn runs the batch serially |
| 150 | mirrors := probe_mirrors(9) |
| 151 | mut tally := &ProbeTally{} |
| 152 | out := probe_batch_with(3, mirrors, fn [mut tally] (m Mirror) Probe { |
| 153 | tally.enter(m.url) |
| 154 | tally.wait_for_peak(3) |
| 155 | tally.leave() |
| 156 | return Probe{ |
| 157 | url: m.url |
| 158 | ok: true |
| 159 | } |
| 160 | }) |
| 161 | assert out.len == 9 |
| 162 | assert !tally.stalled, 'the probes ran one at a time' |
| 163 | assert tally.peak == 3 |
| 164 | } |
| 165 | |
| 166 | fn test_probe_batch_zero_parallelism_still_probes() { |
| 167 | // a parallelism of 0 slipped past the config's floor starts one worker, |
| 168 | // not none (which would leave the collector waiting forever) |
| 169 | mirrors := probe_mirrors(5) |
| 170 | mut tally := &ProbeTally{} |
| 171 | out := probe_batch_with(0, mirrors, fn [mut tally] (m Mirror) Probe { |
| 172 | tally.enter(m.url) |
| 173 | tally.leave() |
| 174 | return Probe{ |
| 175 | url: m.url |
| 176 | ok: true |
| 177 | } |
| 178 | }) |
| 179 | assert out.len == 5 |
| 180 | assert tally.peak == 1 |
| 181 | } |
| 182 | |
| 183 | fn test_probe_batch_more_workers_than_mirrors() { |
| 184 | mirrors := probe_mirrors(2) |
| 185 | out := probe_batch_with(16, mirrors, fn (m Mirror) Probe { |
| 186 | return Probe{ |
| 187 | url: m.url |
| 188 | ok: true |
| 189 | } |
| 190 | }) |
| 191 | assert out.len == 2 |
| 192 | } |
| 193 | |
| 194 | fn test_probe_batch_empty() { |
| 195 | out := probe_batch_with(4, []Mirror{}, fn (m Mirror) Probe { |
| 196 | assert false, 'no mirror to probe' |
| 197 | return Probe{} |
| 198 | }) |
| 199 | assert out.len == 0 |
| 200 | } |
| 201 | |
| 202 | // TrickleServer is one trickle url's listener and the body shape it serves, |
| 203 | // handed to its serving thread by channel |
| 204 | struct TrickleServer { |
| 205 | listener &net.TcpListener |
| 206 | total int |
| 207 | pause time.Duration |
| 208 | } |
| 209 | |
| 210 | // the trickle server's spec crosses to its serving thread by channel as a |
| 211 | // value, and the thread spawns with no arguments, like the fetcher's jobs in |
| 212 | // mirrors.v: the arguments of a spawned call have been seen carrying another |
| 213 | // probe's values, and an answer that takes another probe's totals holds that |
| 214 | // probe's fetch past its deadline |
| 215 | __global server_specs = chan TrickleServer{cap: 16} |
| 216 | |
| 217 | // trickle_mirror_worker answers every connection on the listener with a body |
| 218 | // of total bytes, sent one byte each pause: each read lands well inside the |
| 219 | // socket timeout while the whole answer takes total pauses. connections are |
| 220 | // served one at a time — a probe makes one connection, and a client that |
| 221 | // hangs up ends its connection's answer at the next write, so the next |
| 222 | // probe's connection is accepted soon after. the loop ends when the listener |
| 223 | // closes |
| 224 | fn trickle_mirror_worker() { |
| 225 | spec := <-server_specs or { return } |
| 226 | mut l := spec.listener |
| 227 | for { |
| 228 | mut conn := l.accept() or { return } |
| 229 | trickle_answer(mut conn, spec.total, spec.pause) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | fn trickle_answer(mut conn net.TcpConn, total int, pause time.Duration) { |
| 234 | defer { |
| 235 | conn.close() or {} |
| 236 | } |
| 237 | mut request := []u8{len: 4096} |
| 238 | conn.read(mut request) or { return } |
| 239 | conn.write_string('HTTP/1.1 200 OK\r\nContent-Length: ${total}\r\nConnection: close\r\n\r\n') or { |
| 240 | return |
| 241 | } |
| 242 | for _ in 0 .. total { |
| 243 | conn.write_string('x') or { return } |
| 244 | time.sleep(pause) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // trickle_url starts a trickling mirror on a port of its own and answers its |
| 249 | // url with the listener, which the test closes |
| 250 | fn trickle_url(total int, pause time.Duration) !(&net.TcpListener, string) { |
| 251 | mut l := net.listen_tcp(.ip, '127.0.0.1:0')! |
| 252 | port := l.addr()!.port()! |
| 253 | server_specs <- TrickleServer{ |
| 254 | listener: l |
| 255 | total: total |
| 256 | pause: pause |
| 257 | } |
| 258 | spawn trickle_mirror_worker() |
| 259 | return l, 'http://127.0.0.1:${port}/core.db' |
| 260 | } |
| 261 | |
| 262 | // a mirror that keeps every read short of the socket timeout but the whole |
| 263 | // answer past the rank timeout fails the probe, close to the timeout and not |
| 264 | // at the answer's end (some six seconds here) |
| 265 | fn test_timed_get_bounds_the_whole_answer() ! { |
| 266 | saved := rank_timeout |
| 267 | defer { |
| 268 | rank_timeout = saved |
| 269 | } |
| 270 | rank_timeout = 1 |
| 271 | mut l, url := trickle_url(30, 200 * time.millisecond)! |
| 272 | defer { |
| 273 | l.close() or {} |
| 274 | } |
| 275 | for range in [false, true] { |
| 276 | start := time.now() |
| 277 | if _, bytes := timed_get(url, range) { |
| 278 | assert false, 'a trickled answer of ${bytes} bytes passed the timeout (range ${range})' |
| 279 | } |
| 280 | took := time.now() - start |
| 281 | assert took < 3 * time.second, 'the probe took ${took} (range ${range})' |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // a mirror that answers a byte, then falls silent past the deadline gives the |
| 286 | // probe neither a progress callback to check the clock with nor a socket |
| 287 | // timeout to end the wait — each byte lands just inside it. the deadline must |
| 288 | // be held by the wait on the answer itself, so the probe ends at the deadline |
| 289 | // and not when the next chunk finally arrives (some 1.8s here) |
| 290 | fn test_timed_get_holds_the_deadline_when_no_chunk_arrives() ! { |
| 291 | saved := rank_timeout |
| 292 | defer { |
| 293 | rank_timeout = saved |
| 294 | } |
| 295 | rank_timeout = 1 |
| 296 | mut l, url := trickle_url(30, 900 * time.millisecond)! |
| 297 | defer { |
| 298 | l.close() or {} |
| 299 | } |
| 300 | for range in [false, true] { |
| 301 | start := time.now() |
| 302 | if _, bytes := timed_get(url, range) { |
| 303 | assert false, 'a silent mirror passed the timeout with ${bytes} bytes (range ${range})' |
| 304 | } |
| 305 | took := time.now() - start |
| 306 | assert took < 1500 * time.millisecond, 'the probe took ${took} (range ${range})' |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // an answer that is whole within the timeout is measured as before |
| 311 | fn test_timed_get_measures_a_prompt_answer() ! { |
| 312 | saved := rank_timeout |
| 313 | defer { |
| 314 | rank_timeout = saved |
| 315 | } |
| 316 | rank_timeout = 5 |
| 317 | mut l, url := trickle_url(4, time.millisecond)! |
| 318 | defer { |
| 319 | l.close() or {} |
| 320 | } |
| 321 | _, bytes := timed_get(url, false)! |
| 322 | assert bytes == 4 |
| 323 | } |
| 324 | |
| 325 | fn test_rank_os_names_are_sorted() { |
| 326 | // the message spells the oses the same way whatever order the config's |
| 327 | // mirrorlist forms defined them in; one os and none are spelled too |
| 328 | saved := rank_oses.clone() |
| 329 | defer { rank_oses = saved.clone() } |
| 330 | rank_oses = map[string]RankOs{} |
| 331 | assert rank_os_names() == '' |
| 332 | rank_oses['zeta'] = RankOs{} |
| 333 | assert rank_os_names() == 'zeta' |
| 334 | rank_oses['arch'] = RankOs{} |
| 335 | rank_oses['Mid'] = RankOs{} |
| 336 | rank_oses['artix'] = RankOs{} |
| 337 | assert rank_os_names() == 'Mid, arch, artix, zeta' |
| 338 | } |
| 339 | |
| 340 | fn test_timed_probe_edges() { |
| 341 | // latency alone: the bytes are not looked at |
| 342 | lat := timed_probe('u', 12, 0, false) |
| 343 | assert lat.ok && lat.latency_ms == 12 && lat.speed == 0.0 && lat.url == 'u' |
| 344 | // a zero latency is still a mirror that answered |
| 345 | assert timed_probe('u', 0, 0, false).ok |
| 346 | // speed: bytes over seconds |
| 347 | sp := timed_probe('u', 500, 1000, true) |
| 348 | assert sp.ok && sp.speed == 2000.0 && sp.latency_ms == 500 |
| 349 | // a fetch that brought nothing failed, whatever it took |
| 350 | assert !timed_probe('u', 500, 0, true).ok |
| 351 | assert !timed_probe('u', 0, 0, true).ok |
| 352 | // quicker than a millisecond, or a clock that went back, is one |
| 353 | // millisecond: a speed, not a division by zero |
| 354 | assert timed_probe('u', 0, 10, true).speed == 10000.0 |
| 355 | assert timed_probe('u', -5, 10, true).speed == 10000.0 |
| 356 | } |
| 357 | |
| 358 | fn test_http_text_names_the_refusal() ! { |
| 359 | // nothing listens here: the fetch fails as the fetch it was called |
| 360 | if body := http_text('http://127.0.0.1:1/x', 1, 'fetching the test', 'the test server') { |
| 361 | assert false, 'port 1 answered: ${body}' |
| 362 | } else { |
| 363 | assert err.msg().starts_with('fetching the test failed: '), err.msg() |
| 364 | } |
| 365 | } |
| 366 | |