Skip to content

Commit

Permalink
Remove kotlinx.datetime dependency (#123)
Browse files Browse the repository at this point in the history
* Remove kotlinx.datetime dependency

* Add 1s delays in tests before history and message counts

* Add verbose test logging
  • Loading branch information
wkal-pubnub authored Nov 30, 2024
1 parent b88a242 commit 3052402
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
${{ runner.os }}-gradle-
- name: Build and run tests
run: |
./gradlew check
./gradlew check -i
env:
SDK_PUB_KEY: ${{ secrets.SDK_PUB_KEY }}
SDK_SUB_KEY: ${{ secrets.SDK_SUB_KEY }}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ kotlin {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation("com.pubnub:pubnub-kotlin-test")
implementation(libs.pubnub.kotlin.test)
}
}
}
Expand Down
10 changes: 1 addition & 9 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@ kotlin = "2.0.21"
vanniktech = "0.29.0"
ktlint = "12.1.0"
dokka = "1.9.20"
kotlinx_datetime = "0.6.0"
kotlinx_coroutines = "1.8.1"
kotlinx_serialization = "1.7.1"
pubnub = "10.1.0"
pubnub = "10.2.0"

[libraries]
pubnub-kotlin-api = { module = "com.pubnub:pubnub-kotlin-api", version.ref = "pubnub" }
pubnub-kotlin-impl = { module = "com.pubnub:pubnub-kotlin-impl", version.ref = "pubnub" }
pubnub-kotlin = { module = "com.pubnub:pubnub-kotlin", version.ref = "pubnub" }
pubnub-kotlin-test = { module = "com.pubnub:pubnub-kotlin-test", version.ref = "pubnub" }
#slf4j = { module = "org.slf4j:slf4j-api", version = "1.7.36" }
#cbor = { module = "co.nstant.in:cbor", version = "0.9" }
#jetbrains-annotations = { module = "org.jetbrains:annotations", version = "24.1.0" }
kotlinx-atomicfu = { module = "org.jetbrains.kotlinx:atomicfu", version = "0.25.0" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx_serialization" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx_datetime"}
touchlab-kermit = { module = "co.touchlab:kermit", version = "2.0.4" }
diff-match-patch = { module = "org.bitbucket.cowwoc:diff-match-patch", version = "1.2" }

