Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concurrency bug when two tests write to the same image file #1727

Open
elihart opened this issue Dec 4, 2024 · 0 comments
Open

Concurrency bug when two tests write to the same image file #1727

elihart opened this issue Dec 4, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@elihart
Copy link

elihart commented Dec 4, 2024

Description
If two different test classes take a snapshot with the same sha, and "maxParallelForks" is set so that the tests can run in parallel, it is possible to encounter a race condition where the image does not exist at the end of the test.

Steps to Reproduce

This copies the writeAtomically function from HtmlReportWriter and shows how using it to write a string to a file reproduces the issue. If you run this, occasionally it will print "false", indicating that the expected file does not exist.

fun main(): Unit = runBlocking(Dispatchers.IO) {
    val file = File("test.txt")

     (1..1000).map {
        async {
            file.writeAtomically("Hello, World!")
        }
    }.awaitAll()

    println(file.exists())
}

private fun File.writeAtomically(msg: String) {
    val tmpFile = File(parentFile, "$name.tmp")
    tmpFile.writeText(msg)
    delete()
    tmpFile.renameTo(this)
}

Expected behavior
Tests can safely be run in parallel, and produce the correct image file output.

I believe this can be fixed by changing val tmpFile = File(parentFile, "$name.tmp") to val tmpFile = File(parentFile, "$name-${UUID.randomUUID()}.tmp") so that the tmp files do not collide

@elihart elihart added the bug Something isn't working label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant