Skip to content

Commit

Permalink
Improve shard count validation (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
bootstraponline authored Aug 5, 2019
1 parent e0e2b21 commit d872f95
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [#574](https://github.com/TestArmada/flank/pull/574) Improve test shard error reporting. Update device catalog to use projectId. ([bootstraponline](https://github.com/bootstraponline))
- [#582](https://github.com/TestArmada/flank/pull/582) Fix iOS exit code when using flaky-test-attempts. Don't print environment-variables to stdout for security. ([bootstraponline](https://github.com/bootstraponline))
- [#584](https://github.com/TestArmada/flank/pull/584) Poll all test executions instead of only the first per matrix. ([bootstraponline](https://github.com/bootstraponline))
- [#585](https://github.com/TestArmada/flank/pull/585) Fix bug in smart flank when sharding tests that run in 0 seconds. ([bootstraponline](https://github.com/bootstraponline))

## v6.2.3

Expand Down
13 changes: 11 additions & 2 deletions test_runner/src/main/kotlin/ftl/shard/Shard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,28 @@ object Shard {
args: IArgs
): Int {
if (args.shardTime == -1) return -1
if (args.shardTime < -1 || args.shardTime == 0) fatalError("Invalid shard time ${args.shardTime}")

val junitMap = createJunitMap(oldTestResult, args)
val testsTotalTime = testsToRun.sumByDouble { junitMap[it] ?: 10.0 }

val shardsByTime = ceil(testsTotalTime / args.shardTime).toInt()

// Use a single shard unless total test time is greater than shardTime.
if (testsTotalTime <= args.shardTime) {
return 1
}

// If there is no limit, use the calculated amount
if (args.maxTestShards == -1) {
return shardsByTime
}

// We need to respect the maxTestShards
return min(shardsByTime, args.maxTestShards)
val shardCount = min(shardsByTime, args.maxTestShards)

if (shardCount <= 0) fatalError("Invalid shard count $shardCount")
return shardCount
}

// take in the XML with timing info then return list of shards based on the amount of shards to use
Expand All @@ -85,7 +94,7 @@ object Shard {
args: IArgs,
forcedShardCount: Int = -1
): List<TestShard> {
if (forcedShardCount < -1) fatalError("Invalid forcedShardCount value $forcedShardCount")
if (forcedShardCount < -1 || forcedShardCount == 0) fatalError("Invalid forcedShardCount value $forcedShardCount")

val maxShards = if (forcedShardCount == -1) args.maxTestShards else forcedShardCount
val junitMap = createJunitMap(oldTestResult, args)
Expand Down
64 changes: 64 additions & 0 deletions test_runner/src/test/kotlin/ftl/shard/ShardTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,68 @@ class ShardTest {

assertThat(result).isEqualTo(3)
}

@Test(expected = RuntimeException::class)
fun `createShardsByShardCount throws on forcedShardCount = 0`() {
Shard.createShardsByShardCount(
listOf(),
sample(),
mockArgs(-1, 7),
0)
}

private fun newSuite(testCases: MutableList<JUnitTestCase>): JUnitTestResult {
return JUnitTestResult(mutableListOf(
JUnitTestSuite("",
"3",
"0",
"0",
"0",
"12.032",
"2019-07-27T08:15:04",
"localhost",
testCases,
null,
null,
null)))
}

private fun shardCountByTime(shardTime: Int): Int {
val testsToRun = listOf("a/a", "b/b", "c/c")
val testCases = mutableListOf(
JUnitTestCase("a", "a", "0.001"),
JUnitTestCase("b", "b", "0.0"),
JUnitTestCase("c", "c", "0.0")
)

val oldTestResult = newSuite(testCases)

return Shard.shardCountByTime(
testsToRun,
oldTestResult,
mockArgs(-1, shardTime))
}

@Test(expected = RuntimeException::class)
fun `shardCountByTime throws on invalid shard time -3`() {
shardCountByTime(-3)
}

@Test(expected = RuntimeException::class)
fun `shardCountByTime throws on invalid shard time -2`() {
shardCountByTime(-2)
}

@Test(expected = RuntimeException::class)
fun `shardCountByTime throws on invalid shard time 0`() {
shardCountByTime(0)
}

@Test
fun `shardCountByTime shards valid times`() {
assertThat(shardCountByTime(-1)).isEqualTo(-1)
assertThat(shardCountByTime(1)).isEqualTo(1)
assertThat(shardCountByTime(2)).isEqualTo(1)
assertThat(shardCountByTime(3)).isEqualTo(1)
}
}

0 comments on commit d872f95

Please sign in to comment.