Skip to content

Commit c7273bc

Browse files
committed
Update default test run time to avoid timeouts on runs without cached results.
1 parent 4ac4ce3 commit c7273bc

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

test_runner/src/main/kotlin/ftl/shard/Shard.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ftl.shard
22

3+
import com.google.common.annotations.VisibleForTesting
34
import ftl.args.AndroidArgs
45
import ftl.args.IArgs
56
import ftl.args.IosArgs
@@ -44,6 +45,9 @@ class com.foo.ClassName#testMethodToSkip
4445
*/
4546

4647
object Shard {
48+
// When a test does not have previous results to reference, fall back to this run time.
49+
@VisibleForTesting
50+
const val DEFAULT_TEST_TIME_SEC: Double = 120.0
4751

4852
private fun JUnitTestCase.androidKey(): String {
4953
return "class $classname#$name"
@@ -66,7 +70,7 @@ object Shard {
6670
if (args.shardTime < -1 || args.shardTime == 0) fatalError("Invalid shard time ${args.shardTime}")
6771

6872
val junitMap = createJunitMap(oldTestResult, args)
69-
val testsTotalTime = testsToRun.sumByDouble { junitMap[it] ?: 10.0 }
73+
val testsTotalTime = testsToRun.sumByDouble { junitMap[it] ?: DEFAULT_TEST_TIME_SEC }
7074

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

@@ -104,7 +108,7 @@ object Shard {
104108

105109
testsToRun.forEach { key ->
106110
val previousTime = junitMap[key]
107-
val time = previousTime ?: 10.0
111+
val time = previousTime ?: DEFAULT_TEST_TIME_SEC
108112

109113
if (previousTime == null) {
110114
cacheMiss += 1

test_runner/src/test/kotlin/ftl/shard/ShardTest.kt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class ShardTest {
9191
val result = Shard.createShardsByShardCount(testsToRun, JUnitTestResult(null), mockArgs(2))
9292

9393
assertThat(result.size).isEqualTo(2)
94-
assertThat(result.sumByDouble { it.time }).isEqualTo(30.0)
94+
assertThat(result.sumByDouble { it.time }).isEqualTo(3 * Shard.DEFAULT_TEST_TIME_SEC)
9595

9696
val ordered = result.sortedBy { it.testMethods.size }
9797
assertThat(ordered[0].testMethods.size).isEqualTo(1)
@@ -103,9 +103,10 @@ class ShardTest {
103103
val testsToRun = listOf("a/a", "b/b", "c/c", "w", "y", "z")
104104
val result = Shard.createShardsByShardCount(testsToRun, sample(), mockArgs(4))
105105
assertThat(result.size).isEqualTo(4)
106-
assertThat(result.sumByDouble { it.time }).isEqualTo(37.0)
106+
assertThat(result.sumByDouble { it.time }).isEqualTo(7.0 + 3 * Shard.DEFAULT_TEST_TIME_SEC)
107107

108108
val ordered = result.sortedBy { it.testMethods.size }
109+
// Expect a/a, b/b, c/c to be in one shard, and w, y, z to each be in their own shards.
109110
assertThat(ordered[0].testMethods.size).isEqualTo(1)
110111
assertThat(ordered[1].testMethods.size).isEqualTo(1)
111112
assertThat(ordered[2].testMethods.size).isEqualTo(1)
@@ -153,6 +154,43 @@ class ShardTest {
153154
assertThat(result).isEqualTo(3)
154155
}
155156

157+
@Test
158+
fun `createShardsByShardTime uncachedTestResultsUseDefaultTime`() {
159+
val testsToRun = listOf("h/h", "i/i", "j/j")
160+
val suite = sample()
161+
val result = Shard.shardCountByTime(
162+
testsToRun,
163+
suite,
164+
mockArgs(maxTestShards = -1, shardTime = Shard.DEFAULT_TEST_TIME_SEC.toInt()))
165+
166+
assertThat(result).isEqualTo(3)
167+
}
168+
169+
@Test
170+
fun `createShardsByShardTime mixedCachedAndUncachedTestResultsUseDefaultTime`() {
171+
// Test "a/a" is hard-coded to have 1.0 second run time in test suite results.
172+
val testsToRun = listOf("a/a", "i/i", "j/j")
173+
val suite = sample()
174+
val result = Shard.shardCountByTime(
175+
testsToRun,
176+
suite,
177+
mockArgs(maxTestShards = -1, shardTime = Shard.DEFAULT_TEST_TIME_SEC.toInt() + 1))
178+
179+
assertThat(result).isEqualTo(2)
180+
}
181+
182+
@Test
183+
fun `createShardsByShardTime uncachedTestResultsAllInOneShard`() {
184+
val testsToRun = listOf("i/i", "j/j")
185+
val suite = sample()
186+
val result = Shard.shardCountByTime(
187+
testsToRun,
188+
suite,
189+
mockArgs(maxTestShards = -1, shardTime = (Shard.DEFAULT_TEST_TIME_SEC * 2).toInt()))
190+
191+
assertThat(result).isEqualTo(1)
192+
}
193+
156194
@Test(expected = RuntimeException::class)
157195
fun `createShardsByShardCount throws on forcedShardCount = 0`() {
158196
Shard.createShardsByShardCount(

0 commit comments

Comments
 (0)