@@ -13,7 +13,6 @@ import (
1313 "crypto/rand"
1414 "fmt"
1515 "io"
16- "net"
1716 "testing"
1817)
1918
@@ -23,85 +22,37 @@ func makeRandByteSlice(size int) []byte {
2322 return randBytes
2423}
2524
26- func newMockConn () * mysqlConn {
27- newConn := & mysqlConn {cfg : NewConfig ()}
28- return newConn
29- }
30-
31- func newMockBuf (data []byte ) buffer {
32- return buffer {
33- buf : data ,
34- length : len (data ),
35- }
36- }
37-
38- type dummyConn struct {
39- buf bytes.Buffer
40- net.Conn
41- }
42-
43- func (c * dummyConn ) Write (data []byte ) (int , error ) {
44- return c .buf .Write (data )
45- }
46-
4725// compressHelper compresses uncompressedPacket and checks state variables
4826func compressHelper (t * testing.T , mc * mysqlConn , uncompressedPacket []byte ) []byte {
49- // get status variables
50-
51- cs := mc .compressSequence
52-
53- var b dummyConn
54- mc .netConn = & b
55- cw := newCompressor (mc )
56-
57- n , err := cw .Write (uncompressedPacket )
27+ conn := new (mockConn )
28+ mc .netConn = conn
5829
30+ n , err := mc .writeCompressed (uncompressedPacket )
5931 if err != nil {
60- t .Fatal (err . Error () )
32+ t .Fatal (err )
6133 }
62-
6334 if n != len (uncompressedPacket ) {
6435 t .Fatalf ("expected to write %d bytes, wrote %d bytes" , len (uncompressedPacket ), n )
6536 }
66-
67- if len (uncompressedPacket ) > 0 {
68- if mc .compressSequence != (cs + 1 ) {
69- t .Fatalf ("mc.compressionSequence updated incorrectly, expected %d and saw %d" , (cs + 1 ), mc .compressSequence )
70- }
71-
72- } else {
73- if mc .compressSequence != cs {
74- t .Fatalf ("mc.compressionSequence updated incorrectly for case of empty write, expected %d and saw %d" , cs , mc .compressSequence )
75- }
76- }
77-
78- return b .buf .Bytes ()
37+ return conn .written
7938}
8039
8140// uncompressHelper uncompresses compressedPacket and checks state variables
8241func uncompressHelper (t * testing.T , mc * mysqlConn , compressedPacket []byte , expSize int ) []byte {
83- // get status variables
84- cs := mc .compressSequence
85-
8642 // mocking out buf variable
87- mc .buf = newMockBuf (compressedPacket )
88- cr := newCompressor (mc )
43+ conn := new (mockConn )
44+ conn .data = compressedPacket
45+ mc .buf .nc = conn
46+ cr := newDecompressor (mc )
8947
9048 uncompressedPacket , err := cr .readNext (expSize )
9149 if err != nil {
9250 if err != io .EOF {
9351 t .Fatalf ("non-nil/non-EOF error when reading contents: %s" , err .Error ())
9452 }
9553 }
96-
97- if expSize > 0 {
98- if mc .compressSequence != (cs + 1 ) {
99- t .Fatalf ("mc.compressionSequence updated incorrectly, expected %d and saw %d" , (cs + 1 ), mc .compressSequence )
100- }
101- } else {
102- if mc .compressSequence != cs {
103- t .Fatalf ("mc.compressionSequence updated incorrectly for case of empty read, expected %d and saw %d" , cs , mc .compressSequence )
104- }
54+ if len (uncompressedPacket ) != expSize {
55+ t .Errorf ("uncompressed size is unexpected. expected %d but got %d" , expSize , len (uncompressedPacket ))
10556 }
10657 return uncompressedPacket
10758}
@@ -141,20 +92,33 @@ func TestRoundtrip(t *testing.T) {
14192 {uncompressed : makeRandByteSlice (32768 ),
14293 desc : "32768 rand bytes" ,
14394 },
144- {uncompressed : makeRandByteSlice (33000 ),
145- desc : "33000 rand bytes" ,
95+ {uncompressed : bytes . Repeat ( makeRandByteSlice (100 ), 10000 ),
96+ desc : "100 rand * 10000 repeat bytes" ,
14697 },
14798 }
14899
149- cSend := newMockConn ()
150- cReceive := newMockConn ()
100+ _ , cSend := newRWMockConn (0 )
101+ cSend .compress = true
102+ _ , cReceive := newRWMockConn (0 )
103+ cReceive .compress = true
151104
152105 for _ , test := range tests {
153106 s := fmt .Sprintf ("Test roundtrip with %s" , test .desc )
107+ cSend .resetSequenceNr ()
108+ cReceive .resetSequenceNr ()
154109
155110 uncompressed := roundtripHelper (t , cSend , cReceive , test .uncompressed )
156111 if ! bytes .Equal (uncompressed , test .uncompressed ) {
157112 t .Fatalf ("%s: roundtrip failed" , s )
158113 }
114+
115+ if cSend .sequence != cReceive .sequence {
116+ t .Errorf ("inconsistent sequence number: send=%v recv=%v" ,
117+ cSend .sequence , cReceive .sequence )
118+ }
119+ if cSend .compressSequence != cReceive .compressSequence {
120+ t .Errorf ("inconsistent compress sequence number: send=%v recv=%v" ,
121+ cSend .compressSequence , cReceive .compressSequence )
122+ }
159123 }
160124}
0 commit comments