-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
187 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/atlassian/performance/tools/awsinfrastructure/FailSafeRunnable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.atlassian.performance.tools.awsinfrastructure | ||
|
||
internal class FailSafeRunnable( | ||
private val delegates: Iterable<Runnable> | ||
) : Runnable { | ||
override fun run() { | ||
val exceptions = delegates.mapNotNull { | ||
try { | ||
it.run() | ||
null | ||
} catch (e: Exception) { | ||
e | ||
} | ||
} | ||
|
||
when { | ||
exceptions.isEmpty() -> return | ||
exceptions.size == 1 -> throw exceptions[0] | ||
else -> { | ||
val root = Exception("Multiple exceptions were thrown and are added suppressed into this one") | ||
exceptions.forEach { root.addSuppressed(it) } | ||
throw root | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...atlassian/performance/tools/awsinfrastructure/api/loadbalancer/DiagnosableLoadBalancer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.atlassian.performance.tools.awsinfrastructure.api.loadbalancer | ||
|
||
import com.atlassian.performance.tools.aws.api.StorageLocation | ||
import com.atlassian.performance.tools.infrastructure.api.loadbalancer.LoadBalancer | ||
|
||
interface DiagnosableLoadBalancer : LoadBalancer { | ||
fun gatherEvidence(location: StorageLocation) | ||
} |
23 changes: 23 additions & 0 deletions
23
...lassian/performance/tools/awsinfrastructure/loadbalancer/LoadBalancerMeasurementSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.atlassian.performance.tools.awsinfrastructure.loadbalancer | ||
|
||
import com.atlassian.performance.tools.aws.api.StorageLocation | ||
import com.atlassian.performance.tools.awsinfrastructure.api.loadbalancer.DiagnosableLoadBalancer | ||
import com.atlassian.performance.tools.infrastructure.api.MeasurementSource | ||
|
||
internal class LoadBalancerMeasurementSource( | ||
private val loadBalancer: DiagnosableLoadBalancer, | ||
private val target: StorageLocation | ||
) : MeasurementSource { | ||
internal object Extension { | ||
fun DiagnosableLoadBalancer.asMeasurementSource( | ||
target: StorageLocation | ||
) = LoadBalancerMeasurementSource( | ||
loadBalancer = this, | ||
target = target | ||
) | ||
} | ||
|
||
override fun gatherResults() { | ||
loadBalancer.gatherEvidence(target) | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/test/kotlin/com/atlassian/performance/tools/awsinfrastructure/FailSafeRunnableTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.atlassian.performance.tools.awsinfrastructure | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
|
||
class FailSafeRunnableTest { | ||
@Test | ||
fun shouldExecuteAllEvenIfFirstFails() { | ||
var executed1 = false | ||
var executed2 = false | ||
var executed3 = false | ||
val runnable = FailSafeRunnable( | ||
listOf( | ||
Runnable { executed1 = true; throw Exception("Fail 1") }, | ||
Runnable { executed2 = true; throw Exception("Fail 2") }, | ||
Runnable { executed3 = true; throw Exception("Fail 3") } | ||
) | ||
) | ||
|
||
try { | ||
runnable.run() | ||
} catch (e: Exception) { | ||
// Expected and ignored, so that we can go to asserts | ||
} | ||
|
||
assertThat(executed1).isTrue() | ||
assertThat(executed2).isTrue() | ||
assertThat(executed3).isTrue() | ||
} | ||
|
||
@Test | ||
fun shouldThrowAllFailures() { | ||
val runnable = FailSafeRunnable( | ||
listOf( | ||
Runnable { throw Exception("Banana") }, | ||
Runnable { throw Exception("Apple") }, | ||
Runnable { throw Exception("Pear") }, | ||
Runnable { throw Exception("Peach") } | ||
) | ||
) | ||
|
||
val exception = try { | ||
runnable.run() | ||
null | ||
} catch (e: Exception) { | ||
e | ||
} | ||
|
||
val allExceptions = listOf(exception!!) + exception.suppressed.toList() | ||
val allMessages = allExceptions.map { it.message } | ||
assertThat(allMessages).contains("Banana", "Apple", "Pear", "Peach") | ||
} | ||
|
||
|
||
@Test | ||
fun shouldExecuteAll() { | ||
val allIndexes = Array(20) { it } | ||
val finishedIndexes = mutableListOf<Int>() | ||
val runnable = FailSafeRunnable( | ||
allIndexes.map { index -> Runnable { finishedIndexes.add(index) } } | ||
) | ||
|
||
runnable.run() | ||
|
||
assertThat(finishedIndexes).contains(*allIndexes) | ||
} | ||
} |