Skip to content

Commit

Permalink
Do not cancel subscribe calls on set state (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleewho authored Apr 11, 2023
1 parent 965f901 commit 76e2294
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 69 deletions.
13 changes: 9 additions & 4 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: kotlin
version: 7.4.2
version: 7.4.3
schema: 1
scm: github.com/pubnub/kotlin
files:
- build/libs/pubnub-kotlin-7.4.2-all.jar
- build/libs/pubnub-kotlin-7.4.3-all.jar
sdks:
-
type: library
Expand All @@ -23,8 +23,8 @@ sdks:
-
distribution-type: library
distribution-repository: maven
package-name: pubnub-kotlin-7.4.2
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-kotlin/7.4.2/pubnub-kotlin-7.4.2.jar
package-name: pubnub-kotlin-7.4.3
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-kotlin/7.4.3/pubnub-kotlin-7.4.3.jar
supported-platforms:
supported-operating-systems:
Android:
Expand Down Expand Up @@ -121,6 +121,11 @@ sdks:
license-url: https://github.com/stleary/JSON-java/blob/20210307/LICENSE
is-required: Required
changelog:
- date: 2023-04-11
version: v7.4.3
changes:
- type: bug
text: "Do not cancel subscribe call on set state."
- date: 2023-03-07
version: v7.4.2
changes:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v7.4.3
April 11 2023

#### Fixed
- Do not cancel subscribe call on set state.

## v7.4.2
March 07 2023

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ You will need the publish and subscribe keys to authenticate your app. Get your
<dependency>
<groupId>com.pubnub</groupId>
<artifactId>pubnub-kotlin</artifactId>
<version>7.4.2</version>
<version>7.4.3</version>
</dependency>
```

* for Gradle, add the following dependency in your `gradle.build`:
```groovy
implementation 'com.pubnub:pubnub-kotlin:7.4.2'
implementation 'com.pubnub:pubnub-kotlin:7.4.3'
```

2. Configure your keys:
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SONATYPE_HOST=DEFAULT
SONATYPE_AUTOMATIC_RELEASE=true
GROUP=com.pubnub
POM_ARTIFACT_ID=pubnub-kotlin
VERSION_NAME=7.4.2
VERSION_NAME=7.4.3
POM_PACKAGING=jar

POM_NAME=PubNub Android Chat Components
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/pubnub/api/PubNub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PubNub(val configuration: PNConfiguration) {

companion object {
private const val TIMESTAMP_DIVIDER = 1000
private const val SDK_VERSION = "7.4.2"
private const val SDK_VERSION = "7.4.3"
private const val MAX_SEQUENCE = 65535

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class SubscriptionManager(val pubnub: PubNub, private val subscriptionState: Sta
}

internal fun adaptStateBuilder(stateOperation: StateOperation) {
reconnect(stateOperation)
subscriptionState.handleOperation(stateOperation)
}

internal fun adaptSubscribeBuilder(subscribeOperation: SubscribeOperation) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/pubnub/api/legacy/PubNubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PubNubTest : BaseTest() {
fun getVersionAndTimeStamp() {
val version = pubnub.version
val timeStamp = pubnub.timestamp()
assertEquals("7.4.2", version)
assertEquals("7.4.3", version)
assertTrue(timeStamp > 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching
import com.google.gson.reflect.TypeToken
import com.pubnub.api.CommonUtils.emptyJson
import com.pubnub.api.PubNub
import com.pubnub.api.PubNubException
import com.pubnub.api.PubNubUtil
import com.pubnub.api.callbacks.SubscribeCallback
import com.pubnub.api.enums.PNHeartbeatNotificationOptions
Expand Down Expand Up @@ -1400,25 +1399,20 @@ class SubscriptionManagerTest : BaseTest() {
.atMost(5, TimeUnit.SECONDS)
.untilAtomic(atomic, Matchers.greaterThan(0))
}

@Test
fun testSubscribeBuilderWithState() {
val subscribeHits = AtomicInteger(0)
val heartbeatHits = AtomicInteger(0)
val expectedPayload = PubNubUtil.urlDecode(
"""%7B%22ch1%22%3A%5B%22p1%22%2C%22p2%22%5D%2C%22cg2%22%3A%5B%22p1%22%2C%22p2%22%5D%7D"""
)

val expectedMap = pubnub.mapper.fromJson<HashMap<String, Any>?>(
expectedPayload,
object : TypeToken<HashMap<String, Any>?>() {}.type
val expectedMap = pubnub.mapper.fromJson<HashMap<String, Any>>(
expectedPayload, object : TypeToken<HashMap<String, Any>?>() {}.type
)

stubFor(
get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch1/0"))
.willReturn(
aResponse().withBody(
"""
get(urlPathEqualTo("/v2/subscribe/mySubscribeKey/ch2,ch1/0")).willReturn(
aResponse().withBody(
"""
{
"t": {
"t": "14607577960932487",
Expand All @@ -1442,77 +1436,66 @@ class SubscriptionManagerTest : BaseTest() {
}
]
}
""".trimIndent()
)
""".trimIndent()
)
)
)

stubFor(
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch2,ch1/heartbeat"))
.willReturn(emptyJson())
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch2,ch1/heartbeat")).willReturn(emptyJson())
)

stubFor(
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/uuid/myUUID/data"))
.willReturn(emptyJson())
get(urlPathEqualTo("/v2/presence/sub-key/mySubscribeKey/channel/ch1/uuid/myUUID/data")).willReturn(emptyJson())
)

pubnub.configuration.presenceTimeout = 20
pubnub.configuration.heartbeatNotificationOptions = PNHeartbeatNotificationOptions.ALL

pubnub.addListener(object : SubscribeCallback() {
override fun status(pubnub: PubNub, pnStatus: PNStatus) {
val heartbeatRequests = findAll(
getRequestedFor(
urlMatching(
"""/v2/presence/sub-key/${pubnub.configuration.subscribeKey}/channel/ch2,ch1/heartbeat.*"""
)
)
)
val subscribeRequests = findAll(
getRequestedFor(
urlMatching(
"""/v2/subscribe/${pubnub.configuration.subscribeKey}/ch2,ch1/.*"""
)
)
)
for (request in subscribeRequests) {
val stateString = PubNubUtil.urlDecode(request.queryParameter("state").firstValue())
var actualMap: HashMap<String, Any>? = null
try {
actualMap = pubnub.mapper.fromJson(
stateString,
object : TypeToken<HashMap<String, Any>?>() {}.type
)
} catch (e: PubNubException) {
e.printStackTrace()
}
if (actualMap != null && actualMap == expectedMap) {
subscribeHits.getAndAdd(1)
}
}
for (request in heartbeatRequests) {
if (!request.queryParams.containsKey("state")) {
heartbeatHits.getAndAdd(1)
}
}
// do nothing
}
})

pubnub.subscribe(
channels = listOf("ch1", "ch2"),
channelGroups = listOf("cg1", "cg2")
channels = listOf("ch1", "ch2"), channelGroups = listOf("cg1", "cg2")
)

pubnub.setPresenceState(
channels = listOf("ch1"),
channelGroups = listOf("cg2"),
state = listOf("p1", "p2")
channels = listOf("ch1"), channelGroups = listOf("cg2"), state = listOf("p1", "p2")
).async { _, _ -> }

Awaitility.await()
.atMost(5, TimeUnit.SECONDS)
.until { subscribeHits.get() > 0 && heartbeatHits.get() > 0 }
Awaitility.await().atMost(5, TimeUnit.SECONDS).until { verifyCalls(expectedMap) }
}

private fun verifyCalls(expectedMap: Map<String, Any>): Boolean {
val subscribeRequests = findAll(
getRequestedFor(
urlMatching(
"""/v2/subscribe/${pubnub.configuration.subscribeKey}/ch2,ch1/.*"""
)
)
)
val subCond = subscribeRequests.any {
val stateString = PubNubUtil.urlDecode(it.queryParameter("state").firstValue())
val actualMap: HashMap<String, Any> = pubnub.mapper.fromJson(
stateString, object : TypeToken<HashMap<String, Any>>() {}.type
)
actualMap == expectedMap
}
val heartbeatRequests = findAll(
getRequestedFor(
urlMatching(
"""/v2/presence/sub-key/${pubnub.configuration.subscribeKey}/channel/ch2,ch1/heartbeat.*"""
)
)
)
val heartbeatCond = heartbeatRequests.any {
!it.queryParams.containsKey("state")
}

return subCond && heartbeatCond
}

@Test
Expand Down

0 comments on commit 76e2294

Please sign in to comment.