vtest "Test round robin director"

server s1 {
	rxreq
	txresp -body "1"
} -start

server s2 {
	rxreq
	txresp -body "22"
} -start

server s3 {
	rxreq
	txresp -body "333"
} -start

server s4 {
	rxreq
	txresp -body "4444"
} -start

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

	sub vcl_init {
		new rr = directors.round_robin();
		rr.add_backend(s1);
		rr.add_backend(s2);
		rr.add_backend(s3);
		rr.add_backend(s4);
	}

	sub vcl_recv {
		if (req.method == "DELETE") {
			rr.remove_backend(s1);
			rr.remove_backend(s2);
			rr.remove_backend(s3);
			return(synth(204));
		}
	}

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

	sub vcl_backend_fetch {
		set bereq.backend = rr.backend();
	}
} -start

client c1 {
	timeout 3
	txreq -url "/foo1"
	rxresp
	expect resp.bodylen == 1
	expect resp.http.healthy == "true"
	txreq -url "/foo2"
	rxresp
	expect resp.bodylen == 2
	expect resp.http.healthy == "true"
	txreq -url "/foo3"
	rxresp
	expect resp.bodylen == 3
	expect resp.http.healthy == "true"
	txreq -url "/foo4"
	rxresp
	expect resp.bodylen == 4
	expect resp.http.healthy == "true"
} -run

server s1 -start
server s2 -start

client c2 {
	timeout 3
	txreq -url "/foo11"
	rxresp
	expect resp.bodylen == 1
	expect resp.http.healthy == "true"
	txreq -url "/foo22"
	rxresp
	expect resp.bodylen == 2
	expect resp.http.healthy == "true"
} -run

server s4 -start

client c3 {
	txreq -req "DELETE"
	rxresp
	expect resp.status == 204
	txreq -url "/foo31"
	rxresp
	expect resp.bodylen == 4
	expect resp.http.healthy == "true"
} -run
