Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package com.github.jasync.sql.db.mysql.codec

import io.netty.buffer.ByteBuf
import io.netty.buffer.ByteBufAllocator
import io.netty.buffer.UnpooledByteBufAllocator
import io.netty.buffer.PooledByteBufAllocator
import java.nio.ByteOrder

/**
* Allocates ByteBuf which have LITTLE_ENDIAN order.
*/
class LittleEndianByteBufAllocator(private val allocator: UnpooledByteBufAllocator = UnpooledByteBufAllocator(false)) :
class LittleEndianByteBufAllocator(private val allocator: ByteBufAllocator = PooledByteBufAllocator.DEFAULT) :
ByteBufAllocator by allocator {

companion object {
val INSTANCE = LittleEndianByteBufAllocator()
}

override fun isDirectBufferPooled(): Boolean = false
override fun isDirectBufferPooled(): Boolean = allocator.isDirectBufferPooled

override fun buffer() = littleEndian(allocator.buffer())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class MySQLConnectionHandler(
})

this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true)
this.bootstrap.option(ChannelOption.ALLOCATOR, LittleEndianByteBufAllocator.INSTANCE)
this.bootstrap.option(ChannelOption.ALLOCATOR, LittleEndianByteBufAllocator(this.configuration.allocator))
this.bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.configuration.connectionTimeout)

val channelFuture: ChannelFuture = this.bootstrap.connect(socketChannelAddress.socketAddress)
Expand Down Expand Up @@ -152,28 +152,27 @@ class MySQLConnectionHandler(
}
ServerMessage.Row -> {
val rsrMessage = message as ResultSetRowMessage
val items = Array(rsrMessage.size) {
if (rsrMessage[it] == null) {
null
} else {
if (this.currentQuery == null) {
throw NullPointerException("currentQuery is null")
}
val columnDescription: ColumnDefinitionMessage = this.currentQuery!!.columnTypes[it]
val buf = rsrMessage[it]!!
try {
try {
val query = this.currentQuery ?: throw NullPointerException("currentQuery is null")
val items = Array(rsrMessage.size) {
val buf = rsrMessage[it]
if (buf == null) {
null
} else {
val columnDescription: ColumnDefinitionMessage = query.columnTypes[it]
columnDescription.textDecoder.decode(
columnDescription,
buf,
configuration.charset
)
} finally {
buf.release()
}
}
query.addRow(items)
} finally {
for (buf in rsrMessage) {
buf?.release()
}
}

this.currentQuery!!.addRow(items)
}
ServerMessage.BinaryRow -> {
val m = message as BinaryRowMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.github.jasync.sql.db.mysql.codec

import com.github.jasync.sql.db.Configuration
import com.github.jasync.sql.db.column.ColumnDecoder
import com.github.jasync.sql.db.general.ColumnData
import com.github.jasync.sql.db.general.MutableResultSet
import com.github.jasync.sql.db.mysql.binary.decoder.ByteArrayDecoder
import com.github.jasync.sql.db.mysql.column.ColumnTypes
import com.github.jasync.sql.db.mysql.message.server.ColumnDefinitionMessage
import com.github.jasync.sql.db.mysql.message.server.ResultSetRowMessage
import com.github.jasync.sql.db.mysql.util.CharsetMapper
import io.mockk.mockk
import io.netty.buffer.ByteBuf
import io.netty.buffer.PooledByteBufAllocator
import io.netty.buffer.Unpooled
import io.netty.buffer.UnpooledByteBufAllocator
import io.netty.channel.DefaultEventLoopGroup
import io.netty.channel.embedded.EmbeddedChannel
import org.junit.Test
import java.nio.ByteOrder
import java.nio.charset.Charset
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class LittleEndianByteBufAllocatorSpec {

@Test
@Suppress("DEPRECATION")
fun `defaults to PooledByteBufAllocator and allocates little endian buffers`() {
val allocator = LittleEndianByteBufAllocator()
assertEquals(PooledByteBufAllocator.DEFAULT.isDirectBufferPooled, allocator.isDirectBufferPooled)

val heapBuf = allocator.heapBuffer(16)
val directBuf = allocator.directBuffer(16)
val ioBuf = allocator.ioBuffer(16)
try {
assertEquals(ByteOrder.LITTLE_ENDIAN, heapBuf.order())
assertEquals(ByteOrder.LITTLE_ENDIAN, directBuf.order())
assertEquals(ByteOrder.LITTLE_ENDIAN, ioBuf.order())
} finally {
heapBuf.release()
directBuf.release()
ioBuf.release()
}
}

@Test
fun `delegates isDirectBufferPooled to custom allocator`() {
val unpooled = LittleEndianByteBufAllocator(UnpooledByteBufAllocator(false))
assertFalse(unpooled.isDirectBufferPooled)
}

@Test
fun `releases all ResultSetRowMessage column slices when a column decoder throws`() {
val failingDecoder = object : ColumnDecoder {
override fun decode(kind: ColumnData, value: ByteBuf, charset: Charset): Any {
throw IllegalArgumentException("decode failure on col 0")
}

override fun decode(value: String): Any {
throw IllegalArgumentException("decode failure")
}
}
val col0 = ColumnDefinitionMessage(
"bad", "db", "t", "t", "c0", "c0",
CharsetMapper.Binary, 10, ColumnTypes.FIELD_TYPE_VARCHAR, 0, 0,
ByteArrayDecoder, failingDecoder
)
val col1 = ColumnDefinitionMessage(
"ok", "db", "t", "t", "c1", "c1",
CharsetMapper.Binary, 10, ColumnTypes.FIELD_TYPE_VARCHAR, 0, 0,
ByteArrayDecoder, failingDecoder
)

val group = DefaultEventLoopGroup(1)
try {
var capturedException: Throwable? = null
val delegate = object : MySQLHandlerDelegate by mockk(relaxed = true) {
override fun exceptionCaught(exception: Throwable) {
capturedException = exception
}
}
val handler = MySQLConnectionHandler(
Configuration("root"),
CharsetMapper.Instance,
delegate,
group,
connectionId = "test-conn"
)
val currentQueryField = MySQLConnectionHandler::class.java.getDeclaredField("currentQuery").apply {
isAccessible = true
}
currentQueryField.set(handler, MutableResultSet(listOf(col0, col1)))

val channel = EmbeddedChannel(handler)
val slice0 = Unpooled.copiedBuffer("abc", Charsets.UTF_8)
val slice1 = Unpooled.copiedBuffer("def", Charsets.UTF_8)
val row = ResultSetRowMessage().apply {
add(slice0)
add(slice1)
}

channel.writeInbound(row)

assertNotNull(capturedException)
assertTrue(capturedException is IllegalArgumentException)
assertEquals(0, slice0.refCnt())
assertEquals(0, slice1.refCnt())
channel.finishAndReleaseAll()
} finally {
group.shutdownGracefully().sync()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class MySQLFrameDecoderSpec {

embedder.writeInbound(row)

embedder.readInbound() as ResultSetRowMessage
val rowMessage = embedder.readInbound() as ResultSetRowMessage
rowMessage.forEach { it?.release() }

embedder.writeInbound(this.createEOFPacket())

Expand Down
Loading