Skip to content

Commit 1c6e980

Browse files
committed
re-implement TestReadPacketSplit
1 parent 1eaf0c0 commit 1c6e980

File tree

1 file changed

+121
-91
lines changed

1 file changed

+121
-91
lines changed

packets_test.go

+121-91
Original file line numberDiff line numberDiff line change
@@ -91,110 +91,140 @@ func TestReadPacketWrongSequenceID(t *testing.T) {
9191
}
9292
}
9393

94-
// func TestReadPacketSplit(t *testing.T) {
95-
// conn := new(mockConn)
96-
// mc := &mysqlConn{
97-
// buf: newBuffer(conn),
98-
// }
94+
func TestReadPacketSplit(t *testing.T) {
95+
const pkt2ofs = maxPacketSize + 4
96+
const pkt3ofs = 2 * (maxPacketSize + 4)
9997

100-
// data := make([]byte, maxPacketSize*2+4*3)
101-
// const pkt2ofs = maxPacketSize + 4
102-
// const pkt3ofs = 2 * (maxPacketSize + 4)
98+
t.Run("case 1: payload has length maxPacketSize", func(t *testing.T) {
99+
conn, mc := newRWMockConn(t, 0)
100+
data := make([]byte, pkt2ofs+4)
103101

104-
// // case 1: payload has length maxPacketSize
105-
// data = data[:pkt2ofs+4]
102+
// 1st packet has maxPacketSize length and sequence id 0
103+
// ff ff ff 00 ...
104+
data[0] = 0xff
105+
data[1] = 0xff
106+
data[2] = 0xff
106107

107-
// // 1st packet has maxPacketSize length and sequence id 0
108-
// // ff ff ff 00 ...
109-
// data[0] = 0xff
110-
// data[1] = 0xff
111-
// data[2] = 0xff
108+
// mark the payload start and end of 1st packet so that we can check if the
109+
// content was correctly appended
110+
data[4] = 0x11
111+
data[maxPacketSize+3] = 0x22
112112

113-
// // mark the payload start and end of 1st packet so that we can check if the
114-
// // content was correctly appended
115-
// data[4] = 0x11
116-
// data[maxPacketSize+3] = 0x22
113+
// 2nd packet has payload length 0 and sequence id 1
114+
// 00 00 00 01
115+
data[pkt2ofs+3] = 0x01
117116

118-
// // 2nd packet has payload length 0 and sequence id 1
119-
// // 00 00 00 01
120-
// data[pkt2ofs+3] = 0x01
117+
go func() {
118+
conn.Write(data)
119+
}()
120+
// TODO: check read operation count
121121

122-
// conn.data = data
123-
// conn.maxReads = 3
124-
// packet, err := mc.readPacket()
125-
// if err != nil {
126-
// t.Fatal(err)
127-
// }
128-
// if len(packet) != maxPacketSize {
129-
// t.Fatalf("unexpected packet length: expected %d, got %d", maxPacketSize, len(packet))
130-
// }
131-
// if packet[0] != 0x11 {
132-
// t.Fatalf("unexpected payload start: expected %x, got %x", 0x11, packet[0])
133-
// }
134-
// if packet[maxPacketSize-1] != 0x22 {
135-
// t.Fatalf("unexpected payload end: expected %x, got %x", 0x22, packet[maxPacketSize-1])
136-
// }
122+
packet, err := mc.readPacket(context.Background())
123+
if err != nil {
124+
t.Fatal(err)
125+
}
126+
if len(packet) != maxPacketSize {
127+
t.Fatalf("unexpected packet length: expected %d, got %d", maxPacketSize, len(packet))
128+
}
129+
if packet[0] != 0x11 {
130+
t.Fatalf("unexpected payload start: expected %x, got %x", 0x11, packet[0])
131+
}
132+
if packet[maxPacketSize-1] != 0x22 {
133+
t.Fatalf("unexpected payload end: expected %x, got %x", 0x22, packet[maxPacketSize-1])
134+
}
135+
})
137136

138-
// // case 2: payload has length which is a multiple of maxPacketSize
139-
// data = data[:cap(data)]
137+
t.Run("case 2: payload has length which is a multiple of maxPacketSize", func(t *testing.T) {
138+
conn, mc := newRWMockConn(t, 0)
139+
data := make([]byte, maxPacketSize*2+4*3)
140140

141-
// // 2nd packet now has maxPacketSize length
142-
// data[pkt2ofs] = 0xff
143-
// data[pkt2ofs+1] = 0xff
144-
// data[pkt2ofs+2] = 0xff
141+
// 1st packet has maxPacketSize length and sequence id 0
142+
// ff ff ff 00 ...
143+
data[0] = 0xff
144+
data[1] = 0xff
145+
data[2] = 0xff
145146

146-
// // mark the payload start and end of the 2nd packet
147-
// data[pkt2ofs+4] = 0x33
148-
// data[pkt2ofs+maxPacketSize+3] = 0x44
147+
// mark the payload start and end of 1st packet so that we can check if the
148+
// content was correctly appended
149+
data[4] = 0x11
150+
data[maxPacketSize+3] = 0x22
149151

150-
// // 3rd packet has payload length 0 and sequence id 2
151-
// // 00 00 00 02
152-
// data[pkt3ofs+3] = 0x02
152+
// 2nd packet now has maxPacketSize length
153+
data[pkt2ofs] = 0xff
154+
data[pkt2ofs+1] = 0xff
155+
data[pkt2ofs+2] = 0xff
156+
data[pkt2ofs+3] = 0x01
153157

154-
// conn.data = data
155-
// conn.reads = 0
156-
// conn.maxReads = 5
157-
// mc.sequence = 0
158-
// packet, err = mc.readPacket()
159-
// if err != nil {
160-
// t.Fatal(err)
161-
// }
162-
// if len(packet) != 2*maxPacketSize {
163-
// t.Fatalf("unexpected packet length: expected %d, got %d", 2*maxPacketSize, len(packet))
164-
// }
165-
// if packet[0] != 0x11 {
166-
// t.Fatalf("unexpected payload start: expected %x, got %x", 0x11, packet[0])
167-
// }
168-
// if packet[2*maxPacketSize-1] != 0x44 {
169-
// t.Fatalf("unexpected payload end: expected %x, got %x", 0x44, packet[2*maxPacketSize-1])
170-
// }
158+
// mark the payload start and end of the 2nd packet
159+
data[pkt2ofs+4] = 0x33
160+
data[pkt2ofs+maxPacketSize+3] = 0x44
171161

172-
// // case 3: payload has a length larger maxPacketSize, which is not an exact
173-
// // multiple of it
174-
// data = data[:pkt2ofs+4+42]
175-
// data[pkt2ofs] = 0x2a
176-
// data[pkt2ofs+1] = 0x00
177-
// data[pkt2ofs+2] = 0x00
178-
// data[pkt2ofs+4+41] = 0x44
162+
// 3rd packet has payload length 0 and sequence id 2
163+
// 00 00 00 02
164+
data[pkt3ofs+3] = 0x02
179165

180-
// conn.data = data
181-
// conn.reads = 0
182-
// conn.maxReads = 4
183-
// mc.sequence = 0
184-
// packet, err = mc.readPacket()
185-
// if err != nil {
186-
// t.Fatal(err)
187-
// }
188-
// if len(packet) != maxPacketSize+42 {
189-
// t.Fatalf("unexpected packet length: expected %d, got %d", maxPacketSize+42, len(packet))
190-
// }
191-
// if packet[0] != 0x11 {
192-
// t.Fatalf("unexpected payload start: expected %x, got %x", 0x11, packet[0])
193-
// }
194-
// if packet[maxPacketSize+41] != 0x44 {
195-
// t.Fatalf("unexpected payload end: expected %x, got %x", 0x44, packet[maxPacketSize+41])
196-
// }
197-
// }
166+
go func() {
167+
conn.Write(data)
168+
}()
169+
// TODO: check read operation count
170+
171+
packet, err := mc.readPacket(context.Background())
172+
if err != nil {
173+
t.Fatal(err)
174+
}
175+
if len(packet) != 2*maxPacketSize {
176+
t.Fatalf("unexpected packet length: expected %d, got %d", 2*maxPacketSize, len(packet))
177+
}
178+
if packet[0] != 0x11 {
179+
t.Fatalf("unexpected payload start: expected %x, got %x", 0x11, packet[0])
180+
}
181+
if packet[2*maxPacketSize-1] != 0x44 {
182+
t.Fatalf("unexpected payload end: expected %x, got %x", 0x44, packet[2*maxPacketSize-1])
183+
}
184+
})
185+
186+
t.Run("case 3: payload has a length larger maxPacketSize, which is not an exact multiple of it", func(t *testing.T) {
187+
conn, mc := newRWMockConn(t, 0)
188+
data := make([]byte, pkt2ofs+4+42)
189+
190+
// 1st packet has maxPacketSize length and sequence id 0
191+
// ff ff ff 00 ...
192+
data[0] = 0xff
193+
data[1] = 0xff
194+
data[2] = 0xff
195+
196+
// mark the payload start and end of 1st packet so that we can check if the
197+
// content was correctly appended
198+
data[4] = 0x11
199+
data[maxPacketSize+3] = 0x22
200+
201+
// 2nd packet
202+
data[pkt2ofs] = 0x2a
203+
data[pkt2ofs+1] = 0x00
204+
data[pkt2ofs+2] = 0x00
205+
data[pkt2ofs+3] = 0x01
206+
data[pkt2ofs+4+41] = 0x44
207+
208+
go func() {
209+
conn.Write(data)
210+
}()
211+
// TODO: check read operation count
212+
213+
packet, err := mc.readPacket(context.Background())
214+
if err != nil {
215+
t.Fatal(err)
216+
}
217+
if len(packet) != maxPacketSize+42 {
218+
t.Fatalf("unexpected packet length: expected %d, got %d", maxPacketSize+42, len(packet))
219+
}
220+
if packet[0] != 0x11 {
221+
t.Fatalf("unexpected payload start: expected %x, got %x", 0x11, packet[0])
222+
}
223+
if packet[maxPacketSize+41] != 0x44 {
224+
t.Fatalf("unexpected payload end: expected %x, got %x", 0x44, packet[maxPacketSize+41])
225+
}
226+
})
227+
}
198228

199229
// func TestReadPacketFail(t *testing.T) {
200230
// conn := new(mockConn)

0 commit comments

Comments
 (0)