varnishtest "Test workspace functions in vmod_vtc"

server s1 {
	rxreq
	txresp

	rxreq
	txresp
} -start

varnish v1 -vcl+backend {
	import vtc;
	sub vcl_recv {
		if (req.url == "/overflow") {
			vtc.workspace_alloc(client, 100000000);
		}
	}
	sub vcl_backend_response {
		set beresp.http.free_backend = vtc.workspace_free(backend);
	}

	sub vcl_deliver {
		set resp.http.free_session = vtc.workspace_free(session);
		set resp.http.free_thread = vtc.workspace_free(thread);

		set resp.http.overflowed = vtc.workspace_overflowed(client);
		vtc.workspace_alloc(client, 2048);

		if (req.url == "/bar") {
			vtc.workspace_overflow(client);
		}
	}
} -start

logexpect l1 -v v1 -d 1 -g vxid -q "Error ~ 'overflow'" {
	expect 0 *	Begin
	expect * =	Error		"workspace_client overflow"
	expect * =	End
} -start

client c1 {
	txreq -url /foo
	rxresp
	expect resp.http.overflowed == "false"

	expect resp.http.free_backend > 9000
	expect resp.http.free_session >= 200
	expect resp.http.free_thread > 2000
} -run

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

logexpect l1 -wait

logexpect l2 -v v1 -d 1 -g vxid  {
	expect * 1007	VCL_Error
} -start

client c1 {
	txreq -url /overflow
	rxresp
	expect resp.status == 503
} -run

logexpect l2 -wait

varnish v1 -expect client_resp_500 == 1
varnish v1 -expect ws_client_overflow == 2
