Skip to content

Commit

Permalink
fragmented read buffer fixes & native buffer fix (#54)
Browse files Browse the repository at this point in the history
* fragmented read buffer fixes

* add 10s timeout to browser

* add more information for failed tests

* fix native write(buffer) issue
  • Loading branch information
thebehera authored Feb 6, 2024
1 parent 89988e6 commit 93cbeb9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
9 changes: 8 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ kotlin {
js(IR) {
moduleName = "buffer-kt"
browser { binaries.library() }
nodejs { binaries.library() }
nodejs {
binaries.library()
testTask {
useMocha {
timeout = "10s"
}
}
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions karma.config.d/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
config.client = config.client || {}
config.client.mocha = config.client.mocha || {}
config.client.mocha.timeout = 10000
config.browserNoActivityTimeout = 10000
config.browserDisconnectTimeout = 10000
40 changes: 25 additions & 15 deletions src/commonMain/kotlin/com/ditchoom/buffer/FragmentedReadBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ class FragmentedReadBuffer(
private val first: ReadBuffer,
private val second: ReadBuffer
) : ReadBuffer {
private val firstInitialPosition = first.position()
private val firstInitialLimit = first.limit()
private val secondInitialPosition = second.position()
private val secondInitialLimit = second.limit()
private var currentPosition = 0
private var currentLimit = firstInitialLimit + secondInitialLimit
Expand All @@ -37,8 +35,8 @@ class FragmentedReadBuffer(

override fun resetForRead() {
currentPosition = 0
first.position(firstInitialPosition)
second.position(secondInitialPosition)
first.resetForRead()
second.resetForRead()
}

override fun readByte(): Byte {
Expand Down Expand Up @@ -143,19 +141,31 @@ class FragmentedReadBuffer(
out += second
}
}
fun toSingleBuffer(zone: AllocationZone = AllocationZone.Heap): PlatformBuffer {
val firstLimit = firstInitialLimit
val secondLimit = secondInitialLimit
val initialPosition = position()
val buffer = PlatformBuffer.allocate(firstLimit + secondLimit - initialPosition, zone)
buffer.write(first)
buffer.write(second)
buffer.position(initialPosition)
return buffer
}
}

fun List<ReadBuffer>.toComposableBuffer(): ReadBuffer {
return when (size) {
1 -> {
first()
}

else -> {
FragmentedReadBuffer(
first(),
subList(1, size).toComposableBuffer()
)
}
if (isEmpty()) {
return ReadBuffer.EMPTY_BUFFER
}
if (size == 1) {
return first()
}
if (size == 2) {
return FragmentedReadBuffer(first(), get(1))
}
val half = size / 2
return FragmentedReadBuffer(
subList(0, half).toComposableBuffer(),
subList(half, size).toComposableBuffer()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,30 @@ class FragmentedReadBufferTests {
assertEquals("", composableBuffer.readUtf8Line().toString())
assertTrue { composableBuffer.remaining() == 0 }
}

@Test
fun largeFragmentedBuffer() {
val buffers = mutableListOf<ReadBuffer>()
var indexCount = 0
do { // 64 byte chunks
val buffer = PlatformBuffer.allocate(64)
repeat(64 / 4) {
buffer.writeInt(indexCount++)
}
buffers += buffer
} while (indexCount < 1024 * 1024)
val fragmentedBuffer = buffers.toComposableBuffer() as FragmentedReadBuffer
fragmentedBuffer.resetForRead()
repeat(indexCount) {
assertEquals(it, fragmentedBuffer.readInt(), "failed to read byte on fragmented at $indexCount")
}
assertEquals(0, fragmentedBuffer.remaining(), "fragmented should have 0 remaining")
fragmentedBuffer.resetForRead()
val combined = fragmentedBuffer.toSingleBuffer()
assertEquals(indexCount * 4, combined.remaining(), "failed to validate remaining")
repeat(indexCount) {
assertEquals(it, combined.readInt(), "failed to read byte on combined at $indexCount")
}
assertEquals(0, combined.remaining(), "combined should have 0 remaining")
}
}
2 changes: 1 addition & 1 deletion src/nativeMain/kotlin/com/ditchoom/buffer/NativeBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ data class NativeBuffer(
writeBytes(buffer.data)
buffer.data.size
} else {
val numBytes = remaining()
val numBytes = buffer.remaining()
writeBytes(buffer.readByteArray(numBytes))
numBytes
}
Expand Down

0 comments on commit 93cbeb9

Please sign in to comment.