vtest "Test a dynamic backend hot swap after it was hinted to a req"

barrier b1 cond 2

server s1 {
} -start

server s2 {
	rxreq
	txresp
} -start

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

	backend dummy { .host = "${bad_backend}"; }

	sub vcl_init {
		new s1 = debug.dyn("${s1_addr}", "${s1_port}");
	}

	sub vcl_recv {
		if (req.method == "SWAP") {
			s1.refresh(req.http.X-Addr, req.http.X-Port);
			return (synth(200));
		}
		set req.backend_hint = s1.backend();
		# hot swap should happen while we sleep
		vtc.sleep(2s);
		if (std.healthy(req.backend_hint)) {
			return(synth(800));
		} else {
			set req.backend_hint = s1.backend();
		}
	}
} -start

vinyl v1 -expect MAIN.n_backend == 2

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

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

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

vinyl v1 -cli backend.list
#vinyl v1 -expect MAIN.n_backend == 2
