vtest "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

vinyl 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.user-agent == "c1") {
			set bereq.backend = s1;
		} else if (bereq.http.user-agent == "c2") {
			set bereq.backend = s2;
		} else if (bereq.http.user-agent == "c3") {
			set bereq.backend = s3;
		} else if (bereq.http.user-agent == "c4") {
			set bereq.backend = s4;
		} else if (bereq.http.user-agent == "c5") {
			set bereq.backend = s5;
		} else if (bereq.http.user-agent == "c6") {
			set bereq.backend = s6;
		}
	}
	sub vcl_backend_response {
		set beresp.uncacheable = true;
	}
} -start

client c1 {
	txreq
	rxresp
} -start

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

client c2 {
	txreq
	rxresp
} -start

client c3 {
	txreq
	rxresp
} -start

client c4 {
	txreq
	rxresp
} -start

client c5 {
	txreq
	rxresp
} -start

client c6 {
	txreq
	rxresp
} -start

# Wait until c2-c6 are on the waitinglist
vinyl v1 -vsl_catchup
vinyl 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
