Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add special case for retry step #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -675,7 +675,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) {
Copy link
Author

@stuartrowe stuartrowe Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a greatly simplified implementation of RetryStepExecution

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
}
}