vtest "Check backend wait limit"

#
# s1, v1, c1, c2 are the success case
# c2 will get the backend connection after c1 is finished with it.
#
barrier b1 cond 2

server s1 {
	rxreq
	barrier b1 sync
	delay 0.2
	txresp

	rxreq
	txresp
} -start

vinyl v1 -vcl {
	backend s1 {
		.host = "${s1_addr}";
		.port = "${s1_port}";
		.max_connections = 1;
		.connect_timeout = 2s;
		.wait_timeout = 1s;    # longer than the s1 'delay 0.2'
		.wait_limit = 1;
	}

	sub vcl_recv {
		return(pass);
	}
} -start

client c1 -connect ${v1_sock} {
	txreq
	rxresp
	expect resp.status == 200
} -start

client c2 -connect ${v1_sock} {
	barrier b1 sync
	txreq
	rxresp
	expect resp.status == 200
} -run

client c1 -wait

vinyl v1 -expect backend_wait == 1
vinyl v1 -expect backend_wait_fail == 0

#
# s2, v2, c3, c4 are the fail case.
# c4 will timeout before it gets the backend connection.
#
barrier b2 cond 2

server s2 {
	rxreq
	barrier b2 sync
	delay 0.2
	txresp

	rxreq
	txresp
} -start

vinyl v2 -vcl {
	backend s2 {
		.host = "${s2_addr}";
		.port = "${s2_port}";
		.max_connections = 1;
		.connect_timeout = 2s;
		.wait_timeout = 100ms;    # shorter than the s2 'delay 0.2'
		.wait_limit = 1;
	}

	sub vcl_recv {
		return(pass);
	}
} -start

client c3 -connect ${v2_sock} {
	txreq
	rxresp
	expect resp.status == 200
} -start

client c4 -connect ${v2_sock} {
	barrier b2 sync
	txreq
	rxresp
	expect resp.status == 503
} -run

client c3 -wait

vinyl v2 -expect backend_wait == 1
vinyl v2 -expect backend_wait_fail == 1
