2

We run unit tests on Jenkins and one of our tests freezes sometimes.

We have timeouts defined in the Jenkins pipeline and the freeze triggers the timeout and that kills the testing process.

Is there a way (via Jenkins pipelines, maybe via Groovy) to execute a command (e.g. create a process dump of the testing process) as soon as we run into a timeout, but (of course) before the timeout kills the testing process?

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
hardfork
  • 2,470
  • 1
  • 23
  • 43

1 Answers1

1

You can wrap our test execution with a try-catch and do whatever you need after catching the timeout exception. Here is a sample Pipeline.

pipeline {
    agent any
    stages {
           
        stage('Hello') {

            steps {
                script{
                    try {
                        timeout(unit: 'SECONDS', time: 5) { 
                        echo "Running your  Tests here!!!!"
                        sleep 10
                        }   
                    } catch (e){
                        echo "The tests erroredout!!!" + e.getCauses()
                        if(e.getCauses()[0] instanceof org.jenkinsci.plugins.workflow.steps.TimeoutStepExecution$ExceededTimeout) {
                            echo "This is a timeout, do whatever you want..."
                        }
                    } 
            }
        }
    }
}
}
ycr
  • 12,828
  • 2
  • 25
  • 45
  • The problem is, that we have a bat '....' call in the try block and that the timeout of the pipeline kills that process. And after it's killed, we can't create a dump of it anymore. (If we reach the catch block, the process we want to create a dump of was already killed) – hardfork Jan 05 '23 at 12:56
  • @ndsvw I'm not sure what you are referring to when you say creating a dump. If that is something associated to the batch process there is no way to do this in the Jenkins level, yo may have to handle it withn the bat command itself. Can't you always run your tests in a verbose mode or something? – ycr Jan 05 '23 at 13:00
  • 1
    To elaborate, don't let Jenkins kill the timed-out test execution. Handle the timeout procedure in the Batch command itself. This will give you much more flexibility. – ycr Jan 05 '23 at 13:03
  • 1
    That's probably true. We've just found a feature request for what we are trying to achieve, but it's not implemented: https://issues.jenkins.io/browse/JENKINS-55958 – hardfork Jan 05 '23 at 13:12