vtest "vcl.state coverage tests"

server s1 -repeat 20 {
	rxreq
	txresp
	delay .2
} -start

# The debug vmod logs temperature vcl events
vinyl v1 -arg "-p vcl_cooldown=1" \
	   -arg "-p thread_pool_min=5" \
	   -arg "-p thread_pool_max=5" \
	   -vcl {
	import debug;
	backend default {
		.host = "${s1_addr}";
		.port = "${s1_port}";
		.probe = { .interval = 1s; }
	}
	backend noprobe {
		.host = "${s1_addr}";
		.port = "${s1_port}";
	}
	sub vcl_backend_fetch {
		set bereq.backend = noprobe;
	}
} -start

# We only have one vcl yet
vinyl v1 -expect VBE.vcl1.default.happy >= 0
vinyl v1 -expect VBE.vcl1.noprobe.happy > 9223372036854775808
vinyl v1 -expect !VBE.vcl2.default.happy
vinyl v1 -cliok "backend.list -p *.*"

vinyl v1 -vcl {
	backend default {
		.host = "${s1_addr}";
		.port = "${s1_port}";
		.probe = { .interval = 1s; }
	}
}

# Now we have two vcls (and run on the latest loaded)
delay .4
vinyl v1 -expect VBE.vcl1.default.happy >= 0
vinyl v1 -expect VBE.vcl2.default.happy >= 0

# We are about to freeze vcl, then implicitly thaw it via use.
# check that we see the events

logexpect l1 -v v1 -g raw {
	expect * 0 Debug "vcl1: VCL_EVENT_COLD"
	expect * 0 Debug "vcl1: VCL_EVENT_WARM"
} -start

# Freeze vcl1
vinyl v1 -cliok "vcl.state vcl1 cold"
vinyl v1 -cliexpect "available *cold *cold *[0-9]+ *vcl1\\s+active *auto *warm *[0-9]+ *vcl2" "vcl.list"
delay .4
vinyl v1 -expect !VBE.vcl1.default.happy

# Manual temperature control needs to be explicit before use
vinyl v1 -clierr 300 "vcl.use vcl1"

vinyl v1 -cliok "vcl.state vcl1 warm"
vinyl v1 -cliok "vcl.use vcl1"
vinyl v1 -cliexpect "active *warm *warm *[0-9]+ *vcl1\\s+available *auto *warm *[0-9]+ *vcl2" "vcl.list"
delay .4
vinyl v1 -expect VBE.vcl1.default.happy >= 0
vinyl v1 -expect VBE.vcl2.default.happy >= 0

logexpect l1 -wait

# and the unused one should go cold
delay 4
vinyl v1 -cliexpect "active *warm *warm *[0-9]+ *vcl1\\s+available *auto *cold *[0-9]+ *vcl2" "vcl.list"
vinyl v1 -expect !VBE.vcl2.default.happy

# use the other
vinyl v1 -cliok "vcl.use vcl2"
vinyl v1 -cliexpect "available *warm *warm *[0-9]+ *vcl1\\s+active *auto *warm *[0-9]+ *vcl2" "vcl.list"

# the non-auto vcl will stay warm even after the cooldown period
delay 4
vinyl v1 -cliexpect "available *warm *warm *[0-9]+ *vcl1\\s+active *auto *warm *[0-9]+ *vcl2" "vcl.list"
vinyl v1 -expect VBE.vcl1.default.happy >= 0
vinyl v1 -expect VBE.vcl2.default.happy >= 0

# You can't freeze the active VCL
vinyl v1 -clierr 300 "vcl.state vcl2 cold"

# the non-auto vcl will apply the cooldown again once changed back to auto
vinyl v1 -cliok "vcl.state vcl1 auto"
vinyl v1 -cliexpect "available *auto *warm *[0-9]+ *vcl1\\s+active *auto *warm *[0-9]+ *vcl2" "vcl.list"
delay .4
vinyl v1 -expect VBE.vcl1.default.happy >= 0
delay 4
vinyl v1 -cliexpect "available *auto *cold *[0-9]+ *vcl1\\s+active *auto *warm *[0-9]+ *vcl2" "vcl.list"
vinyl v1 -expect !VBE.vcl1.default.happy

# A VMOD's warm-up can fail
vinyl v1 -cliok "param.set max_esi_depth 42"
vinyl v1 -clierr 300 "vcl.state vcl1 warm"

vinyl v1 -cliexpect "available *auto *cold *[0-9]+ *vcl1\\s+active *auto *warm *[0-9]+ *vcl2" "vcl.list"

# A warm-up failure can also fail a child start
vinyl v1 -cliok stop
vinyl v1 -cliok "vcl.state vcl1 warm"
vinyl v1 -cliok "vcl.list"
vinyl v1 -clierr 300 start
