vtest "Test random director"

server s1 {
	rxreq
	txresp -body "foo1"
	rxreq
	txresp -body "bar1"
} -start

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

	sub vcl_init {
		new foo = directors.random();
		foo.add_backend(s1, 1);
		foo.add_backend(s1, 1);
	}

	sub vcl_backend_fetch {
		set bereq.backend = foo.backend();
	}

	sub vcl_backend_response {
		set beresp.http.where = bereq.backend + "-->" + beresp.backend;
		set beresp.http.health = std.healthy(foo.backend());
	}
} -start

client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.http.where == "foo-->s1"
	txreq -url "/bar"
	rxresp
	expect resp.http.where == "foo-->s1"
} -run

server s1 -start
server s2 {
	loop 20 {
		rxreq
		txresp
	}
} -start

vinyl v1 -vcl+backend {
	import directors;

	sub vcl_init {
		new foo = directors.random();
		foo.add_backend(s1, 1);
		foo.add_backend(s2, 1);
	}

	sub vcl_recv {
		if (req.method == "DELETE") {
			foo.remove_backend(s1);
			return(synth(204));
		}
		set req.backend_hint = foo.backend();
		return (pass);
	}

	sub vcl_backend_response {
		set beresp.http.where = bereq.backend + "-->" + beresp.backend;
	}
}

client c1 {
	txreq -req "DELETE"
	rxresp
	expect resp.status == 204
	loop 20 {
		txreq
		rxresp
		expect resp.status == 200
		expect resp.http.where == "foo-->s2"
	}
} -run
