#!/usr/bin/env nu
use common.nu *
def main [] {
let tracked_did = "did:plc:qfpnj4og54vl56wngdriaxug"
let port = resolve-test-port 3002
let debug_port = resolve-test-debug-port ($port + 1)
let url = $"http://localhost:($port)"
let debug_url = $"http://localhost:($debug_port)"
let ws_url = $"ws://127.0.0.1:($port)/stream"
let db_path = (mktemp -d -t hydrant_stream_live_backfill.XXXXXX)
print "testing live /stream events while backfill work is pending..."
print $"database path: ($db_path)"
let binary = build-hydrant
let instance = (with-env {
HYDRANT_RELAY_HOSTS: "",
HYDRANT_CRAWLER_URLS: "",
HYDRANT_ENABLE_FIREHOSE: "false",
HYDRANT_ENABLE_CRAWLER: "false",
HYDRANT_ENABLE_BACKFILL: "false",
HYDRANT_PLC_URL: "http://127.0.0.1:1"
} {
start-hydrant $binary $db_path $port
})
if not (wait-for-api $url) {
fail "api failed to start" $instance.pid
}
let live_output = $"($db_path)/stream_live.txt"
let stream_pid = start-ws-listener $ws_url $live_output
sleep 500ms
http put -t application/json $"($url)/repos" [{ did: $tracked_did }]
let pending = (http get $"($url)/stats").counts.pending | into int
if $pending != 1 {
fail $"expected one queued backfill, got ($pending)" $stream_pid $instance.pid
}
let seed = (
http post -H [Content-Length 0] $"($debug_url)/debug/seed_xrpc_repo" ""
)
mut live_events = []
for _ in 1..50 {
if ($live_output | path exists) {
$live_events = (
open --raw $live_output
| into string
| str trim
| lines
| each { |line| try { $line | from json } catch { null } }
| compact
| where { |event| ($event | describe | str starts-with "record") }
)
if ($live_events | is-not-empty) {
break
}
}
sleep 100ms
}
try { kill $stream_pid }
sleep 100ms
if ($live_events | is-empty) {
fail "no live record event received while backfill was pending" $instance.pid
}
let event = $live_events | first
if $event.id != $seed.event_id {
fail $"expected event ($seed.event_id), got ($event.id)" $instance.pid
}
if $event.type != "record" or $event.record.rkey != "stinkpot" {
fail $"unexpected live event: ($event | to json --raw)" $instance.pid
}
try { kill $instance.pid }
print "live /stream event during pending backfill passed!"
}