-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isolate exceptions in event/status listeners (#314)
An exception in a user's listener shouldn't crash the whole event engine
- Loading branch information
1 parent
4e27a06
commit 986142b
Showing
4 changed files
with
81 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...in/pubnub-kotlin-impl/src/test/kotlin/com/pubnub/internal/managers/ListenerManagerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.pubnub.internal.managers | ||
|
||
import com.google.gson.JsonPrimitive | ||
import com.pubnub.api.PubNub | ||
import com.pubnub.api.legacy.BaseTest | ||
import com.pubnub.api.models.consumer.pubsub.BasePubSubResult | ||
import com.pubnub.api.models.consumer.pubsub.PNMessageResult | ||
import com.pubnub.api.v2.callbacks.EventListener | ||
import org.junit.Test | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
|
||
class ListenerManagerTest : BaseTest() { | ||
@Test | ||
fun `exception in listener is isolated from other listeners`() { | ||
// given | ||
val listenerManager = ListenerManager(pubnub) | ||
val exception = Exception("Crash!") | ||
var received = mutableListOf<Any>() | ||
|
||
listenerManager.addListener(object : EventListener { | ||
override fun message(pubnub: PubNub, result: PNMessageResult) { | ||
received += exception | ||
throw exception | ||
} | ||
}) | ||
listenerManager.addListener(object : EventListener { | ||
override fun message(pubnub: PubNub, result: PNMessageResult) { | ||
received += true | ||
} | ||
}) | ||
|
||
// when | ||
listenerManager.announce(PNMessageResult(BasePubSubResult("a", null, null, null, null), JsonPrimitive("a"))) | ||
|
||
// then | ||
assertEquals(listOf(exception, true), received) | ||
} | ||
} |