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