## tests
#logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ public final class name/fraser/neil/plaintext/DiffMatchPatch {
public fun <init> ()V
public fun <init> (FSFIFS)V
public synthetic fun <init> (FSFIFSILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun diff_bisect (Ljava/lang/String;Ljava/lang/String;J)Ljava/util/List;
public final fun diff_bisect-UpJHN0k (Ljava/lang/String;Ljava/lang/String;J)Ljava/util/List;
public final fun diff_charsToLines (Ljava/util/List;Ljava/util/List;)V
public final fun diff_cleanupEfficiency (Ljava/util/List;)V
public final fun diff_cleanupMerge (Ljava/util/List;)V
Expand Down
5 changes: 0 additions & 5 deletions pubnub-3p-diff-match-patch/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ mavenPublishing {

kotlin {
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")
}
}
commonTest {
dependencies {
implementation(kotlin("test"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
*/
package name.fraser.neil.plaintext

import kotlinx.datetime.Clock
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.TimeSource

/*
* Functions for diff, match and patch.
Expand Down Expand Up @@ -123,11 +125,11 @@ class DiffMatchPatch(
checklines: Boolean = true,
): MutableList<Diff> { // was LinkedList
// Set a deadline by which time the diff must be complete.
val deadline: Long
val deadline: TimeSource.Monotonic.ValueTimeMark
if (diffTimeout <= 0) {
deadline = Long.MAX_VALUE
deadline = TimeSource.Monotonic.markNow() + Duration.INFINITE
} else {
deadline = Clock.System.now().toEpochMilliseconds() + (diffTimeout * 1000).toLong()
deadline = TimeSource.Monotonic.markNow() + (diffTimeout * 1000).toLong().milliseconds
}
return diff_main(text1, text2, checklines, deadline)
}
Expand All @@ -140,13 +142,15 @@ class DiffMatchPatch(
* @param checklines Speedup flag. If false, then don't run a
* line-level diff first to identify the changed areas.
* If true, then run a faster slightly less optimal diff.
* @param deadline Time when the diff should be complete by. Used
* @param deadline When the computation started. Used
* internally for recursive calls. Users should set DiffTimeout instead.
* @return Linked List of Diff objects.
*/
private fun diff_main(
text1: String, text2: String,
checklines: Boolean, deadline: Long,
text1: String,
text2: String,
checklines: Boolean,
deadline: TimeSource.Monotonic.ValueTimeMark,
): MutableList<Diff> { // was LinkedList
// Check for null inputs.
var text1 = text1
Expand Down Expand Up @@ -197,12 +201,14 @@ class DiffMatchPatch(
* @param checklines Speedup flag. If false, then don't run a
* line-level diff first to identify the changed areas.
* If true, then run a faster slightly less optimal diff.
* @param deadline Time when the diff should be complete by.
* @param deadline When the computation started.
* @return Linked List of Diff objects.
*/
private fun diff_compute(
text1: String, text2: String,
checklines: Boolean, deadline: Long,
text1: String,
text2: String,
checklines: Boolean,
deadline: TimeSource.Monotonic.ValueTimeMark,
): MutableList<Diff> { // was LinkedList
var diffs = mutableListOf<Diff>() // was LinkedList

Expand Down Expand Up @@ -274,12 +280,13 @@ class DiffMatchPatch(
* This speedup can produce non-minimal diffs.
* @param text1 Old string to be diffed.
* @param text2 New string to be diffed.
* @param deadline Time when the diff should be complete by.
* @param deadline When the computation started.
* @return Linked List of Diff objects.
*/
private fun diff_lineMode(
text1: String, text2: String,
deadline: Long,
text1: String,
text2: String,
deadline: TimeSource.Monotonic.ValueTimeMark,
): MutableList<Diff> { // was LinkedList
// Scan the text on a line-by-line basis first.
var text1 = text1
Expand Down Expand Up @@ -353,12 +360,13 @@ class DiffMatchPatch(
* See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
* @param text1 Old string to be diffed.
* @param text2 New string to be diffed.
* @param deadline Time at which to bail if not yet complete.
* @param deadline When the computation started.
* @return LinkedList of Diff objects.
*/
fun diff_bisect(
text1: String, text2: String,
deadline: Long,
text1: String,
text2: String,
deadline: TimeSource.Monotonic.ValueTimeMark,
): MutableList<Diff> { // was LinkedList
// Cache the text lengths to prevent multiple calls.
val text1_length = text1.length
Expand Down Expand Up @@ -386,7 +394,7 @@ class DiffMatchPatch(
var k2end = 0
for (d in 0 until max_d) {
// Bail out if deadline is reached.
if (Clock.System.now().toEpochMilliseconds() > deadline) {
if (deadline.hasPassedNow()) {
break
}

Expand Down Expand Up @@ -478,12 +486,14 @@ class DiffMatchPatch(
* @param text2 New string to be diffed.
* @param x Index of split point in text1.
* @param y Index of split point in text2.
* @param deadline Time at which to bail if not yet complete.
* @param deadline When the computation started.
* @return LinkedList of Diff objects.
*/
private fun diff_bisectSplit(
text1: String, text2: String,
x: Int, y: Int, deadline: Long,
text1: String,
text2: String,
x: Int, y: Int,
deadline: TimeSource.Monotonic.ValueTimeMark,
): MutableList<Diff> { // was LinkedList
val text1a: String = text1.substring(0, x)
val text2a: String = text2.substring(0, y)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
*/
package name.fraser.neil.plaintext

import kotlinx.datetime.Clock
import name.fraser.neil.plaintext.DiffMatchPatch.LinesToCharsResult
import kotlin.test.Test
import kotlin.time.Duration
import kotlin.time.TimeSource
import kotlin.time.measureTime

class DiffMatchPatchTest {
private val dmp: DiffMatchPatch = DiffMatchPatch()
Expand Down Expand Up @@ -851,11 +853,11 @@ class DiffMatchPatchTest {
EQUAL, "a"
), DiffMatchPatch.Diff(DELETE, "t"), DiffMatchPatch.Diff(INSERT, "p")
)
assertEquals("diff_bisect: Normal.", diffs, dmp.diff_bisect(a, b, Long.MAX_VALUE))
assertEquals("diff_bisect: Normal.", diffs, dmp.diff_bisect(a, b, TimeSource.Monotonic.markNow() + Duration.INFINITE))

// Timeout.
diffs = diffList(DiffMatchPatch.Diff(DELETE, "cat"), DiffMatchPatch.Diff(INSERT, "map"))
assertEquals("diff_bisect: Timeout.", diffs, dmp.diff_bisect(a, b, 0))
assertEquals("diff_bisect: Timeout.", diffs, dmp.diff_bisect(a, b, TimeSource.Monotonic.markNow() - Duration.INFINITE))
}

@Test
Expand Down Expand Up @@ -979,15 +981,15 @@ class DiffMatchPatchTest {
a += a
b += b
}
val startTime = Clock.System.now().toEpochMilliseconds()
dmp.diff_main(a, b)
val endTime = Clock.System.now().toEpochMilliseconds()
val time = measureTime {
dmp.diff_main(a, b)
}
// Test that we took at least the timeout period.
assertTrue("diff_main: Timeout min.", dmp.diffTimeout * 1000 <= endTime - startTime)
assertTrue("diff_main: Timeout min.", dmp.diffTimeout * 1000 <= time.inWholeMilliseconds)
// Test that we didn't take forever (be forgiving).
// Theoretically this test could fail very occasionally if the
// OS task swaps or locks up for a second at the wrong moment.
assertTrue("diff_main: Timeout max.", dmp.diffTimeout * 1000 * 2 > endTime - startTime)
assertTrue("diff_main: Timeout max.", dmp.diffTimeout * 1000 * 2 > time.inWholeMilliseconds)
dmp.diffTimeout = 0f

// Test the linemode speedup.
Expand Down
1 change: 0 additions & 1 deletion pubnub-chat-impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ kotlin {
implementation(project(":pubnub-chat-api"))
implementation(project(":pubnub-3p-diff-match-patch"))
implementation(libs.pubnub.kotlin.api)
implementation(libs.kotlinx.datetime)
implementation(libs.kotlinx.serialization.core)
implementation(libs.kotlinx.atomicfu)
implementation(libs.touchlab.kermit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import com.pubnub.api.models.consumer.pubsub.PNEvent
import com.pubnub.api.models.consumer.push.PNPushAddChannelResult
import com.pubnub.api.models.consumer.push.PNPushListProvisionsResult
import com.pubnub.api.models.consumer.push.PNPushRemoveChannelResult
import com.pubnub.api.utils.Clock
import com.pubnub.api.utils.Instant
import com.pubnub.api.v2.callbacks.Result
import com.pubnub.chat.Channel
import com.pubnub.chat.Chat
Expand Down Expand Up @@ -109,8 +111,6 @@ import com.pubnub.kmp.createEventListener
import com.pubnub.kmp.then
import com.pubnub.kmp.thenAsync
import encodeForSending
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.reflect.KClass
import kotlin.time.Duration.Companion.seconds

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.pubnub.api.models.consumer.objects.membership.PNChannelMembershipArra
import com.pubnub.api.models.consumer.objects.uuid.PNUUIDMetadata
import com.pubnub.api.models.consumer.pubsub.objects.PNDeleteUUIDMetadataEventMessage
import com.pubnub.api.models.consumer.pubsub.objects.PNSetUUIDMetadataEventMessage
import com.pubnub.api.utils.Clock
import com.pubnub.api.utils.Instant
import com.pubnub.api.utils.PatchValue
import com.pubnub.api.v2.callbacks.Result
import com.pubnub.chat.Channel
Expand All @@ -31,8 +33,6 @@ import com.pubnub.kmp.asFuture
import com.pubnub.kmp.catch
import com.pubnub.kmp.createEventListener
import com.pubnub.kmp.then
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import tryLong

data class UserImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.pubnub.api.models.consumer.pubsub.objects.PNDeleteChannelMetadataEven
import com.pubnub.api.models.consumer.pubsub.objects.PNObjectEventResult
import com.pubnub.api.models.consumer.pubsub.objects.PNSetChannelMetadataEventMessage
import com.pubnub.api.models.consumer.push.payload.PushPayloadHelper
import com.pubnub.api.utils.Clock
import com.pubnub.api.utils.Instant
import com.pubnub.api.utils.PatchValue
import com.pubnub.api.v2.callbacks.Result
import com.pubnub.api.v2.subscriptions.SubscriptionOptions
Expand Down Expand Up @@ -95,8 +97,6 @@ import encodeForSending
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.locks.reentrantLock
import kotlinx.atomicfu.locks.withLock
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import tryLong
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.pubnub.chat.internal.channel

import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata
import com.pubnub.api.utils.Clock
import com.pubnub.chat.Channel
import com.pubnub.chat.Message
import com.pubnub.chat.internal.ChatInternal
import com.pubnub.chat.internal.DELETED
import com.pubnub.chat.internal.message.MessageImpl
import com.pubnub.chat.types.ChannelType
import kotlinx.datetime.Clock

data class ChannelImpl(
override val chat: ChatInternal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import co.touchlab.kermit.Logger
import com.pubnub.api.models.consumer.PNPublishResult
import com.pubnub.api.models.consumer.message_actions.PNMessageAction
import com.pubnub.api.models.consumer.objects.channel.PNChannelMetadata
import com.pubnub.api.utils.Clock
import com.pubnub.chat.Channel
import com.pubnub.chat.Message
import com.pubnub.chat.ThreadChannel
Expand All @@ -25,7 +26,6 @@ import com.pubnub.kmp.asFuture
import com.pubnub.kmp.awaitAll
import com.pubnub.kmp.then
import com.pubnub.kmp.thenAsync
import kotlinx.datetime.Clock

data class ThreadChannelImpl(
override val parentMessage: Message,
Expand Down
Loading

0 comments on commit 3052402

Please sign in to comment.