Skip to content

Commit b6b0125

Browse files
committed
Improved Job performance by using reference equality on state, added deferred & channel tests for bad class with exceptions on equals
1 parent f6fed2a commit b6b0125

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Job.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ internal open class JobSupport : AbstractCoroutineContextElement(Job), Job {
250250

251251
}
252252
// otherwise -- do nothing (Empty)
253-
else -> check(expect == Empty)
253+
else -> check(expect === Empty)
254254
}
255255
// #4. Do other (overridable) processing after completion handlers
256256
completionException?.let { handleCompletionException(it) }
@@ -291,7 +291,7 @@ internal open class JobSupport : AbstractCoroutineContextElement(Job), Job {
291291
// LIST -- a list of completion handlers
292292
state is NodeList -> {
293293
val node = nodeCache ?: makeNode(handler).also { nodeCache = it }
294-
if (state.addLastIf(node) { this.state == state }) return node
294+
if (state.addLastIf(node) { this.state === state }) return node
295295
}
296296
// is not active anymore
297297
else -> {

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/DeferTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kotlinx.coroutines.experimental
22

33
import org.junit.Test
44
import java.io.IOException
5+
import kotlin.test.assertTrue
56

67
class DeferTest : TestBase() {
78
@Test
@@ -73,4 +74,21 @@ class DeferTest : TestBase() {
7374
yield() // yield to both waiters
7475
finish(13)
7576
}
77+
78+
class BadClass {
79+
override fun equals(other: Any?): Boolean = error("equals")
80+
override fun hashCode(): Int = error("hashCode")
81+
override fun toString(): String = error("toString")
82+
}
83+
84+
@Test
85+
fun testDeferBadClass() = runBlocking {
86+
val bad = BadClass()
87+
val d = defer(context) {
88+
expect(1)
89+
bad
90+
}
91+
assertTrue(d.await() === bad)
92+
finish(2)
93+
}
7694
}

kotlinx-coroutines-core/src/test/kotlin/kotlinx/coroutines/experimental/channels/RendezvousChannelTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,21 @@ class RendezvousChannelTest : TestBase() {
229229
}
230230
finish(10)
231231
}
232+
233+
class BadClass {
234+
override fun equals(other: Any?): Boolean = error("equals")
235+
override fun hashCode(): Int = error("hashCode")
236+
override fun toString(): String = error("toString")
237+
}
238+
239+
@Test
240+
fun testDeferBadClass() = runBlocking {
241+
val bad = BadClass()
242+
val c = buildChannel(context) {
243+
expect(1)
244+
send(bad)
245+
}
246+
assertTrue(c.receive() === bad)
247+
finish(2)
248+
}
232249
}

0 commit comments

Comments
 (0)