@@ -11,7 +11,6 @@ package mysql
1111import (
1212 "bytes"
1313 "crypto/rand"
14- "fmt"
1514 "io"
1615 "testing"
1716)
@@ -26,15 +25,12 @@ func makeRandByteSlice(size int) []byte {
2625func compressHelper (t * testing.T , mc * mysqlConn , uncompressedPacket []byte ) []byte {
2726 conn := new (mockConn )
2827 mc .netConn = conn
29- comp := newCompIO (mc )
3028
31- n , err := comp . writePackets ( uncompressedPacket )
29+ err := mc . writePacket ( append ( make ([] byte , 4 ), uncompressedPacket ... ) )
3230 if err != nil {
3331 t .Fatal (err )
3432 }
35- if n != len (uncompressedPacket ) {
36- t .Fatalf ("expected to write %d bytes, wrote %d bytes" , len (uncompressedPacket ), n )
37- }
33+
3834 return conn .written
3935}
4036
@@ -43,10 +39,9 @@ func uncompressHelper(t *testing.T, mc *mysqlConn, compressedPacket []byte, expS
4339 // mocking out buf variable
4440 conn := new (mockConn )
4541 conn .data = compressedPacket
46- mc .buf .nc = conn
47- cr := newCompIO (mc )
42+ mc .netConn = conn
4843
49- uncompressedPacket , err := cr . readNext ( expSize )
44+ uncompressedPacket , err := mc . readPacket ( )
5045 if err != nil {
5146 if err != io .EOF {
5247 t .Fatalf ("non-nil/non-EOF error when reading contents: %s" , err .Error ())
@@ -72,8 +67,6 @@ func TestRoundtrip(t *testing.T) {
7267 }{
7368 {uncompressed : []byte ("a" ),
7469 desc : "a" },
75- {uncompressed : []byte {0 },
76- desc : "0 byte" },
7770 {uncompressed : []byte ("hello world" ),
7871 desc : "hello world" },
7972 {uncompressed : make ([]byte , 100 ),
@@ -82,8 +75,6 @@ func TestRoundtrip(t *testing.T) {
8275 desc : "32768 bytes" },
8376 {uncompressed : make ([]byte , 330000 ),
8477 desc : "33000 bytes" },
85- {uncompressed : make ([]byte , 0 ),
86- desc : "nothing" },
8778 {uncompressed : makeRandByteSlice (10 ),
8879 desc : "10 rand bytes" ,
8980 },
@@ -100,26 +91,29 @@ func TestRoundtrip(t *testing.T) {
10091
10192 _ , cSend := newRWMockConn (0 )
10293 cSend .compress = true
94+ cSend .compIO = newCompIO (cSend )
10395 _ , cReceive := newRWMockConn (0 )
10496 cReceive .compress = true
97+ cReceive .compIO = newCompIO (cReceive )
10598
10699 for _ , test := range tests {
107- s := fmt . Sprintf ( "Test roundtrip with %s" , test .desc )
108- cSend .resetSequenceNr ()
109- cReceive .resetSequenceNr ()
100+ t . Run ( test .desc , func ( t * testing. T ) {
101+ cSend .resetSequenceNr ()
102+ cReceive .resetSequenceNr ()
110103
111- uncompressed := roundtripHelper (t , cSend , cReceive , test .uncompressed )
112- if ! bytes .Equal (uncompressed , test .uncompressed ) {
113- t .Fatalf ("%s: roundtrip failed" , s )
114- }
104+ uncompressed := roundtripHelper (t , cSend , cReceive , test .uncompressed )
105+ if ! bytes .Equal (uncompressed , test .uncompressed ) {
106+ t .Fatalf ("roundtrip failed" )
107+ }
115108
116- if cSend .sequence != cReceive .sequence {
117- t .Errorf ("inconsistent sequence number: send=%v recv=%v" ,
118- cSend .sequence , cReceive .sequence )
119- }
120- if cSend .compressSequence != cReceive .compressSequence {
121- t .Errorf ("inconsistent compress sequence number: send=%v recv=%v" ,
122- cSend .compressSequence , cReceive .compressSequence )
123- }
109+ if cSend .sequence != cReceive .sequence {
110+ t .Errorf ("inconsistent sequence number: send=%v recv=%v" ,
111+ cSend .sequence , cReceive .sequence )
112+ }
113+ if cSend .compressSequence != cReceive .compressSequence {
114+ t .Errorf ("inconsistent compress sequence number: send=%v recv=%v" ,
115+ cSend .compressSequence , cReceive .compressSequence )
116+ }
117+ })
124118 }
125119}
0 commit comments