varnishtest "Hit-for-miss and waitinglist rushing"

# Barrier to make sure that s1 is run first
barrier b1 cond 2

# Barrier to make sure that all requests are on waitinglist before
# HSH_Unbusy is called
barrier b2 cond 2

# Barrier to control that all backends are reached before any request
# finishes. This tests that waitinglists are rushed before
# HSH_DerefObjCore().
barrier b3 cond 6

server s1 {
	rxreq
	barrier b1 sync
	barrier b2 sync
	txresp -nolen -hdr "Transfer-Encoding: chunked"
	chunkedlen 10
	barrier b3 sync
	chunkedlen 10
	chunkedlen 0
} -start

server s2 {
	rxreq
	txresp -nolen -hdr "Transfer-Encoding: chunked"
	chunkedlen 10
	barrier b3 sync
	chunkedlen 10
	chunkedlen 0
} -start

server s3 {
	rxreq
	txresp -nolen -hdr "Transfer-Encoding: chunked"
	chunkedlen 10
	barrier b3 sync
	chunkedlen 10
	chunkedlen 0
} -start

server s4 {
	rxreq
	txresp -nolen -hdr "Transfer-Encoding: chunked"
	chunkedlen 10
	barrier b3 sync
	chunkedlen 10
	chunkedlen 0
} -start

server s5 {
	rxreq
	txresp -nolen -hdr "Transfer-Encoding: chunked"
	chunkedlen 10
	barrier b3 sync
	chunkedlen 10
	chunkedlen 0
} -start

server s6 {
	rxreq
	txresp -nolen -hdr "Transfer-Encoding: chunked"
	chunkedlen 10
	barrier b3 sync
	chunkedlen 10
	chunkedlen 0
} -start

varnish v1 -arg "-p thread_pools=1" -arg "-p thread_pool_min=30" -arg "-p rush_exponent=2" -arg "-p debug=+syncvsl" -arg "-p debug=+waitinglist" -vcl+backend {
	sub vcl_backend_fetch {
		if (bereq.http.client == "1") {
			set bereq.backend = s1;
		} else if (bereq.http.client == "2") {
			set bereq.backend = s2;
		} else if (bereq.http.client == "3") {
			set bereq.backend = s3;
		} else if (bereq.http.client == "4") {
			set bereq.backend = s4;
		} else if (bereq.http.client == "5") {
			set bereq.backend = s5;
		} else if (bereq.http.client == "6") {
			set bereq.backend = s6;
		}
	}
	sub vcl_backend_response {
		set beresp.uncacheable = true;
	}
} -start

client c1 {
	txreq -url /hfm -hdr "Client: 1"
	rxresp
} -start

# This makes sure that c1->s1 is done first
barrier b1 sync

client c2 {
	txreq -url /hfm -hdr "Client: 2"
	rxresp
} -start

client c3 {
	txreq -url /hfm -hdr "Client: 3"
	rxresp
} -start

client c4 {
	txreq -url /hfm -hdr "Client: 4"
	rxresp
} -start

client c5 {
	txreq -url /hfm -hdr "Client: 5"
	rxresp
} -start

client c6 {
	txreq -url /hfm -hdr "Client: 6"
	rxresp
} -start

# Wait until c2-c6 are on the waitinglist
delay 1
varnish v1 -expect busy_sleep == 5

# Open up the response headers from s1, and as a result HSH_Unbusy
barrier b2 sync

client c1 -wait
client c2 -wait
client c3 -wait
client c4 -wait
client c5 -wait
client c6 -wait
