@@ -194,3 +194,56 @@ 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+
239+ for _ , tc := range testcases {
240+ e := HeartbeatEvent {Version : tc .version }
241+ err := e .Decode (tc .input )
242+ if tc .err {
243+ require .Error (t , err )
244+ } else {
245+ require .NoError (t , err )
246+ require .Equal (t , tc .hbEvent , e )
247+ }
248+ }
249+ }
0 commit comments