Skip to content

Commit

Permalink
Merge pull request #665 from jenkinsci/bugfix/call-stack-dump
Browse files Browse the repository at this point in the history
fix(PipelineBaseTest): callstackdump should be updated if callstack is changed
  • Loading branch information
nre-ableton authored Jul 15, 2024
2 parents 82eec69 + 969ac00 commit 70482cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ abstract class BasePipelineTest {
}

private String cachedStackDump = null
private String cachedStackDumpHashCode = null

/**
* Clear the call stack in preparation to reuse this test helper.
Expand All @@ -459,11 +460,16 @@ abstract class BasePipelineTest {
String callStackDump() {
// Avoid use of Memoized to support clearing the stack
// and reusing the instance of this helper class.
if (cachedStackDump == null) {
cachedStackDump = helper.callStack.stream()
if (cachedStackDump != null && cachedStackDumpHashCode != null) {
if (cachedStackDumpHashCode == helper.callStack.hashCode())
return cachedStackDump
}

cachedStackDumpHashCode = helper.callStack.hashCode()
cachedStackDump = helper.callStack.stream()
.map { it -> it.toString() }
.collect(joining('\n'))
}

return cachedStackDump
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CallStackDumpTest extends BasePipelineTest {
void test_callStackDump_returns_new_value() {
setUp()
def script = loadScript('src/test/jenkins/job/callStackDump.jenkins')

// Call method1 and expect callstack to match
script.someMethod1()
assertCallStackContains('callStackDump.someMethod1()')
Expand All @@ -19,11 +19,30 @@ class CallStackDumpTest extends BasePipelineTest {
script.someMethod2()
assertCallStackContains('callStackDump.someMethod2()')
assertCallStack().doesNotContain('callStackDump.someMethod1()')

// Reset using clearCallStack call
clearCallStack()
// Call method1 and expect callstack to match
script.someMethod1()
assertCallStackContains('callStackDump.someMethod1()')
}

@Test
void test_callStackDump_returns_cached_value() {
setUp()
def script = loadScript('src/test/jenkins/job/callStackDump.jenkins')

// Call method1 and expect callstack to match
script.someMethod1()
assertCallStackContains('callStackDump.someMethod1()')
assertCallStack().doesNotContain('callStackDump.someMethod2()')

// Do not clear call stack continue your test

// Call method2 and expect callstack to match
script.someMethod2()
assertCallStackContains('callStackDump.someMethod1()')
assertCallStackContains('callStackDump.someMethod2()')

}
}

0 comments on commit 70482cb

Please sign in to comment.