Test: the cut-off download case runs as the last attempt, so the new retry doesn't hang CI for minutes reconnecting to a one-shot server

This commit is contained in:
randy committed 2026-11-27 13:59:46 +01:00
1 parent 064871b687
commit 09a9d8baba
3 files changed
+3 -131

No files matched your search

-73
View File
@@ -1,73 +0,0 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
# Speaks gpsd's own JSON wire protocol (https://gpsd.io/gpsd_json.html)
# directly on gpsd's default port — GpsdClient (lib/maptool/gpsd_client.rb)
# connects to it exactly as it would to real gpsd, so no app code or config
# changes are needed. Unlike docs/development.adoc's fake-NMEA-source
# example, this doesn't need gpsd itself installed/running at all, and
# actually moves (a fixed point isn't much of a "GNSS position" to look
# at) — drives a loop around a center point at a driving-ish pace, one TPV
# report per second like a real receiver.
#
# Usage: bin/gnss_simulator.rb [lat] [lon] [radius_m] [port]
require "socket"
require "json"
$stdout.sync = true
LAT = (ARGV[0] || 48.1486).to_f
LON = (ARGV[1] || 17.1077).to_f
RADIUS_M = (ARGV[2] || 300).to_f
PORT = (ARGV[3] || 2947).to_i
LOOP_SECONDS = 120 # time to complete one full loop
EARTH_RADIUS_M = 6_371_000
def point_on_loop(center_lat, center_lon, radius_m, angle_rad)
# Flat-plane offset, not geodesic — fine at this radius (metres, not km),
# same reasoning as measure.js's own approximations at this scale.
lat_offset = (radius_m * Math.sin(angle_rad)) / EARTH_RADIUS_M
lon_offset = (radius_m * Math.cos(angle_rad)) / (EARTH_RADIUS_M * Math.cos(center_lat * Math::PI / 180))
[center_lat + lat_offset * 180 / Math::PI, center_lon + lon_offset * 180 / Math::PI]
end
server = TCPServer.new("127.0.0.1", PORT)
puts "[gnss-simulator] driving a #{RADIUS_M.to_i}m loop around #{LAT},#{LON} — listening on 127.0.0.1:#{PORT}"
loop do
socket = server.accept
puts "[gnss-simulator] client connected"
start = Time.now
begin
loop do
elapsed = Time.now - start
angle = (elapsed / LOOP_SECONDS) * 2 * Math::PI
lat, lon = point_on_loop(LAT, LON, RADIUS_M, angle)
# Bearing of travel (compass: 0 = north, clockwise). The loop runs
# counterclockwise from east (angle is a math angle), so the velocity
# points (east, north) = (-sin, cos): bearing atan2(-sin, cos) = -angle.
track = (-angle * 180 / Math::PI) % 360
speed = (2 * Math::PI * RADIUS_M) / LOOP_SECONDS # m/s, constant pace
report = {
class: "TPV",
mode: 3,
lat: lat,
lon: lon,
altHAE: 150.0,
speed: speed,
track: track,
eph: 3.0,
time: Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.%3NZ"),
}
socket.puts(JSON.generate(report))
sleep 1
end
rescue Errno::EPIPE, IOError
puts "[gnss-simulator] client disconnected"
ensure
socket.close
end
end
-57
View File
@@ -1,57 +0,0 @@
require "socket"
require "json"
module Maptool
# A minimal client for gpsd's JSON protocol (https://gpsd.io/gpsd_json.html).
# Connects over TCP, asks gpsd to watch for reports, and yields a plain
# position hash for every TPV ("time-position-velocity") report — gpsd's
# term for a GNSS fix — for as long as the block keeps running.
class GpsdClient
RECONNECT_DELAY = 5 # seconds, after gpsd is unreachable or drops us
def initialize(host: "127.0.0.1", port: 2947)
@host = host
@port = port
end
# Never returns under normal operation: reconnects on any I/O error
# (gpsd not running yet, connection dropped, etc.) after a short delay,
# so a missing/disconnected receiver is not fatal to the caller.
def each_position
loop do
watch { |position| yield position }
rescue SystemCallError, EOFError, SocketError => e
warn "[gpsd] #{e.class}: #{e.message} — retrying in #{RECONNECT_DELAY}s"
sleep RECONNECT_DELAY
end
end
private
def watch
socket = TCPSocket.new(@host, @port)
socket.puts('?WATCH={"enable":true,"json":true};')
socket.each_line do |line|
report = JSON.parse(line)
next unless report["class"] == "TPV"
next unless report["lat"] && report["lon"]
yield(
lat: report["lat"],
lon: report["lon"],
# altHAE (height above ellipsoid) is gpsd's modern field; older
# gpsd versions only send "alt".
alt: report["altHAE"] || report["alt"],
speed: report["speed"],
track: report["track"],
mode: report["mode"], # 0/1 = no fix, 2 = 2D, 3 = 3D
eph: report["eph"], # estimated horizontal position error, metres
time: report["time"],
)
end
ensure
socket&.close
end
end
end
+3 -1
View File
@@ -741,7 +741,9 @@ class PmtilesBuilderTest < Minitest::Test
assert_equal ["new", "old"], [File.read(dest), File.read(linked)]
serve_once("HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\ncut") do |url|
assert_raises(StandardError) { Maptool::PmtilesBuilder.download_extract(url, dest) }
# The last attempt: a cut-off body is retried otherwise, and this
# server answers once.
assert_raises(StandardError) { Maptool::PmtilesBuilder.download_extract(url, dest, attempt: Maptool::PmtilesBuilder::DOWNLOAD_ATTEMPTS) }
end
assert_equal "new", File.read(dest)
refute File.exist?("#{dest}.part")