@param maximumWaitSeconds the maximum time to wait before considering it failed.
*/
public static void executeBlocking(GradlePluginLord gradlePluginLord, String fullCommandLine, String displayName, final ExecuteGradleCommandServerProtocol.ExecutionInteraction executionInteraction, int maximumWaitSeconds) {
gradlePluginLord.startExecutionQueue(); //make sure its started
final AtomicBoolean isComplete = new AtomicBoolean();
GradlePluginLord.RequestObserver observer = new GradlePluginLord.RequestObserver() {
public void executionRequestAdded( ExecutionRequest request )
{
request.setExecutionInteraction( executionInteraction );
}
public void refreshRequestAdded( RefreshTaskListRequest request ) { }
public void aboutToExecuteRequest( Request request ) { }
public void requestExecutionComplete( Request request, int result, String output ) {
isComplete.set(true);
}
};
gradlePluginLord.addRequestObserver( observer, false ); //add the observer before we add the request due to timing issues. It's possible for it to completely execute before we return from addExecutionRequestToQueue.
Request request = gradlePluginLord.addExecutionRequestToQueue( fullCommandLine, displayName );
//make sure we've got a request
Assert.assertNotNull(request);
//now sleep until we're complete, but bail if we wait too long
int totalWaitTime = 0;
while (!isComplete.get() && totalWaitTime <= maximumWaitSeconds) {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
totalWaitTime += 1;
}
gradlePluginLord.removeRequestObserver( observer );
if (!isComplete.get()) //its still running. Something is wrong.
{
request.cancel(); //just to clean up after ourselves a little, cancel the request.
throw new AssertionFailedError("Failed to comlete execution in alotted time: " + maximumWaitSeconds + " seconds. Considering this failed.");
}
}