From 3e9c2668ec7442aac1c2833ed9ddb73b76f4f40e Mon Sep 17 00:00:00 2001 From: Mattias Reichel Date: Fri, 21 Nov 2025 11:30:54 +0100 Subject: [PATCH] test: fix more flakiness in `PerTestRecordingSpec` --- .../demo/spock/PerTestRecordingSpec.groovy | 38 ++++++++++++++++--- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/grails-test-examples/geb/src/integration-test/groovy/org/demo/spock/PerTestRecordingSpec.groovy b/grails-test-examples/geb/src/integration-test/groovy/org/demo/spock/PerTestRecordingSpec.groovy index 67213bb4975..16a51b27bf4 100644 --- a/grails-test-examples/geb/src/integration-test/groovy/org/demo/spock/PerTestRecordingSpec.groovy +++ b/grails-test-examples/geb/src/integration-test/groovy/org/demo/spock/PerTestRecordingSpec.groovy @@ -44,7 +44,10 @@ class PerTestRecordingSpec extends ContainerGebSpec { void '(setup) running a second test to create another recording'() { when: 'visiting another page than the previous test' to(UploadPage) - + + and: 'pausing so we can see the page change in the video' + Thread.sleep(500) + then: 'the page loads correctly' title == 'Upload Test' } @@ -77,13 +80,14 @@ class PerTestRecordingSpec extends ContainerGebSpec { recordingDir != null when: 'getting all video recording files (mp4 or flv) from the recording directory' - def recordingFiles = recordingDir?.listFiles({ File file -> - isVideoFile(file) && file.name.contains(this.class.simpleName) - } as FileFilter) + def minFileCount = 2 // At least 2 files for the first two test methods + def recordingFiles = waitForRecordingFiles(recordingDir, this.class.simpleName, minFileCount) + def names = recordingFiles*.name.join('\n') then: 'recording files should exist for each test method' - recordingFiles != null - recordingFiles.length >= 2 // At least 2 files for the first two test methods + recordingFiles.length >= minFileCount + names.contains('setup_running_a_test_to_create_a_recording') + names.contains('setup_running_a_second_test_to_create_another') and: 'the recording files should have different content' // Sort by last modified time to get the most recent files @@ -96,4 +100,26 @@ class PerTestRecordingSpec extends ContainerGebSpec { private static boolean isVideoFile(File file) { file.isFile() && (file.name.endsWith('.mp4') || file.name.endsWith('.flv')) } + + private static File[] waitForRecordingFiles( + File recordingDir, + String testClassName, + int minFileCount = 2, + long timeoutMillis = 10_000L, + long pollIntervalMillis = 500L + ) { + long deadline = System.currentTimeMillis() + timeoutMillis + File[] recordingFiles = [] + while (System.currentTimeMillis() < deadline) { + recordingFiles = recordingDir.listFiles({ file -> + isVideoFile(file) && file.name.contains(testClassName) + } as FileFilter) ?: [] as File[] + + if (recordingFiles.length >= minFileCount) { + break + } + sleep(pollIntervalMillis) + } + return recordingFiles + } }