vtest "Test dynamic UDS backend hot swap after it was picked by a bereq"

barrier b1 sock 2

server s1 -listen "${tmpdir}/s1.sock" {
} -start

server s2 -listen "${tmpdir}/s2.sock" {
	rxreq
	txresp
} -start

vinyl v1 -vcl {
	import std;
	import debug;
	import vtc;

	backend dummy None;

	sub vcl_init {
		new s1 = debug.dyn_uds("${s1_sock}");
	}

	sub vcl_recv {
		if (req.method == "SWAP") {
			s1.refresh(req.http.X-Path);
			return (synth(200));
		}
	}

	sub vcl_backend_fetch {
		set bereq.backend = s1.backend();
		# hot swap has happened
		vtc.barrier_sync("${b1_sock}");
		if (std.healthy(bereq.backend)) {
			return(abandon);
		} else {
			set bereq.backend = s1.backend();
		}
	}
} -start

vinyl v1 -expect MAIN.n_backend == 1

client c1 {
	txreq
	rxresp
	expect resp.status == 200
}

client c2 {
	delay 0.1
	txreq -req "SWAP" -hdr "X-Path: ${s2_sock}"
	rxresp
	expect resp.status == 200
	barrier b1 sync
}

client c1 -start
client c2 -run
client c1 -wait
