vtest "Test a dynamic backend discard during a request"

# vcl.discard testing inspired by v00006.vtc from commit e1f7207

barrier b1 cond 2
barrier b2 cond 2

server s1 {
	rxreq
	expect req.url == "/foo"
	barrier b1 sync
	barrier b2 sync
	txresp
} -start

vinyl v1 -arg "-p thread_pools=1" -vcl {
	import debug;

	backend dummy { .host = "${bad_backend}"; }

	sub vcl_init {
		new s1 = debug.dyn("${s1_addr}", "${s1_port}");
	}

	sub vcl_recv {
		set req.backend_hint = s1.backend();
	}
} -start

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

vinyl v1 -expect MAIN.n_backend == 2
# expected: vcl1.dummy, vcl1.s1

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

barrier b1 sync

vinyl v1 -vcl {
	import debug;

	backend dummy { .host = "${bad_backend}"; }

	sub vcl_init {
		new s2 = debug.dyn("${s2_addr}", "${s2_port}");
	}

	sub vcl_recv {
		set req.backend_hint = s2.backend();
	}
}

vinyl v1 -cli "vcl.discard vcl1"
barrier b2 sync

client c1 -wait
delay 2

vinyl v1 -expect MAIN.n_backend == 4
# expected: vcl1.dummy, vcl1.s1, vcl2.dummy, vcl2.s2

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

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

vinyl v1 -cli "vcl.list"
#vinyl v1 -expect MAIN.n_backend == 2
# expected: vcl2.dummy, vcl2.s2
vinyl v1 -expect n_vcl_avail == 1

# XXX This test fails on FreeBSD
# XXX vinyl v1 -expect n_vcl_discard == 0
