varnishtest "TCP pool teardown with open connection"

feature ipv6

# when c1's request has gotten to s1
barrier b1 cond 3

# when vcls have been swizzled
barrier b2 cond 3

server s1 -listen "::1" {
	rxreq
	expect req.url == "/s1"
	barrier b1 sync
	barrier b2 sync
	txresp -hdr "Host: s1"
	expect_close
} -start

varnish v1 -cliok "param.set debug +vclrel"

varnish v1 -vcl+backend {} -start

client c1 {
	txreq -url /s1
	barrier b1 sync
	barrier b2 sync
	rxresp
	expect resp.http.host == "s1"

	txreq -url /s2
	rxresp
	expect resp.http.host == "s2"
} -start

barrier b1 sync

varnish v1 -cliok "param.set debug +vclrel"

server s2 -listen "::1" {
	rxreq
	expect req.url == "/s2"
	txresp -hdr "Host: s2"
} -start

varnish v1 -vcl {
	backend b2 {
		.host = "${s2_sock}";
	}
}

varnish v1 -vsc LCK.conn_pool.*
varnish v1 -cliok "vcl.discard vcl1"
varnish v1 -cliok "vcl.list"
varnish v1 -vsc LCK.conn_pool.*
varnish v1 -expect LCK.conn_pool.destroy == 0

barrier b2 sync

client c1 -wait
varnish v1 -cliok "vcl.list"
server s1 -wait
varnish v1 -vsc LCK.conn_pool.*
varnish v1 -expect LCK.conn_pool.destroy == 1
