Skip to content

Commit 9a9eb70

Browse files
committed
Remove validations since server does that.
1 parent 943f078 commit 9a9eb70

File tree

5 files changed

+65
-93
lines changed

5 files changed

+65
-93
lines changed

pubnub-kotlin/pubnub-kotlin-api/src/jvmMain/kotlin/com/pubnub/api/PubNub.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,6 @@ actual interface PubNub : StatusEmitter, EventEmitter {
774774
* - Values outside the valid range are automatically adjusted to 1000 with a warning log
775775
* @param offset Zero-based starting index for pagination. Returns occupants starting from this position in the list. Must be >= 0.
776776
* - Default: null (no offset)
777-
* - Requires limit > 0 (throws [PubNubException] with [PubNubError.HERE_NOW_OFFSET_REQUIRES_LIMIT_HIGHER_THAN_0] if limit is 0)
778-
* - Use with limit to paginate through large user lists
779-
* - Throws [PubNubException] with [PubNubError.HERE_NOW_OFFSET_OUT_OF_RANGE] if negative
780777
*/
781778
actual fun hereNow(
782779
channels: List<String>,

pubnub-kotlin/pubnub-kotlin-core-api/src/commonMain/kotlin/com/pubnub/api/PubNubError.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,6 @@ enum class PubNubError(private val code: Int, val message: String) {
240240
"Channel and/or ChannelGroup contains empty string which is not allowed.",
241241
),
242242

243-
HERE_NOW_OFFSET_OUT_OF_RANGE(
244-
182,
245-
"HereNow offset is out of range. Valid range is 0 to infinity.",
246-
),
247-
248-
HERE_NOW_OFFSET_REQUIRES_LIMIT_HIGHER_THAN_0(
249-
183,
250-
"offset requires limit > 0",
251-
)
252-
253243
;
254244

255245
override fun toString(): String {

pubnub-kotlin/pubnub-kotlin-impl/src/integrationTest/kotlin/com/pubnub/api/integration/PresenceIntegrationTests.kt

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pubnub.api.integration
22

33
import com.pubnub.api.PubNub
4+
import com.pubnub.api.PubNubException
45
import com.pubnub.api.callbacks.SubscribeCallback
56
import com.pubnub.api.enums.PNHeartbeatNotificationOptions
67
import com.pubnub.api.enums.PNStatusCategory
@@ -24,7 +25,6 @@ import org.junit.Assert.assertFalse
2425
import org.junit.Assert.assertNull
2526
import org.junit.Assert.assertTrue
2627
import org.junit.Test
27-
import org.junit.jupiter.api.Assertions.assertNotNull
2828
import org.junit.jupiter.api.Timeout
2929
import java.util.concurrent.TimeUnit
3030
import java.util.concurrent.atomic.AtomicBoolean
@@ -741,4 +741,62 @@ class PresenceIntegrationTests : BaseIntegrationTest() {
741741
channelGroup = channelGroupName,
742742
).sync()
743743
}
744+
745+
@Test
746+
fun testHereNowWithLimitAbove1000andBelow0() {
747+
val limitAboveMax = 2000
748+
val totalClientsCount = 5
749+
val expectedChannel = randomChannel()
750+
751+
val clients =
752+
mutableListOf(pubnub).apply {
753+
addAll(generateSequence { createPubNub {} }.take(totalClientsCount - 1).toList())
754+
}
755+
756+
clients.forEach {
757+
it.subscribeNonBlocking(expectedChannel)
758+
}
759+
Thread.sleep(2000)
760+
761+
// This should not throw a client-side validation error
762+
// Server will validate the limit and respond accordingly
763+
pubnub.hereNow(
764+
channels = listOf(expectedChannel),
765+
includeUUIDs = true,
766+
limit = limitAboveMax,
767+
).asyncRetry { result ->
768+
assertTrue(result.isFailure)
769+
result.onFailure { exception: PubNubException ->
770+
assertTrue(exception.message!!.contains("Cannot return more than 1000 uuids at a time"))
771+
}
772+
}
773+
}
774+
775+
@Test
776+
fun testHereNowWithLimitBelow0() {
777+
val limitBelow0 = -1
778+
val totalClientsCount = 5
779+
val expectedChannel = randomChannel()
780+
781+
val clients =
782+
mutableListOf(pubnub).apply {
783+
addAll(generateSequence { createPubNub {} }.take(totalClientsCount - 1).toList())
784+
}
785+
786+
clients.forEach {
787+
it.subscribeNonBlocking(expectedChannel)
788+
}
789+
Thread.sleep(2000)
790+
791+
pubnub.hereNow(
792+
channels = listOf(expectedChannel),
793+
includeUUIDs = true,
794+
limit = limitBelow0,
795+
).asyncRetry { result ->
796+
assertTrue(result.isFailure)
797+
result.onFailure { exception: PubNubException ->
798+
assertTrue(exception.message!!.contains("Limit must be greater than or equal to 0"))
799+
}
800+
}
801+
}
744802
}

pubnub-kotlin/pubnub-kotlin-impl/src/main/kotlin/com/pubnub/internal/endpoints/presence/HereNowEndpoint.kt

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.pubnub.internal.endpoints.presence
22

33
import com.google.gson.JsonElement
4-
import com.pubnub.api.PubNubError
5-
import com.pubnub.api.PubNubException
64
import com.pubnub.api.endpoints.presence.HereNow
75
import com.pubnub.api.enums.PNOperationType
86
import com.pubnub.api.logging.LogMessage
@@ -22,8 +20,6 @@ import retrofit2.Response
2220

2321
private const val MAX_CHANNEL_OCCUPANTS_LIMIT = 1000
2422

25-
private const val MIN_CHANNEL_OCCUPANTS_LIMIT = 0
26-
2723
/**
2824
* @see [PubNubImpl.hereNow]
2925
*/
@@ -37,19 +33,6 @@ class HereNowEndpoint internal constructor(
3733
override val offset: Int? = null,
3834
) : EndpointCore<Envelope<JsonElement>, PNHereNowResult>(pubnub), HereNow {
3935
private val log: PNLogger = LoggerManager.instance.getLogger(pubnub.logConfig, this::class.java)
40-
internal val effectiveLimit: Int = if (limit in MIN_CHANNEL_OCCUPANTS_LIMIT..MAX_CHANNEL_OCCUPANTS_LIMIT) {
41-
limit
42-
} else {
43-
log.warn(
44-
LogMessage(
45-
LogMessageContent.Text(
46-
"Valid range is $MIN_CHANNEL_OCCUPANTS_LIMIT to $MAX_CHANNEL_OCCUPANTS_LIMIT. " +
47-
"Shrinking limit to $MAX_CHANNEL_OCCUPANTS_LIMIT."
48-
)
49-
)
50-
)
51-
MAX_CHANNEL_OCCUPANTS_LIMIT
52-
}
5336

5437
private fun isGlobalHereNow() = channels.isEmpty() && channelGroups.isEmpty()
5538

@@ -58,13 +41,6 @@ class HereNowEndpoint internal constructor(
5841
override fun getAffectedChannelGroups() = channelGroups
5942

6043
override fun doWork(queryParams: HashMap<String, String>): Call<Envelope<JsonElement>> {
61-
if (offset != null && offset < 0) {
62-
throw PubNubException(PubNubError.HERE_NOW_OFFSET_OUT_OF_RANGE)
63-
}
64-
if (offset != null && effectiveLimit == 0) {
65-
throw PubNubException(PubNubError.HERE_NOW_OFFSET_REQUIRES_LIMIT_HIGHER_THAN_0)
66-
}
67-
6844
log.debug(
6945
LogMessage(
7046
message = LogMessageContent.Object(
@@ -73,7 +49,7 @@ class HereNowEndpoint internal constructor(
7349
"channelGroups" to channelGroups,
7450
"includeState" to includeState,
7551
"includeUUIDs" to includeUUIDs,
76-
"limit" to effectiveLimit,
52+
"limit" to limit,
7753
"offset" to (offset?.toString() ?: "null"),
7854
"isGlobalHereNow" to isGlobalHereNow(),
7955
),
@@ -196,7 +172,7 @@ class HereNowEndpoint internal constructor(
196172
if (channelGroups.isNotEmpty()) {
197173
queryParams["channel-group"] = channelGroups.toCsv()
198174
}
199-
queryParams["limit"] = effectiveLimit.toString()
175+
queryParams["limit"] = limit.toString()
200176
offset?.let { queryParams["offset"] = it.toString() }
201177
}
202178
}

pubnub-kotlin/pubnub-kotlin-impl/src/test/kotlin/com/pubnub/internal/endpoints/presence/HereNowEndpointTest.kt

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,7 @@ class HereNowEndpointTest : BaseTest() {
4040
}
4141

4242
@Test
43-
fun testHereNowLimitMinimumBoundary() {
44-
// Test minimum valid limit value
45-
val hereNow = pubnub.hereNow(
46-
channels = listOf("test-channel"),
47-
includeUUIDs = true,
48-
limit = 1
49-
)
50-
assertNotNull(hereNow)
51-
assertEquals(1, (hereNow as HereNowEndpoint).limit)
52-
}
53-
54-
@Test
55-
fun testHereNowLimitMaximumBoundary() {
43+
fun testHereNowAcceptsDefaultLimitValue() {
5644
// Test maximum valid limit value
5745
val hereNow = pubnub.hereNow(
5846
channels = listOf("test-channel"),
@@ -64,33 +52,9 @@ class HereNowEndpointTest : BaseTest() {
6452
}
6553

6654
@Test
67-
fun testHereNowLimitAboveMaximumShrinks() {
68-
// Test that limit > 1000 automatically shrinks to 1000
69-
val endpoint = HereNowEndpoint(
70-
pubnub = pubnub,
71-
channels = listOf("test-channel"),
72-
includeUUIDs = true,
73-
limit = 1500
74-
)
75-
assertNotNull(endpoint)
76-
// When limit is out of range, endpoint stores the original value
77-
assertEquals(1500, endpoint.limit)
78-
assertEquals(1000, endpoint.effectiveLimit)
79-
}
80-
81-
@Test
82-
fun testHereNowLimitNegativeUsesDefault() {
83-
// Test that negative limit uses default of 1000
84-
val endpoint = HereNowEndpoint(
85-
pubnub = pubnub,
86-
channels = listOf("test-channel"),
87-
includeUUIDs = true,
88-
limit = -5
89-
)
90-
assertNotNull(endpoint)
91-
// When limit is out of range, the original value is stored
92-
assertEquals(-5, endpoint.limit)
93-
assertEquals(1000, endpoint.effectiveLimit)
55+
fun testHereNowAcceptsLimitBeyondAdvisedMaximum() {
56+
val hereNow = pubnub.hereNow(channels = listOf("test"), limit = 5000)
57+
assertEquals(5000, (hereNow as HereNowEndpoint).limit) // Passes to server for validation
9458
}
9559

9660
@Test
@@ -104,17 +68,4 @@ class HereNowEndpointTest : BaseTest() {
10468
assertNotNull(hereNow)
10569
assertEquals(1000000, (hereNow as HereNowEndpoint).offset)
10670
}
107-
108-
@Test
109-
fun testHereNowStartFromLargeNegativeAccepted() {
110-
// Test that large negative offset is accepted at creation time
111-
// (validation happens during execution in doWork())
112-
val hereNow = pubnub.hereNow(
113-
channels = listOf("test-channel"),
114-
includeUUIDs = true,
115-
offset = -100
116-
)
117-
assertNotNull(hereNow)
118-
assertEquals(-100, (hereNow as HereNowEndpoint).offset)
119-
}
12071
}

0 commit comments

Comments
 (0)