vitess.io/vitess@v0.16.2/go/mysql/binlog_event_mysql56_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package mysql
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  // Sample event data for MySQL 5.6.
    28  var (
    29  	mysql56FormatEvent             = NewMysql56BinlogEvent([]byte{0x78, 0x4e, 0x49, 0x55, 0xf, 0x64, 0x0, 0x0, 0x0, 0x74, 0x0, 0x0, 0x0, 0x78, 0x0, 0x0, 0x0, 0x1, 0x0, 0x4, 0x0, 0x35, 0x2e, 0x36, 0x2e, 0x32, 0x34, 0x2d, 0x6c, 0x6f, 0x67, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x78, 0x4e, 0x49, 0x55, 0x13, 0x38, 0xd, 0x0, 0x8, 0x0, 0x12, 0x0, 0x4, 0x4, 0x4, 0x4, 0x12, 0x0, 0x0, 0x5c, 0x0, 0x4, 0x1a, 0x8, 0x0, 0x0, 0x0, 0x8, 0x8, 0x8, 0x2, 0x0, 0x0, 0x0, 0xa, 0xa, 0xa, 0x19, 0x19, 0x0, 0x1, 0x18, 0x4a, 0xf, 0xca})
    30  	mysql56GTIDEvent               = NewMysql56BinlogEvent([]byte{0xff, 0x4e, 0x49, 0x55, 0x21, 0x64, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0xf5, 0x2, 0x0, 0x0, 0x0, 0x0, 0x1, 0x43, 0x91, 0x92, 0xbd, 0xf3, 0x7c, 0x11, 0xe4, 0xbb, 0xeb, 0x2, 0x42, 0xac, 0x11, 0x3, 0x5a, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x48, 0x45, 0x82, 0x27})
    31  	mysql56QueryEvent              = NewMysql56BinlogEvent([]byte{0xff, 0x4e, 0x49, 0x55, 0x2, 0x64, 0x0, 0x0, 0x0, 0x77, 0x0, 0x0, 0x0, 0xdb, 0x3, 0x0, 0x0, 0x0, 0x0, 0x3d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x3, 0x73, 0x74, 0x64, 0x4, 0x8, 0x0, 0x8, 0x0, 0x21, 0x0, 0xc, 0x1, 0x74, 0x65, 0x73, 0x74, 0x0, 0x74, 0x65, 0x73, 0x74, 0x0, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x28, 0x6d, 0x73, 0x67, 0x29, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x20, 0x28, 0x27, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x27, 0x29, 0x92, 0x12, 0x79, 0xc3})
    32  	mysql56SemiSyncNoAckQueryEvent = NewMysql56BinlogEvent([]byte{0xef, 0x00, 0xff, 0x4e, 0x49, 0x55, 0x2, 0x64, 0x0, 0x0, 0x0, 0x77, 0x0, 0x0, 0x0, 0xdb, 0x3, 0x0, 0x0, 0x0, 0x0, 0x3d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x3, 0x73, 0x74, 0x64, 0x4, 0x8, 0x0, 0x8, 0x0, 0x21, 0x0, 0xc, 0x1, 0x74, 0x65, 0x73, 0x74, 0x0, 0x74, 0x65, 0x73, 0x74, 0x0, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x28, 0x6d, 0x73, 0x67, 0x29, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x20, 0x28, 0x27, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x27, 0x29, 0x92, 0x12, 0x79, 0xc3})
    33  	mysql56SemiSyncAckQueryEvent   = NewMysql56BinlogEvent([]byte{0xef, 0x01, 0xff, 0x4e, 0x49, 0x55, 0x2, 0x64, 0x0, 0x0, 0x0, 0x77, 0x0, 0x0, 0x0, 0xdb, 0x3, 0x0, 0x0, 0x0, 0x0, 0x3d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x21, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x3, 0x73, 0x74, 0x64, 0x4, 0x8, 0x0, 0x8, 0x0, 0x21, 0x0, 0xc, 0x1, 0x74, 0x65, 0x73, 0x74, 0x0, 0x74, 0x65, 0x73, 0x74, 0x0, 0x69, 0x6e, 0x73, 0x65, 0x72, 0x74, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x28, 0x6d, 0x73, 0x67, 0x29, 0x20, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x20, 0x28, 0x27, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x27, 0x29, 0x92, 0x12, 0x79, 0xc3})
    34  )
    35  
    36  func TestMysql56IsGTID(t *testing.T) {
    37  	if got, want := mysql56FormatEvent.IsGTID(), false; got != want {
    38  		t.Errorf("%#v.IsGTID() = %#v, want %#v", mysql56FormatEvent, got, want)
    39  	}
    40  	if got, want := mysql56QueryEvent.IsGTID(), false; got != want {
    41  		t.Errorf("%#v.IsGTID() = %#v, want %#v", mysql56QueryEvent, got, want)
    42  	}
    43  	if got, want := mysql56GTIDEvent.IsGTID(), true; got != want {
    44  		t.Errorf("%#v.IsGTID() = %#v, want %#v", mysql56GTIDEvent, got, want)
    45  	}
    46  }
    47  
    48  func TestMysql56StripChecksum(t *testing.T) {
    49  	format, err := mysql56FormatEvent.Format()
    50  	require.NoError(t, err, "Format() error: %v", err)
    51  
    52  	stripped, gotChecksum, err := mysql56QueryEvent.StripChecksum(format)
    53  	require.NoError(t, err, "StripChecksum() error: %v", err)
    54  
    55  	// Check checksum.
    56  	if want := []byte{0x92, 0x12, 0x79, 0xc3}; !reflect.DeepEqual(gotChecksum, want) {
    57  		t.Errorf("checksum = %#v, want %#v", gotChecksum, want)
    58  	}
    59  
    60  	// Check query, to make sure checksum was stripped properly.
    61  	// Query length is defined as "the rest of the bytes after offset X",
    62  	// so the query will be wrong if the checksum is not stripped.
    63  	gotQuery, err := stripped.Query(format)
    64  	require.NoError(t, err, "Query() error: %v", err)
    65  
    66  	if want := "insert into test_table (msg) values ('hello')"; string(gotQuery.SQL) != want {
    67  		t.Errorf("query = %#v, want %#v", string(gotQuery.SQL), want)
    68  	}
    69  }
    70  
    71  func TestMysql56GTID(t *testing.T) {
    72  	format, err := mysql56FormatEvent.Format()
    73  	require.NoError(t, err, "Format() error: %v", err)
    74  
    75  	input, _, err := mysql56GTIDEvent.StripChecksum(format)
    76  	require.NoError(t, err, "StripChecksum() error: %v", err)
    77  	require.True(t, input.IsGTID(), "IsGTID() = false, want true")
    78  
    79  	want, _ := parseMysql56GTID("439192bd-f37c-11e4-bbeb-0242ac11035a:4")
    80  	got, hasBegin, err := input.GTID(format)
    81  	require.NoError(t, err, "GTID() error: %v", err)
    82  	assert.False(t, hasBegin, "GTID() returned hasBegin")
    83  	assert.Equal(t, want, got, "GTID() = %#v, want %#v", got, want)
    84  
    85  }
    86  
    87  func TestMysql56ParseGTID(t *testing.T) {
    88  	input := "00010203-0405-0607-0809-0A0B0C0D0E0F:56789"
    89  	want := Mysql56GTID{
    90  		Server:   SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
    91  		Sequence: 56789,
    92  	}
    93  
    94  	got, err := parseMysql56GTID(input)
    95  	require.NoError(t, err, "unexpected error: %v", err)
    96  	assert.Equal(t, want, got, "(&mysql56{}).ParseGTID(%#v) = %#v, want %#v", input, got, want)
    97  
    98  }
    99  
   100  func TestMysql56ParsePosition(t *testing.T) {
   101  	input := "00010203-0405-0607-0809-0a0b0c0d0e0f:1-2"
   102  
   103  	sid := SID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
   104  	var set GTIDSet = Mysql56GTIDSet{}
   105  	set = set.AddGTID(Mysql56GTID{Server: sid, Sequence: 1})
   106  	set = set.AddGTID(Mysql56GTID{Server: sid, Sequence: 2})
   107  	want := Position{GTIDSet: set}
   108  
   109  	got, err := ParsePosition(Mysql56FlavorID, input)
   110  	assert.NoError(t, err, "unexpected error: %v", err)
   111  	assert.True(t, got.Equal(want), "(&mysql56{}).ParsePosition(%#v) = %#v, want %#v", input, got, want)
   112  
   113  }
   114  
   115  func TestMysql56SemiSyncAck(t *testing.T) {
   116  	{
   117  		c := Conn{ExpectSemiSyncIndicator: false}
   118  		buf, semiSyncAckRequested, err := c.AnalyzeSemiSyncAckRequest(mysql56QueryEvent.Bytes())
   119  		assert.NoError(t, err)
   120  		e := NewMysql56BinlogEventWithSemiSyncInfo(buf, semiSyncAckRequested)
   121  
   122  		assert.False(t, e.IsSemiSyncAckRequested())
   123  		assert.True(t, e.IsQuery())
   124  	}
   125  	{
   126  		c := Conn{ExpectSemiSyncIndicator: true}
   127  		buf, semiSyncAckRequested, err := c.AnalyzeSemiSyncAckRequest(mysql56SemiSyncNoAckQueryEvent.Bytes())
   128  		assert.NoError(t, err)
   129  		e := NewMysql56BinlogEventWithSemiSyncInfo(buf, semiSyncAckRequested)
   130  
   131  		assert.False(t, e.IsSemiSyncAckRequested())
   132  		assert.True(t, e.IsQuery())
   133  	}
   134  	{
   135  		c := Conn{ExpectSemiSyncIndicator: true}
   136  		buf, semiSyncAckRequested, err := c.AnalyzeSemiSyncAckRequest(mysql56SemiSyncAckQueryEvent.Bytes())
   137  		assert.NoError(t, err)
   138  		e := NewMysql56BinlogEventWithSemiSyncInfo(buf, semiSyncAckRequested)
   139  
   140  		assert.True(t, e.IsSemiSyncAckRequested())
   141  		assert.True(t, e.IsQuery())
   142  	}
   143  }