Apply time limit to Gradle JavaExec tasks?

829 views Asked by At

I'm setting up Randoop test case generation to run across my projects. I've achieved this with a Gradle task of type JavaExec:

task RandoopGenerateL1Tests(dependsOn: ['assembleDebug']) {
  group = "Verification"
  description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."

  //Various setup things here...

  doLast {
    javaexec {
      classpath = tasks['testDebugUnitTest'].classpath
      classpath = classpath + files('D:\\randoop-3.1.5\\randoop-all-3.1.5.jar')

      systemProperty 'RANDOOP_PATH', 'D:\\randoop-3.1.5'
      systemProperty 'RANDOOP_JAR', 'D:\\randoop-3.1.5\\randoop-all-3.1.5.jar'

      def classlistarg = '--classlist=' + classlistfilepath
      def packagenamearg = '--junit-package-name=' + key
      def junitoutputdirarg = '--junit-output-dir=' + projectDir.path + '/src/randooptest/java'
      def timelimitarg = '--timeLimit=10'

      main = 'randoop.main.Main'
      args 'gentests',classlistarg,packagenamearg,junitoutputdirarg,timelimitarg

      println "Randoop will be invoked with args: " + args.toString()
    }
  }
}

The --timeLimit=10 argument is meant to apply a time limit (in seconds) to the exploration stage of Randoop, but this is only working sporadically for me. In some executions of this task, Randoop begins the exploration stage and then "freezes" - the java.exe process consumes 0% CPU and no output occurs.

Is it possible to put a time limit on the JavaExec task to apply a time limit on this task?

Thanks!

2

There are 2 answers

1
Vampire On

As far as I know you cannot set a timeout on a JavaExec task or javaExec method call.

Btw. you do not use a task of type JavaExec but the javaExec method call. This only makes sense if you want to do more in your custom task. If all you want to do is the java exec, you should use a task of type JavaExec instead.

I think to get the behavior you want, you need to use standard Groovy way to call an external process like ["java", "-jar", "your.jar", "your", "arguments"].execute() and use Process.waitForOrKill(long) to wait a certain amount of time and then kill the process.

0
Andreas Lärfors On

In the end I didn't implement any time limit on my task, but rather figured out why Randoop was sometimes "stuck" for so long (it was testing a wait(...) method that just sleeps) and set Randoop to omit this method.