vitess.io/vitess@v0.16.2/go/mysql/replication_position_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  	"encoding/json"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestPositionEqual(t *testing.T) {
    28  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
    29  	input2 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
    30  	want := true
    31  
    32  	if got := input1.Equal(input2); got != want {
    33  		t.Errorf("%#v.Equal(%#v) = %v, want %v", input1, input2, got, want)
    34  	}
    35  }
    36  
    37  func TestPositionNotEqual(t *testing.T) {
    38  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
    39  	input2 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 12345}}}
    40  	want := false
    41  
    42  	if got := input1.Equal(input2); got != want {
    43  		t.Errorf("%#v.Equal(%#v) = %v, want %v", input1, input2, got, want)
    44  	}
    45  }
    46  
    47  func TestPositionEqualZero(t *testing.T) {
    48  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
    49  	input2 := Position{}
    50  	want := false
    51  
    52  	if got := input1.Equal(input2); got != want {
    53  		t.Errorf("%#v.Equal(%#v) = %v, want %v", input1, input2, got, want)
    54  	}
    55  }
    56  
    57  func TestPositionZeroEqualZero(t *testing.T) {
    58  	input1 := Position{}
    59  	input2 := Position{}
    60  	want := true
    61  
    62  	if got := input1.Equal(input2); got != want {
    63  		t.Errorf("%#v.Equal(%#v) = %v, want %v", input1, input2, got, want)
    64  	}
    65  }
    66  
    67  func TestPositionAtLeastLess(t *testing.T) {
    68  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1233}}}
    69  	input2 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
    70  	want := false
    71  
    72  	if got := input1.AtLeast(input2); got != want {
    73  		t.Errorf("%#v.AtLeast(%#v) = %v, want %v", input1, input2, got, want)
    74  	}
    75  }
    76  
    77  func TestPositionAtLeastEqual(t *testing.T) {
    78  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
    79  	input2 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
    80  	want := true
    81  
    82  	if got := input1.AtLeast(input2); got != want {
    83  		t.Errorf("%#v.AtLeast(%#v) = %v, want %v", input1, input2, got, want)
    84  	}
    85  }
    86  
    87  func TestPositionAtLeastGreater(t *testing.T) {
    88  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1235}}}
    89  	input2 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
    90  	want := true
    91  
    92  	if got := input1.AtLeast(input2); got != want {
    93  		t.Errorf("%#v.AtLeast(%#v) = %v, want %v", input1, input2, got, want)
    94  	}
    95  }
    96  
    97  func TestPositionAtLeastDifferentServer(t *testing.T) {
    98  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1235}}}
    99  	input2 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 4444, Sequence: 1234}}}
   100  	want := true
   101  
   102  	if got := input1.AtLeast(input2); got != want {
   103  		t.Errorf("%#v.AtLeast(%#v) = %v, want %v", input1, input2, got, want)
   104  	}
   105  }
   106  
   107  func TestPositionAtLeastDifferentDomain(t *testing.T) {
   108  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1235}}}
   109  	input2 := Position{GTIDSet: MariadbGTIDSet{4: MariadbGTID{Domain: 4, Server: 5555, Sequence: 1234}}}
   110  	want := false
   111  
   112  	if got := input1.AtLeast(input2); got != want {
   113  		t.Errorf("%#v.AtLeast(%#v) = %v, want %v", input1, input2, got, want)
   114  	}
   115  }
   116  
   117  func TestPositionZeroAtLeast(t *testing.T) {
   118  	input1 := Position{}
   119  	input2 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
   120  	want := false
   121  
   122  	if got := input1.AtLeast(input2); got != want {
   123  		t.Errorf("%#v.AtLeast(%#v) = %v, want %v", input1, input2, got, want)
   124  	}
   125  }
   126  
   127  func TestPositionAtLeastZero(t *testing.T) {
   128  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
   129  	input2 := Position{GTIDSet: nil}
   130  	want := true
   131  
   132  	if got := input1.AtLeast(input2); got != want {
   133  		t.Errorf("%#v.AtLeast(%#v) = %v, want %v", input1, input2, got, want)
   134  	}
   135  }
   136  
   137  func TestPositionZeroAtLeastZero(t *testing.T) {
   138  	input1 := Position{}
   139  	input2 := Position{}
   140  	want := true
   141  
   142  	if got := input1.AtLeast(input2); got != want {
   143  		t.Errorf("%#v.AtLeast(%#v) = %v, want %v", input1, input2, got, want)
   144  	}
   145  }
   146  
   147  func TestPositionString(t *testing.T) {
   148  	input := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
   149  	want := "3-5555-1234"
   150  
   151  	if got := input.String(); got != want {
   152  		t.Errorf("%#v.String() = %#v, want %#v", input, got, want)
   153  	}
   154  }
   155  
   156  func TestPositionStringNil(t *testing.T) {
   157  	input := Position{}
   158  	want := "<nil>"
   159  
   160  	if got := input.String(); got != want {
   161  		t.Errorf("%#v.String() = %#v, want %#v", input, got, want)
   162  	}
   163  }
   164  
   165  func TestPositionIsZero(t *testing.T) {
   166  	input := Position{}
   167  	want := true
   168  
   169  	if got := input.IsZero(); got != want {
   170  		t.Errorf("%#v.IsZero() = %#v, want %#v", input, got, want)
   171  	}
   172  }
   173  
   174  func TestPositionIsNotZero(t *testing.T) {
   175  	input := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
   176  	want := false
   177  
   178  	if got := input.IsZero(); got != want {
   179  		t.Errorf("%#v.IsZero() = %#v, want %#v", input, got, want)
   180  	}
   181  }
   182  
   183  func TestPositionAppend(t *testing.T) {
   184  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
   185  	input2 := MariadbGTID{Domain: 3, Server: 5555, Sequence: 1235}
   186  	want := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1235}}}
   187  
   188  	if got := AppendGTID(input1, input2); !got.Equal(want) {
   189  		t.Errorf("AppendGTID(%#v, %#v) = %#v, want %#v", input1, input2, got, want)
   190  	}
   191  }
   192  
   193  func TestPositionAppendNil(t *testing.T) {
   194  	input1 := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
   195  	input2 := GTID(nil)
   196  	want := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
   197  
   198  	if got := AppendGTID(input1, input2); !got.Equal(want) {
   199  		t.Errorf("AppendGTID(%#v, %#v) = %#v, want %#v", input1, input2, got, want)
   200  	}
   201  }
   202  
   203  func TestPositionAppendToZero(t *testing.T) {
   204  	input1 := Position{}
   205  	input2 := MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}
   206  	want := Position{GTIDSet: MariadbGTIDSet{3: MariadbGTID{Domain: 3, Server: 5555, Sequence: 1234}}}
   207  
   208  	if got := AppendGTID(input1, input2); !got.Equal(want) {
   209  		t.Errorf("AppendGTID(%#v, %#v) = %#v, want %#v", input1, input2, got, want)
   210  	}
   211  }
   212  
   213  func TestMustParsePosition(t *testing.T) {
   214  	flavor := "fake flavor"
   215  	gtidSetParsers[flavor] = func(s string) (GTIDSet, error) {
   216  		return fakeGTID{value: s}, nil
   217  	}
   218  	input := "12345"
   219  	want := Position{GTIDSet: fakeGTID{value: "12345"}}
   220  
   221  	if got := MustParsePosition(flavor, input); !got.Equal(want) {
   222  		t.Errorf("MustParsePosition(%#v, %#v) = %#v, want %#v", flavor, input, got, want)
   223  	}
   224  }
   225  
   226  func TestMustParsePositionError(t *testing.T) {
   227  	defer func() {
   228  		want := `parse error: unknown GTIDSet flavor "unknown flavor !@$!@"`
   229  		err := recover()
   230  		assert.NotNil(t, err, "wrong error, got %#v, want %#v", err, want)
   231  
   232  		got, ok := err.(error)
   233  		if !ok || !strings.HasPrefix(got.Error(), want) {
   234  			t.Errorf("wrong error, got %#v, want %#v", got, want)
   235  		}
   236  	}()
   237  
   238  	MustParsePosition("unknown flavor !@$!@", "yowzah")
   239  }
   240  
   241  func TestEncodePosition(t *testing.T) {
   242  	input := Position{GTIDSet: fakeGTID{
   243  		flavor: "myflav",
   244  		value:  "1:2:3-4-5-6",
   245  	}}
   246  	want := "myflav/1:2:3-4-5-6"
   247  
   248  	if got := EncodePosition(input); got != want {
   249  		t.Errorf("EncodePosition(%#v) = %#v, want %#v", input, got, want)
   250  	}
   251  }
   252  
   253  func TestEncodePositionZero(t *testing.T) {
   254  	input := Position{}
   255  	want := ""
   256  
   257  	if got := EncodePosition(input); got != want {
   258  		t.Errorf("EncodePosition(%#v) = %#v, want %#v", input, got, want)
   259  	}
   260  }
   261  
   262  func TestDecodePosition(t *testing.T) {
   263  	gtidSetParsers["flavorflav"] = func(s string) (GTIDSet, error) {
   264  		return fakeGTID{value: s}, nil
   265  	}
   266  	input := "flavorflav/123-456:789"
   267  	want := Position{GTIDSet: fakeGTID{value: "123-456:789"}}
   268  
   269  	got, err := DecodePosition(input)
   270  	assert.NoError(t, err, "unexpected error: %v", err)
   271  	assert.True(t, got.Equal(want), "DecodePosition(%#v) = %#v, want %#v", input, got, want)
   272  
   273  }
   274  
   275  func TestDecodePositionZero(t *testing.T) {
   276  	input := ""
   277  	want := Position{}
   278  
   279  	got, err := DecodePosition(input)
   280  	assert.NoError(t, err, "unexpected error: %v", err)
   281  	assert.True(t, got.Equal(want), "DecodePosition(%#v) = %#v, want %#v", input, got, want)
   282  
   283  }
   284  
   285  func TestDecodePositionNoFlavor(t *testing.T) {
   286  	gtidSetParsers[""] = func(s string) (GTIDSet, error) {
   287  		return fakeGTID{value: s}, nil
   288  	}
   289  	input := "12345"
   290  	want := Position{GTIDSet: fakeGTID{value: "12345"}}
   291  
   292  	got, err := DecodePosition(input)
   293  	assert.NoError(t, err, "unexpected error: %v", err)
   294  	assert.True(t, got.Equal(want), "DecodePosition(%#v) = %#v, want %#v", input, got, want)
   295  
   296  }
   297  
   298  func TestJsonMarshalPosition(t *testing.T) {
   299  	input := Position{GTIDSet: fakeGTID{flavor: "golf", value: "par"}}
   300  	want := `"golf/par"`
   301  
   302  	buf, err := json.Marshal(input)
   303  	assert.NoError(t, err, "unexpected error: %v", err)
   304  
   305  	if got := string(buf); got != want {
   306  		t.Errorf("json.Marshal(%#v) = %#v, want %#v", input, got, want)
   307  	}
   308  }
   309  
   310  func TestJsonMarshalPositionPointer(t *testing.T) {
   311  	input := Position{GTIDSet: fakeGTID{flavor: "golf", value: "par"}}
   312  	want := `"golf/par"`
   313  
   314  	buf, err := json.Marshal(&input)
   315  	assert.NoError(t, err, "unexpected error: %v", err)
   316  
   317  	if got := string(buf); got != want {
   318  		t.Errorf("json.Marshal(%#v) = %#v, want %#v", input, got, want)
   319  	}
   320  }
   321  
   322  func TestJsonUnmarshalPosition(t *testing.T) {
   323  	gtidSetParsers["golf"] = func(s string) (GTIDSet, error) {
   324  		return fakeGTID{flavor: "golf", value: s}, nil
   325  	}
   326  	input := `"golf/par"`
   327  	want := Position{GTIDSet: fakeGTID{flavor: "golf", value: "par"}}
   328  
   329  	var got Position
   330  	err := json.Unmarshal([]byte(input), &got)
   331  	assert.NoError(t, err, "unexpected error: %v", err)
   332  	assert.True(t, got.Equal(want), "json.Unmarshal(%#v) = %#v, want %#v", input, got, want)
   333  
   334  }
   335  
   336  func TestJsonMarshalPositionInStruct(t *testing.T) {
   337  	input := Position{GTIDSet: fakeGTID{flavor: "golf", value: "par"}}
   338  	want := `{"Position":"golf/par"}`
   339  
   340  	type mystruct struct {
   341  		Position Position
   342  	}
   343  
   344  	buf, err := json.Marshal(&mystruct{input})
   345  	assert.NoError(t, err, "unexpected error: %v", err)
   346  
   347  	if got := string(buf); got != want {
   348  		t.Errorf("json.Marshal(%#v) = %#v, want %#v", input, got, want)
   349  	}
   350  }
   351  
   352  func TestJsonUnmarshalPositionInStruct(t *testing.T) {
   353  	gtidSetParsers["golf"] = func(s string) (GTIDSet, error) {
   354  		return fakeGTID{flavor: "golf", value: s}, nil
   355  	}
   356  	input := `{"Position":"golf/par"}`
   357  	want := Position{GTIDSet: fakeGTID{flavor: "golf", value: "par"}}
   358  
   359  	var gotStruct struct {
   360  		Position Position
   361  	}
   362  	err := json.Unmarshal([]byte(input), &gotStruct)
   363  	assert.NoError(t, err, "unexpected error: %v", err)
   364  
   365  	if got := gotStruct.Position; !got.Equal(want) {
   366  		t.Errorf("json.Unmarshal(%#v) = %#v, want %#v", input, got, want)
   367  	}
   368  }
   369  
   370  func TestJsonMarshalPositionZero(t *testing.T) {
   371  	input := Position{}
   372  	want := `""`
   373  
   374  	buf, err := json.Marshal(input)
   375  	assert.NoError(t, err, "unexpected error: %v", err)
   376  
   377  	if got := string(buf); got != want {
   378  		t.Errorf("json.Marshal(%#v) = %#v, want %#v", input, got, want)
   379  	}
   380  }
   381  
   382  func TestJsonUnmarshalPositionZero(t *testing.T) {
   383  	input := `""`
   384  	want := Position{}
   385  
   386  	var got Position
   387  	err := json.Unmarshal([]byte(input), &got)
   388  	assert.NoError(t, err, "unexpected error: %v", err)
   389  	assert.True(t, got.Equal(want), "json.Unmarshal(%#v) = %#v, want %#v", input, got, want)
   390  
   391  }