Serial fetches (no connection reuse) measured ~650ms/tile, almost all TCP+TLS handshake — a real bbox at z0..15 is tens of thousands of tiles, so this was hours for a modest region. Now: 16 worker threads share a queue (I/O-bound, so the GVL doesn't get in the way), each keeping its own persistent connection across every tile it fetches. Measured live against the real Terrarium source: ~40ms/tile, ~16x faster. write_terrarium_mbtiles's SQLite writes and progress updates stay on one thread (draining a results Queue the workers feed), so nothing needed its own locking beyond the two Queues themselves.
165 lines
6.4 KiB
Ruby
165 lines
6.4 KiB
Ruby
require_relative "test_helper"
|
|
require "maptool/pmtiles_builder"
|
|
require "tmpdir"
|
|
require "fileutils"
|
|
|
|
# Network calls (the protomaps build index, individual Terrarium tile
|
|
# fetches) are never exercised here — same "keep this offline/deterministic"
|
|
# reasoning test_helper.rb already gives for /api/plugins staying off the
|
|
# real Overpass API — build_offline/build_terrain take fetch_build_url/
|
|
# fetch_tile precisely so tests can swap those out. A fake `pmtiles` CLI
|
|
# (PMTILES_BIN) stands in for the real go-pmtiles binary.
|
|
class PmtilesBuilderTest < Minitest::Test
|
|
def setup
|
|
@data_dir = Dir.mktmpdir("maptool-pmtiles-test-data")
|
|
@bin_dir = Dir.mktmpdir("maptool-pmtiles-test-bin")
|
|
|
|
# $3 is the output path either way: `extract SOURCE OUTPUT --bbox=...
|
|
# --maxzoom=...` and `convert INPUT OUTPUT` both put it 3rd, even though
|
|
# it's not the *last* arg in the extract case (the --flags follow it).
|
|
@pmtiles_ok = File.join(@bin_dir, "pmtiles-ok")
|
|
File.write(@pmtiles_ok, "#!/bin/sh\necho fake > \"$3\"\n")
|
|
File.chmod(0o755, @pmtiles_ok)
|
|
|
|
@pmtiles_fail = File.join(@bin_dir, "pmtiles-fail")
|
|
File.write(@pmtiles_fail, "#!/bin/sh\necho 'boom' >&2\nexit 1\n")
|
|
File.chmod(0o755, @pmtiles_fail)
|
|
|
|
@original_pmtiles_bin = ENV["PMTILES_BIN"]
|
|
Maptool::PmtilesBuilder.reset!
|
|
end
|
|
|
|
def teardown
|
|
FileUtils.remove_entry(@data_dir)
|
|
FileUtils.remove_entry(@bin_dir)
|
|
ENV["PMTILES_BIN"] = @original_pmtiles_bin
|
|
Maptool::PmtilesBuilder.reset!
|
|
end
|
|
|
|
def test_offline_and_terrain_status_start_idle
|
|
assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.offline_status)
|
|
assert_equal({ state: "idle" }, Maptool::PmtilesBuilder.terrain_status)
|
|
end
|
|
|
|
def test_build_offline_writes_the_file_and_reports_done
|
|
ENV["PMTILES_BIN"] = @pmtiles_ok
|
|
thread = Maptool::PmtilesBuilder.build_offline(
|
|
[1, 2, 3, 4],
|
|
data_dir: @data_dir,
|
|
fetch_build_url: -> { "https://example.com/fake.pmtiles" },
|
|
)
|
|
thread.join
|
|
|
|
assert_path_exists File.join(@data_dir, "offline.pmtiles")
|
|
refute_path_exists File.join(@data_dir, "offline.pmtiles.tmp")
|
|
assert_equal "done", Maptool::PmtilesBuilder.offline_status[:state]
|
|
end
|
|
|
|
def test_build_offline_reports_an_error_status_on_non_zero_exit
|
|
ENV["PMTILES_BIN"] = @pmtiles_fail
|
|
thread = Maptool::PmtilesBuilder.build_offline(
|
|
[1, 2, 3, 4],
|
|
data_dir: @data_dir,
|
|
fetch_build_url: -> { "https://example.com/fake.pmtiles" },
|
|
)
|
|
thread.join
|
|
|
|
refute_path_exists File.join(@data_dir, "offline.pmtiles")
|
|
status = Maptool::PmtilesBuilder.offline_status
|
|
assert_equal "error", status[:state]
|
|
assert_match(/exited with status/, status[:message])
|
|
end
|
|
|
|
def test_build_offline_reports_a_clear_error_when_the_binary_is_missing
|
|
ENV["PMTILES_BIN"] = File.join(@bin_dir, "does-not-exist")
|
|
thread = Maptool::PmtilesBuilder.build_offline(
|
|
[1, 2, 3, 4],
|
|
data_dir: @data_dir,
|
|
fetch_build_url: -> { "https://example.com/fake.pmtiles" },
|
|
)
|
|
thread.join
|
|
|
|
status = Maptool::PmtilesBuilder.offline_status
|
|
assert_equal "error", status[:state]
|
|
assert_match(/go-pmtiles installed/, status[:message])
|
|
end
|
|
|
|
# fetch_build_url blocks on a Queue so the build is deterministically
|
|
# still "running" when the second call happens, rather than racing a real
|
|
# one to see which state wins.
|
|
def test_build_offline_raises_when_one_is_already_running
|
|
ENV["PMTILES_BIN"] = @pmtiles_ok
|
|
gate = Queue.new
|
|
thread = Maptool::PmtilesBuilder.build_offline([1, 2, 3, 4], data_dir: @data_dir, fetch_build_url: -> { gate.pop })
|
|
|
|
assert_raises(Maptool::PmtilesBuilder::BuildError) do
|
|
Maptool::PmtilesBuilder.build_offline([1, 2, 3, 4], data_dir: @data_dir, fetch_build_url: -> { "unused" })
|
|
end
|
|
ensure
|
|
gate.push("https://example.com/fake.pmtiles")
|
|
thread&.join
|
|
end
|
|
|
|
# A tiny real-world bbox (a few blocks of central Bratislava) keeps the
|
|
# z0..15 tile_range small at every zoom level, so this stays fast even
|
|
# though it iterates all 16 levels.
|
|
def test_build_terrain_packs_fetched_tiles_and_converts
|
|
ENV["PMTILES_BIN"] = @pmtiles_ok
|
|
thread = Maptool::PmtilesBuilder.build_terrain(
|
|
[48.140, 17.100, 48.141, 17.101],
|
|
data_dir: @data_dir,
|
|
fetch_tile: ->(_z, _x, _y) { "not a real png, write_terrarium_mbtiles just stores opaque bytes" },
|
|
)
|
|
thread.join
|
|
|
|
assert_path_exists File.join(@data_dir, "terrain.pmtiles")
|
|
status = Maptool::PmtilesBuilder.terrain_status
|
|
assert_equal "done", status[:state]
|
|
assert_operator status[:tiles_total], :>, 0
|
|
assert_equal status[:tiles_total], status[:tiles_done]
|
|
end
|
|
|
|
# Every fetch_tile call blocks on a gate until explicitly told not to —
|
|
# with CONCURRENT_TILE_FETCHES workers pulling from a shared queue, that
|
|
# holds the whole pool at "in flight, none finished yet" deterministically
|
|
# (a big enough bbox that z0..15 yields far more tiles than worker slots,
|
|
# so most of the job is still queued up behind the blocked pool).
|
|
def test_build_terrain_reports_progress_while_running
|
|
ENV["PMTILES_BIN"] = @pmtiles_ok
|
|
concurrency = Maptool::PmtilesBuilder::CONCURRENT_TILE_FETCHES
|
|
gate = Queue.new
|
|
unblocked = false
|
|
fetch_tile = lambda do |_z, _x, _y|
|
|
gate.pop unless unblocked
|
|
"bytes"
|
|
end
|
|
thread = Maptool::PmtilesBuilder.build_terrain([47.5, 16.5, 48.5, 17.5], data_dir: @data_dir, fetch_tile: fetch_tile)
|
|
|
|
# Let exactly one batch through (fills every worker's first call) then
|
|
# check before releasing the rest — done should be capped at the pool
|
|
# size, nowhere near the real total.
|
|
concurrency.times { gate.push(nil) }
|
|
sleep 0.05
|
|
status = Maptool::PmtilesBuilder.terrain_status
|
|
assert_equal "running", status[:state]
|
|
assert_operator status[:tiles_total], :>, concurrency
|
|
assert_operator status[:tiles_done], :<, status[:tiles_total]
|
|
ensure
|
|
unblocked = true
|
|
concurrency.times { gate.push(nil) } # release whatever's still blocked in an in-progress gate.pop
|
|
thread&.join
|
|
end
|
|
|
|
def test_build_terrain_still_succeeds_when_some_tiles_are_missing
|
|
ENV["PMTILES_BIN"] = @pmtiles_ok
|
|
thread = Maptool::PmtilesBuilder.build_terrain(
|
|
[48.140, 17.100, 48.141, 17.101],
|
|
data_dir: @data_dir,
|
|
fetch_tile: ->(_z, _x, _y) { nil }, # every tile "missing" — should skip, not fail, the same way HillshadeControl's own source tolerates gaps
|
|
)
|
|
thread.join
|
|
|
|
assert_equal "done", Maptool::PmtilesBuilder.terrain_status[:state]
|
|
end
|
|
end
|