@@ -194,3 +194,62 @@ func TestPreviousGTIDEvent(t *testing.T) {
194194 require .Equal (t , tc .GTIDSets , e .GTIDSets )
195195 }
196196}
197+
198+ func TestHeartbeatEvent (t * testing.T ) {
199+ testcases := []struct {
200+ err bool
201+ version int
202+ input []byte // make sure to strip the 4 byte checksum
203+ hbEvent HeartbeatEvent
204+ }{
205+ {
206+ false ,
207+ 2 ,
208+ []byte {0x1 , 0xd , 0x62 , 0x69 , 0x6e , 0x6c , 0x6f , 0x67 , 0x2e , 0x30 , 0x30 , 0x30 , 0x30 , 0x30 , 0x31 , 0x2 , 0x1 , 0x9e , 0x0 },
209+ HeartbeatEvent {Version : 2 , Filename : "binlog.000001" , Offset : 158 },
210+ },
211+ {
212+ true ,
213+ 2 ,
214+ // 0x3 is not a valid field type
215+ []byte {0x3 , 0xd , 0x62 , 0x69 , 0x6e , 0x6c , 0x6f , 0x67 , 0x2e , 0x30 , 0x30 , 0x30 , 0x30 , 0x30 , 0x31 , 0x2 , 0x1 , 0x9e , 0x0 },
216+ HeartbeatEvent {},
217+ },
218+ {
219+ true ,
220+ 2 ,
221+ // 0x2, 0x9e is not a valid length encoded integer
222+ []byte {0x1 , 0xd , 0x62 , 0x69 , 0x6e , 0x6c , 0x6f , 0x67 , 0x2e , 0x30 , 0x30 , 0x30 , 0x30 , 0x30 , 0x31 , 0x2 , 0x2 , 0x9e , 0x0 },
223+ HeartbeatEvent {Version : 2 , Filename : "binlog.000001" , Offset : 158 },
224+ },
225+ {
226+ false ,
227+ 1 ,
228+ []byte {0x62 , 0x69 , 0x6e , 0x6c , 0x6f , 0x67 , 0x2e , 0x30 , 0x30 , 0x30 , 0x30 , 0x30 , 0x31 },
229+ HeartbeatEvent {Version : 1 , Filename : "binlog.000001" },
230+ },
231+ {
232+ true ,
233+ 3 , // invalid heartbeat version
234+ []byte {},
235+ HeartbeatEvent {},
236+ },
237+ {
238+ false ,
239+ 2 ,
240+ []byte {0x1 , 0xd , 0x62 , 0x69 , 0x6e , 0x6c , 0x6f , 0x67 , 0x2e , 0x30 , 0x30 , 0x30 , 0x30 , 0x30 , 0x31 , 0x2 , 0x4 , 0xfd , 0xbb , 0xaf , 0x27 , 0x0 },
241+ HeartbeatEvent {Version : 2 , Filename : "binlog.000001" , Offset : 2600891 },
242+ },
243+ }
244+
245+ for _ , tc := range testcases {
246+ e := HeartbeatEvent {Version : tc .version }
247+ err := e .Decode (tc .input )
248+ if tc .err {
249+ require .Error (t , err )
250+ } else {
251+ require .NoError (t , err )
252+ require .Equal (t , tc .hbEvent , e )
253+ }
254+ }
255+ }
0 commit comments