vtest "VCL: Test backend retirement"
#
# This case is quite sensitive to ordering of the worker threads because
# it has so little actual traffic.  In a real world setting, this should
# not be an issue.
#

# First do one request to get a work-thread that holds a VCL reference

server s1 {
	rxreq
	expect req.url == "/bar"
	txresp
} -start

# Only one pool, to avoid getting more than one work thread
vinyl v1 \
	-arg "-p thread_pools=1" \
	-arg "-p thread_pool_timeout=10" \
	-vcl+backend { } -start

# Give the vinyld a chance to start and create workers, delaying for
# >thread_pool_timeout to allow for any over-bred workers to be kissed
# to death.
# NB: This is important for to avoid mis-ordering of the workers.
delay 11

# But all in all, the delay does not fully prevent over-breeding.
vinyl v1 -expect MAIN.threads >= 10

client c1 {
	txreq -url "/bar"
	rxresp
	expect resp.status == 200
} -run

server s1 -wait

vinyl v1 -expect n_backend == 1
vinyl v1 -expect n_vcl_avail == 1
vinyl v1 -expect n_vcl_discard == 0

# Set up a new VCL and backend

server s2 {
	rxreq
	expect req.url == "/foo"
	txresp
} -start

vinyl v1 -vcl {
	backend b2 {
		.host = "${s2_addr}";
		.port = "${s2_port}";
	}
}

vinyl v1 -vsl_catchup

vinyl v1 -expect n_backend == 2
vinyl v1 -expect n_vcl_avail == 2
vinyl v1 -expect n_vcl_discard == 0

vinyl v1 -cli "vcl.list"

# Discard the first VCL

vinyl v1 -cli "vcl.discard vcl1"

vinyl v1 -vsl_catchup

# Give expiry thread a chance to let go
delay 2

vinyl v1 -vsl_catchup

# It may not go away as long as the workthread holds a VCL reference
vinyl v1 -expect n_backend >= 1
vinyl v1 -expect n_vcl_avail >= 1
vinyl v1 -expect n_vcl_discard >= 0

# Do another request through the new VCL to the new backend
client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
} -run

vinyl v1 -vsl_catchup

server s2 -wait

# The workthread should have released its VCL reference now
# but we need to tickle the CLI to notice

vinyl v1 -cli "vcl.list"

vinyl v1 -expect n_backend == 1
vinyl v1 -expect n_vcl_avail == 1
vinyl v1 -expect n_vcl_discard == 0
