From 77d86eca95188317199e8b3ac0c6a039343addd6 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 15 Mar 2024 16:17:38 +0000 Subject: [PATCH] fix tests --- .github/workflows/test.yml | 2 +- compress.go | 22 ++++++++++++++-------- driver_test.go | 3 ++- packets.go | 22 ++++++++-------------- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c5b2aa313..ea8a972b3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -83,7 +83,7 @@ jobs: my-cnf: | innodb_log_file_size=256MB innodb_buffer_pool_size=512MB - max_allowed_packet=16MB + max_allowed_packet=48MB ; TestConcurrent fails if max_connections is too large max_connections=50 local_infile=1 diff --git a/compress.go b/compress.go index dba660555..2d2bea0c3 100644 --- a/compress.go +++ b/compress.go @@ -13,7 +13,6 @@ import ( "compress/zlib" "fmt" "io" - "os" "sync" ) @@ -124,13 +123,20 @@ func (c *decompressor) uncompressPacket() error { uncompressedLength := int(uint32(header[4]) | uint32(header[5])<<8 | uint32(header[6])<<16) compressionSequence := uint8(header[3]) if debugTrace { - fmt.Fprintf(os.Stderr, "uncompress cmplen=%v uncomplen=%v seq=%v\n", - comprLength, uncompressedLength, compressionSequence) - } - if compressionSequence != c.mc.compressSequence { - return ErrPktSync - } - c.mc.compressSequence++ + c.mc.cfg.Logger.Print( + fmt.Sprintf("uncompress cmplen=%v uncomplen=%v pkt_cmp_seq=%v expected_cmp_seq=%v\n", + comprLength, uncompressedLength, compressionSequence, c.mc.sequence)) + } + if compressionSequence != c.mc.sequence { + // return ErrPktSync + // server may return error packet (e.g. 1153 Got a packet bigger than 'max_allowed_packet' bytes) + // before receiving all packets from client. In this case, seqnr is younger than expected. + c.mc.cfg.Logger.Print( + fmt.Sprintf("[warn] unexpected cmpress seq nr: expected %v, got %v", + c.mc.sequence, compressionSequence)) + } + c.mc.sequence = compressionSequence + 1 + c.mc.compressSequence = c.mc.sequence comprData, err := c.mc.buf.readNext(comprLength) if err != nil { diff --git a/driver_test.go b/driver_test.go index 86f162b1e..3f80e7415 100644 --- a/driver_test.go +++ b/driver_test.go @@ -1277,7 +1277,8 @@ func TestLongData(t *testing.T) { var rows *sql.Rows // Long text data - const nonDataQueryLen = 28 // length query w/o value + // const nonDataQueryLen = 28 // length query w/o value + compress header + const nonDataQueryLen = 100 inS := in[:maxAllowedPacketSize-nonDataQueryLen] dbt.mustExec("INSERT INTO test VALUES('" + inS + "')") rows = dbt.mustQuery("SELECT value FROM test") diff --git a/packets.go b/packets.go index dfe3820d1..a2d5a7746 100644 --- a/packets.go +++ b/packets.go @@ -35,7 +35,9 @@ func (mc *mysqlConn) readPacket() ([]byte, error) { if cerr := mc.canceled.Value(); cerr != nil { return nil, cerr } - // debug.PrintStack() + if debugTrace { + debug.PrintStack() + } mc.cfg.Logger.Print(err) mc.Close() return nil, ErrInvalidConn @@ -43,22 +45,14 @@ func (mc *mysqlConn) readPacket() ([]byte, error) { // packet length [24 bit] pktLen := int(uint32(data[0]) | uint32(data[1])<<8 | uint32(data[2])<<16) - if debugTrace { - mc.cfg.Logger.Print(fmt.Sprintf("readPacket: packet seq = %d, mc.sequence = %d", data[3], mc.sequence)) - } - // check packet sync [8 bit] - if data[3] != mc.sequence { - if debugTrace { - debug.PrintStack() + if !mc.compress { // MySQL and MariaDB doesn't check packet nr in compressed packet. + // check packet sync [8 bit] + if data[3] != mc.sequence { + mc.cfg.Logger.Print(fmt.Sprintf("[warn] unexpected seq nr: expected %v, got %v", mc.sequence, data[3])) } - mc.Close() - if data[3] > mc.sequence { - return nil, ErrPktSyncMul - } - return nil, ErrPktSync + mc.sequence++ } - mc.sequence++ // packets with length 0 terminate a previous packet which is a // multiple of (2^24)-1 bytes long