Skip to content

Instantly share code, notes, and snippets.

@swankjesse
Created February 4, 2025 14:45
Show Gist options
  • Save swankjesse/d995ba7c79b27193fa7a908c7334ba46 to your computer and use it in GitHub Desktop.
Save swankjesse/d995ba7c79b27193fa7a908c7334ba46 to your computer and use it in GitHub Desktop.
@Test
fun closeAJarFileWhileSomebodyIsReadingIt() {
val someJarResource = this::class.java.classLoader.getResource("LICENSE-junit.txt")!!
val connectionA = someJarResource.openConnection()
val sourceA = connectionA.getInputStream().source().buffer()
val connectionB = someJarResource.openConnection()
val sourceB = connectionB.getInputStream().source().buffer()
connectionA.useCaches = false
assertThat(sourceA.readUtf8()).contains("JUnit")
sourceA.close()
assertThat(sourceB.readUtf8()).contains("JUnit")
sourceB.close()
}
@jclyne
Copy link

jclyne commented Feb 4, 2025

awesome! explains the race condition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment