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

Ensure close is called in all use cases. #1188

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions okio/src/commonMain/kotlin/okio/Okio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
result = block(this)
} catch (t: Throwable) {
thrown = t
}

try {
this?.close()
} catch (t: Throwable) {
if (thrown == null) thrown = t
else thrown.addSuppressed(t)
} finally {
try {
this?.close()
} catch (t: Throwable) {
if (thrown == null) thrown = t
else thrown.addSuppressed(t)
}
}

if (thrown != null) throw thrown
Expand Down
15 changes: 15 additions & 0 deletions okio/src/commonTest/kotlin/okio/FakeFileSystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,21 @@ abstract class FakeFileSystemTest internal constructor(
}
}

@Test
fun closesWithUseBlock() {
fun testMethodWithUse() {
val sink = fakeFileSystem.sink(base / "all-files-includes-file")
yschimke marked this conversation as resolved.
Show resolved Hide resolved

sink.use {
return
yschimke marked this conversation as resolved.
Show resolved Hide resolved
}
}

testMethodWithUse()

fakeFileSystem.checkNoOpenFiles()
}

internal data class ContentTypeExtra(
val contentType: String
)
Expand Down