I am working on an Ant target for running tests, and I need to have an app server up and running before the tests start. I'm using the waitFor
task to ping a URL until it gets a response. This works, except that I need to not only make sure the URL is simply accessible, but also check that a particular status string is present. This second requirement is simple on its own, with a get
or telnet
task, followed by a substring search. But the 'waitfor' task doesn't accept any nested elements other than standard Ant conditions, or things they can wrap (things that return booleans). I have tried loading the URL as a resource prior to running the 'waitfor' task with a resourceconatins
condition, but that caches the response, and doesn't refresh from the server.
Is there a way to force the URL to be loaded on each loop? Or some other way to achieve this?
Here's what I want to achieve in pseudo-code:
<waitfor>
<and>
<!-- make sure the URL is accessible at all -->
<http url="theurl.jsp"/>
<!-- load the URL as a resource and make sure it contains a substring -->
<!-- moving the "url" task before the waitfor works if all conditions are met the first time through, but does not refresh the URL if it has to loop over again. having it inside like this, fails because waitfor/and don't support nexted "url" tasks -->
<url id="url.resource" url="url.jsp"/>
<resourcecontains refid="url.resource" substring="status"/>
</and>
<!-- else, keep on trying until timeout -->
</waitfor>
<!-- continue on to the tests to be run -->
You can achieve this using a target called from a
scriptcondition
element in thewaitfor
. Here's an example.To use it, you need to set
theurl
and thecontent.matches
properties to be the resource in question, and the string to match respectively. Adjust the 'maxwait' settings as required.Property
theurl.fail
is set on timeout.This takes advantage of the Ant
local
task that was new in v1.8. Thelr
target fetches the URL and looks for the match, if found the propertytheurl.matches
is set. Thescriptcondition
executes the target, embedded within thewaitfor
, until success or timeout.I tested this against the US Naval Observatory Master Clock Time and various short time strings, seemed to work nicely.
Usage:
Code below allows for this: