vtest "Test fallback director"

server s1 {
	rxreq
	expect req.http.resolved == "s1"
	txresp -hdr "Foo: 1"
} -start

server s2 {
	rxreq
	expect req.http.resolved == "s2"
	txresp -hdr "Foo: 2"
} -start

server s3 {
	rxreq
	expect req.http.resolved == "s3"
	txresp -hdr "Foo: 3"
} -start

vinyl v1 -vcl+backend {
	import std;
	import directors;

	sub vcl_init {
		new fb1 = directors.fallback();
		fb1.add_backend(s1);
		fb1.add_backend(s2);
		fb1.add_backend(s3);
	}

	sub vcl_recv {
		if (req.method == "DELETE") {
			fb1.remove_backend(s2);
			return(synth(204));
		}
		return (pass);
	}

	sub vcl_deliver {
		set resp.http.health = std.healthy(fb1.backend());
	}

	sub vcl_backend_fetch {
		set bereq.backend = fb1.backend();
		set bereq.http.resolved = bereq.backend.resolve();
	}
} -start

vinyl v1 -cliok "param.set debug +vclrel"

vinyl v1 -cliok "backend.set_health s1 sick"
vinyl v1 -cliok "backend.set_health s2 sick"

client c1 {
	# s1 & s2 are both sick, expect response from s3
	txreq
	rxresp
	expect resp.http.foo == "3"
} -run

vinyl v1 -vsl_catchup

vinyl v1 -cliok "backend.set_health s2 healthy"

client c1 {
	txreq
	rxresp
	expect resp.http.foo == "2"
} -run

vinyl v1 -vsl_catchup

vinyl v1 -cliok "backend.set_health s1 healthy"

client c1 {
	txreq
	rxresp
	expect resp.http.foo == "1"
} -run

vinyl v1 -vsl_catchup

vinyl v1 -cliok "backend.set_health s1 sick"
server s3 -start

client c1 {
	txreq -req "DELETE"
	rxresp
	expect resp.status == 204
	txreq
	rxresp
	expect resp.http.foo == "3"
} -run

vinyl v1 -vsl_catchup

vinyl v1 -vcl+backend {
	sub vcl_recv {
		set req.backend_hint = s1;
		set req.backend_hint = s2;
		set req.backend_hint = s3;
	}
}
vinyl v1 -cliok "vcl.discard vcl1"
vinyl v1 -cliok "vcl.list"
