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

Fixed closeableScope #69

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2.2.0 (2024-03-12)
## 2.2.0 (2024-03-26)

### Added

Expand All @@ -19,6 +19,8 @@ been also added for compatibility)
([#67](https://github.com/Black-Kamelia/Sprinkler/pull/67/commits/e546a63e410f96c212ba5c0c7c48b5ccd752c04f))
- Nullable type boxes now work correctly when filled with null
([#67](https://github.com/Black-Kamelia/Sprinkler/pull/67/commits/e546a63e410f96c212ba5c0c7c48b5ccd752c04f))
- Fixed the behavior of `closeableScope` to properly re-throw the exception of the `try` block even when the closing
process fails too.


## 2.1.0 (2024-02-02)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ value class CloseableScope private constructor(private val closeables: ArrayList
fun <T : AutoCloseable> T.usingSelf(): T = using(this)

@PublishedApi
internal fun closeAll() {
var exception: Throwable? = null
internal fun closeAll(initialException: Throwable?) {
var exception: Throwable? = initialException
for (i in closeables.lastIndex downTo 0) {
try {
closeables[i].close()
Expand Down Expand Up @@ -76,10 +76,14 @@ value class CloseableScope private constructor(private val closeables: ArrayList
*/
inline fun <R> closeableScope(vararg closeables: AutoCloseable, block: CloseableScope.() -> R): R {
val scope = CloseableScope()
closeables.forEach(scope::using)
var exception: Throwable? = null
try {
closeables.forEach(scope::using)
return scope.block()
} catch (e: Throwable) {
exception = e
} finally {
scope.closeAll()
scope.closeAll(exception)
}
throw AssertionError("Unreachable code")
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class CloseableScopeTest {
class ThrowingCloseableMock : CloseableMock() {
override fun close() {
super.close()
throw RuntimeException()
throw Exception()
}

val index = i++

class Exception: RuntimeException()

companion object {
var i = 0
}
Expand Down Expand Up @@ -84,6 +86,7 @@ class CloseableScopeTest {

@Test
fun `should close scoped closeables even if an exception is thrown in a closeable`() {
ThrowingCloseableMock.i = 0
lateinit var closeable1: ThrowingCloseableMock
lateinit var closeable2: ThrowingCloseableMock

Expand All @@ -101,10 +104,41 @@ class CloseableScopeTest {
assertTrue(res.isFailure)
val exception = res.exceptionOrNull()
assertNotNull(exception)
assertTrue(exception is RuntimeException)
assertTrue(exception is ThrowingCloseableMock.Exception)
assertEquals(1, exception!!.suppressed.size)
assertEquals(0, exception.suppressed[0].suppressed.size)
assertTrue(exception.suppressed[0] is RuntimeException)
assertTrue(exception.suppressed[0] is ThrowingCloseableMock.Exception)
}

@Test
fun `should close scoped closeables even if an exception is thrown in a closeable and in the scope and the former should be suppressed under the latter`() {
ThrowingCloseableMock.i = 0
lateinit var closeable1: ThrowingCloseableMock
lateinit var closeable2: ThrowingCloseableMock

class ScopeException : RuntimeException()

val res = runCatching {
closeableScope {
closeable1 = using(ThrowingCloseableMock())
closeable2 = using(ThrowingCloseableMock())
throw ScopeException()
}
}

assertTrue(closeable1.isClosed)
assertTrue(closeable2.isClosed)
assertEquals(0, closeable1.index)
assertEquals(1, closeable2.index)
assertTrue(res.isFailure)
val exception = res.exceptionOrNull()
assertNotNull(exception)
assertTrue(exception!! is ScopeException, "Expected ScopeException but got ${exception::class}")
assertEquals(2, exception.suppressed.size)
assertEquals(0, exception.suppressed[0].suppressed.size)
assertEquals(0, exception.suppressed[1].suppressed.size)
assertTrue(exception.suppressed[0] is ThrowingCloseableMock.Exception)
assertTrue(exception.suppressed[1] is ThrowingCloseableMock.Exception)
}

@Test
Expand Down