Skip to content

Commit

Permalink
Add special case for retry step
Browse files Browse the repository at this point in the history
Update CHANGELOG.md
  • Loading branch information
Stuart Rowe committed Jun 10, 2020
1 parent 935c4f2 commit b03dfdb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## Unreleased

* (nothing yet)
### Added

* The `retry()` pipeline step is special-cased and will execute its closure up to the specified count.

## 2.1.4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.jenkinsci.plugins.workflow.cps.CpsScript
import org.slf4j.Logger
import org.slf4j.LoggerFactory

import hudson.AbortException
import hudson.Extension
import hudson.ExtensionList
import hudson.model.Hudson
Expand Down Expand Up @@ -675,7 +676,23 @@ public abstract class JenkinsPipelineSpecification extends Specification {
}
}

} else if( _args != null && _args.length >= 1 && _args[_args.length-1] instanceof Closure ) {
} else if( _name == "retry" ) {
// special-case the retry() step to run up to count times
int left = _args[0]
Closure body = _args[1]
while (left > 0) {
try {
body()
break
} catch (Exception e) {
left--
if (left <= 0) {
throw e
}
}
}

} else if( _args != null && _args.length >= 1 && _args[_args.length-1] instanceof Closure ) {
// there was at least one argument, and the last argument was a Closure
// this almost certainly means that the user's trying to pass the closure to the function as a "body"
// they probably want it to execute right now.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright (c) 2020 Electronic Arts Inc.
All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package com.homeaway.devtools.jenkins.testing;

/**
* Verifies that the <code>retry(...)</code> pipeline step is special-cased and
* that any closures passed to it as arguments are executed up to <code>count</code> times.
*
* @author stuartrowe
*
*/
public class RetryClosureExecutionSpec extends JenkinsPipelineSpecification {

def "retry() closure is only executed once when the first execution succeeds"() {
when:
retry(2) {
sh( "echo hello" )
}
then:
1 * getPipelineMock("sh")(*_)
}

def "retry() closure is executed twice when the first execution fails"() {
when:
retry(2) {
sh( "echo hello" )
}
then:
1 * getPipelineMock("sh")(*_) >> { throw new Exception() }
then:
1 * getPipelineMock("sh")(*_)
}

def "retry() propagates the exception when retries are exhausted"() {
when:
retry(2) {
sh( "echo hello" )
}
then:
2 * getPipelineMock("sh")(*_) >> { throw new Exception() }
then:
thrown Exception
}
}

0 comments on commit b03dfdb

Please sign in to comment.