varnishtest "VMOD purge"

server s1 -repeat 12 {
	rxreq
	txresp
} -start

varnish v1 -arg "-p thread_pools=1" -vcl+backend {
	import purge;
	import vtc;

	sub vcl_recv {
		if (req.url == "recv") { purge.hard(); }
		if (req.url == "pass") { return (pass); }
		if (req.url == "purge") { return (purge); }
		if (req.url == "synth") { return (synth(200)); }
	}
	sub vcl_hash {
		if (req.url == "hash") { purge.hard(); }
	}
	sub vcl_miss {
		if (req.url == "miss") { purge.hard(); }
	}
	sub vcl_hit {
		if (req.url == "hit") { purge.hard(); }
	}
	sub vcl_purge {
		if (req.url == "purge") { purge.hard(); }
	}
	sub vcl_pass {
		if (req.url == "pass") { purge.hard(); }
	}
	sub vcl_deliver {
		if (req.url == "deliver") { purge.hard(); }
	}
	sub vcl_synth {
		if (req.url == "synth") { purge.hard(); }
	}
	sub vcl_backend_fetch {
		if (bereq.url == "fetch") { purge.hard(); }
		if (bereq.url == "error") {
			set bereq.backend = vtc.no_backend();
		}
	}
	sub vcl_backend_error {
		if (bereq.url == "error") { purge.hard(); }
	}
	sub vcl_backend_response {
		if (bereq.url == "response") { purge.hard(); }
	}
} -start

varnish v1 -cliok "param.set timeout_idle 2"

logexpect l0 -v v1 -g raw {
	expect * 0 ExpKill	"EXP_Expired x=1002"
} -start

logexpect l2 -v v1 -g raw {
	expect * 1002	Begin		"bereq 1001 fetch"
} -start

logexpect l1 -v v1 {

	expect * 1003	VCL_call	HIT
	expect * =	VCL_call	DELIVER

	expect * 1004	VCL_call	MISS
	expect * =	VCL_call	DELIVER

	expect * 1007	VCL_call	RECV
	expect * =	VCL_Error	purge
	expect * =	VCL_return	fail

	expect * 1009	VCL_call	HASH
	expect * =	VCL_Error	purge
	expect * =	VCL_return	fail

	expect * 1011	VCL_call	PURGE
	expect * =	VCL_Error	purge

	expect * 1013	VCL_call	PASS
	expect * =	VCL_Error	purge

	expect * 1015	VCL_call	DELIVER
	expect * =	VCL_Error	purge

	expect * 1018	VCL_call	SYNTH
	expect * =	VCL_Error	purge

	expect * 1021	VCL_call	BACKEND_FETCH
	expect * =	VCL_Error	purge

	expect * 1024	VCL_call	BACKEND_ERROR
	expect * =	VCL_Error	purge

	expect * 1027	VCL_call	BACKEND_RESPONSE
	expect * =	VCL_Error	purge
} -start

client c1 {
	txreq -url hit
	rxresp
	expect resp.status == 200

	txreq -url hit
	rxresp
	expect resp.status == 200

	txreq -url miss
	rxresp
	expect resp.status == 200
} -run

logexpect l0 -wait
logexpect l2 -wait

client c1 {
	txreq -url recv
	rxresp
	expect resp.status == 503
	expect_close
} -run

client c1 {
	txreq -url hash
	rxresp
	expect resp.status == 503
	expect_close
} -run

client c1 {
	txreq -url purge
	rxresp
	expect resp.status == 503
	expect_close
} -run

client c1 {
	txreq -url pass
	rxresp
	expect resp.status == 503
	expect_close
} -run

client c1 {
	txreq -url deliver
	rxresp
	expect resp.status == 503
	expect_close
} -run

client c1 {
	txreq -url synth
	expect_close
} -run

client c1 {
	txreq -url fetch
	rxresp
	expect resp.status == 503
	expect_close
} -run

client c1 {
	txreq -url error
	rxresp
	expect resp.status == 503
	expect_close
} -run

client c1 {
	txreq -url response
	rxresp
	expect resp.status == 503
	expect_close
} -run

logexpect l1 -wait
