Skip to content

Commit 57a0b03

Browse files
committed
Modified test to use customLogger instead of httpLoggingInterceptor
1 parent bf102b7 commit 57a0b03

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

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

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import com.google.gson.JsonObject
55
import com.pubnub.api.PubNub
66
import com.pubnub.api.callbacks.SubscribeCallback
77
import com.pubnub.api.enums.PNStatusCategory
8+
import com.pubnub.api.logging.CustomLogger
9+
import com.pubnub.api.logging.LogMessage
10+
import com.pubnub.api.logging.LogMessageContent
11+
import com.pubnub.api.logging.LogMessageType
812
import com.pubnub.api.models.consumer.PNStatus
913
import com.pubnub.api.models.consumer.channel_group.PNChannelGroupsAddChannelResult
1014
import com.pubnub.api.models.consumer.pubsub.PNMessageResult
@@ -1254,30 +1258,34 @@ class SubscribeIntegrationTests : BaseIntegrationTest() {
12541258
}
12551259

12561260
@Test
1257-
fun `shouldDeduplicateChannelSubscriptionsWhenSubscribingToSameChannelMultipleTimes`() {
1261+
fun shouldDeduplicateChannelSubscriptionsWhenSubscribingToSameChannelMultipleTimes() {
12581262
// given
12591263
val numberOfSubscribe = 4
12601264
// punbub.subscribe does subscribe to already subscribed channel so only two subscribe calls should occur. Handshake and actual subscribe.
12611265
val countDownLatch = CountDownLatch(2)
12621266
var interceptedUrl: HttpUrl? = null
12631267
val testChannel = randomChannel()
12641268

1265-
clientConfig = {
1266-
httpLoggingInterceptor =
1267-
HttpLoggingInterceptor { message ->
1268-
// Intercept subscribe GET request
1269-
if (message.startsWith("--> GET https://") && message.contains("/v2/subscribe/")) {
1270-
val url = message.substringAfter("--> GET ").substringBefore(" HTTP")
1271-
interceptedUrl = url.toHttpUrlOrNull()
1269+
val customLogger = object : CustomLogger {
1270+
override fun debug(logMessage: LogMessage) {
1271+
if(logMessage.type == LogMessageType.NETWORK_REQUEST){
1272+
val networkRequestDetails = logMessage.message as LogMessageContent.NetworkRequest
1273+
if (networkRequestDetails.path.contains("/v2/subscribe/")) {
1274+
interceptedUrl = (networkRequestDetails.origin + networkRequestDetails.path).toHttpUrlOrNull()
12721275
countDownLatch.countDown()
12731276
}
1274-
}.apply { level = HttpLoggingInterceptor.Level.BASIC }
1277+
}
1278+
}
1279+
}
1280+
1281+
clientConfig = {
1282+
customLoggers = listOf(customLogger)
12751283
}
12761284

12771285
try {
12781286
repeat(numberOfSubscribe) { iteration ->
12791287
pubnub.subscribe(channels = listOf(testChannel))
1280-
Thread.sleep(2000)
1288+
Thread.sleep(150)
12811289
println("Subscribe call ${iteration + 1}/$numberOfSubscribe completed")
12821290
}
12831291

@@ -1293,9 +1301,10 @@ class SubscribeIntegrationTests : BaseIntegrationTest() {
12931301
// then: verify the actual HTTP request only includes the channel once
12941302
assertNotNull("Expected to intercept subscribe URL", interceptedUrl)
12951303

1296-
val channelsParam =
1297-
interceptedUrl!!.queryParameter("channel") ?: interceptedUrl!!.encodedPath.substringAfter("/subscribe/")
1298-
.substringAfter("/").substringBefore("/")
1304+
val channelsParam = interceptedUrl!!.encodedPath
1305+
.substringAfter("/subscribe/")
1306+
.substringAfter("/")
1307+
.substringBefore("/")
12991308

13001309
val channelList = channelsParam.split(",").filter { it.isNotEmpty() }
13011310

@@ -1306,24 +1315,28 @@ class SubscribeIntegrationTests : BaseIntegrationTest() {
13061315
}
13071316

13081317
@Test
1309-
fun `heartbeatShouldDeduplicateChannelNameInUrlWhenSubscribingToSameChannelMultipleTimes`() {
1318+
fun heartbeatShouldDeduplicateChannelNameInUrlWhenSubscribingToSameChannelMultipleTimes() {
13101319
// given
13111320
val numberOfSubscribe = 4
13121321
val countDownLatch = CountDownLatch(2) // we want to verify second heartbeat URL
13131322
var interceptedUrl: HttpUrl? = null
13141323
val testChannel = randomChannel()
13151324

1316-
clientConfig = {
1317-
heartbeatInterval = 5
1318-
httpLoggingInterceptor =
1319-
HttpLoggingInterceptor { message ->
1320-
// Intercept subscribe GET request
1321-
if (message.startsWith("--> GET https://") && message.contains("/v2/presence/") && message.contains("/heartbeat")) {
1322-
val url = message.substringAfter("--> GET ").substringBefore(" HTTP")
1323-
interceptedUrl = url.toHttpUrlOrNull()
1325+
val customLogger = object : CustomLogger {
1326+
override fun debug(logMessage: LogMessage) {
1327+
if(logMessage.type == LogMessageType.NETWORK_REQUEST){
1328+
val networkRequestDetails = logMessage.message as LogMessageContent.NetworkRequest
1329+
if (networkRequestDetails.path.contains("/v2/presence/") && networkRequestDetails.path.contains("/heartbeat")) {
1330+
interceptedUrl = (networkRequestDetails.origin + networkRequestDetails.path).toHttpUrlOrNull()
13241331
countDownLatch.countDown()
13251332
}
1326-
}.apply { level = HttpLoggingInterceptor.Level.BASIC }
1333+
}
1334+
}
1335+
}
1336+
1337+
clientConfig = {
1338+
customLoggers = listOf(customLogger)
1339+
heartbeatInterval = 5
13271340
}
13281341

13291342
try {
@@ -1354,27 +1367,30 @@ class SubscribeIntegrationTests : BaseIntegrationTest() {
13541367
}
13551368

13561369
@Test
1357-
fun `shouldDeduplicateChannelSubscriptionsWhenSubscribingToListOfTheSameChannels`() {
1370+
fun shouldDeduplicateChannelSubscriptionsWhenSubscribingToListOfTheSameChannels() {
13581371
// given
13591372
val countDownLatch = CountDownLatch(2) // Only two subscribe calls should occur. Handshake and actual subscribe.
13601373
var interceptedUrl: HttpUrl? = null
13611374
val testChannel = randomChannel()
13621375

1376+
val customLogger = object : CustomLogger {
1377+
override fun debug(logMessage: LogMessage) {
1378+
if(logMessage.type == LogMessageType.NETWORK_REQUEST){
1379+
val networkRequestDetails = logMessage.message as LogMessageContent.NetworkRequest
1380+
if (networkRequestDetails.path.contains("/v2/subscribe/")) {
1381+
interceptedUrl = (networkRequestDetails.origin + networkRequestDetails.path).toHttpUrlOrNull()
1382+
countDownLatch.countDown()
1383+
}
1384+
}
1385+
}
1386+
}
1387+
13631388
clientConfig = {
1364-
httpLoggingInterceptor =
1365-
HttpLoggingInterceptor { message ->
1366-
// Intercept subscribe GET request
1367-
if (message.startsWith("--> GET https://") && message.contains("/v2/subscribe/")) {
1368-
val url = message.substringAfter("--> GET ").substringBefore(" HTTP")
1369-
interceptedUrl = url.toHttpUrlOrNull()
1370-
countDownLatch.countDown()
1371-
}
1372-
}.apply { level = HttpLoggingInterceptor.Level.BASIC }
1389+
customLoggers = listOf(customLogger)
13731390
}
13741391

13751392
try {
13761393
pubnub.subscribe(channels = listOf(testChannel, testChannel, testChannel))
1377-
Thread.sleep(2000)
13781394

13791395
// Wait for the subscribe request to be made
13801396
assertTrue(countDownLatch.await(12000, TimeUnit.MILLISECONDS))
@@ -1388,9 +1404,10 @@ class SubscribeIntegrationTests : BaseIntegrationTest() {
13881404
// then: verify the actual HTTP request only includes the channel once
13891405
assertNotNull("Expected to intercept subscribe URL", interceptedUrl)
13901406

1391-
val channelsParam =
1392-
interceptedUrl!!.queryParameter("channel") ?: interceptedUrl!!.encodedPath.substringAfter("/subscribe/")
1393-
.substringAfter("/").substringBefore("/")
1407+
val channelsParam = interceptedUrl!!.encodedPath
1408+
.substringAfter("/subscribe/")
1409+
.substringAfter("/")
1410+
.substringBefore("/")
13941411

13951412
val channelList = channelsParam.split(",").filter { it.isNotEmpty() }
13961413

0 commit comments

Comments
 (0)