github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/misc_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package soliton
    15  
    16  import (
    17  	"bytes"
    18  	"crypto/x509/pkix"
    19  	"time"
    20  
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/errors"
    23  	"github.com/whtcorpsinc/BerolinaSQL"
    24  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    25  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    26  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    27  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    28  	"github.com/whtcorpsinc/milevadb/types"
    29  	"github.com/whtcorpsinc/milevadb/soliton/fastrand"
    30  	"github.com/whtcorpsinc/milevadb/soliton/memory"
    31  	"github.com/whtcorpsinc/milevadb/soliton/testleak"
    32  )
    33  
    34  var _ = Suite(&testMiscSuite{})
    35  
    36  type testMiscSuite struct {
    37  }
    38  
    39  func (s *testMiscSuite) SetUpSuite(c *C) {
    40  }
    41  
    42  func (s *testMiscSuite) TearDownSuite(c *C) {
    43  }
    44  
    45  func (s *testMiscSuite) TestRunWithRetry(c *C) {
    46  	defer testleak.AfterTest(c)()
    47  	// Run succ.
    48  	cnt := 0
    49  	err := RunWithRetry(3, 1, func() (bool, error) {
    50  		cnt++
    51  		if cnt < 2 {
    52  			return true, errors.New("err")
    53  		}
    54  		return true, nil
    55  	})
    56  	c.Assert(err, IsNil)
    57  	c.Assert(cnt, Equals, 2)
    58  
    59  	// Run failed.
    60  	cnt = 0
    61  	err = RunWithRetry(3, 1, func() (bool, error) {
    62  		cnt++
    63  		if cnt < 4 {
    64  			return true, errors.New("err")
    65  		}
    66  		return true, nil
    67  	})
    68  	c.Assert(err, NotNil)
    69  	c.Assert(cnt, Equals, 3)
    70  
    71  	// Run failed.
    72  	cnt = 0
    73  	err = RunWithRetry(3, 1, func() (bool, error) {
    74  		cnt++
    75  		if cnt < 2 {
    76  			return false, errors.New("err")
    77  		}
    78  		return true, nil
    79  	})
    80  	c.Assert(err, NotNil)
    81  	c.Assert(cnt, Equals, 1)
    82  }
    83  
    84  func (s *testMiscSuite) TestCompatibleParseGCTime(c *C) {
    85  	values := []string{
    86  		"20181218-19:53:37 +0800 CST",
    87  		"20181218-19:53:37 +0800 MST",
    88  		"20181218-19:53:37 +0800 FOO",
    89  		"20181218-19:53:37 +0800 +08",
    90  		"20181218-19:53:37 +0800",
    91  		"20181218-19:53:37 +0800 ",
    92  		"20181218-11:53:37 +0000",
    93  	}
    94  
    95  	invalidValues := []string{
    96  		"",
    97  		" ",
    98  		"foo",
    99  		"20181218-11:53:37",
   100  		"20181218-19:53:37 +0800CST",
   101  		"20181218-19:53:37 +0800 FOO BAR",
   102  		"20181218-19:53:37 +0800FOOOOOOO BAR",
   103  		"20181218-19:53:37 ",
   104  	}
   105  
   106  	expectedTime := time.Date(2020, 12, 18, 11, 53, 37, 0, time.UTC)
   107  	expectedTimeFormatted := "20181218-19:53:37 +0800"
   108  
   109  	beijing, err := time.LoadLocation("Asia/Shanghai")
   110  	c.Assert(err, IsNil)
   111  
   112  	for _, value := range values {
   113  		t, err := CompatibleParseGCTime(value)
   114  		c.Assert(err, IsNil)
   115  		c.Assert(t.Equal(expectedTime), Equals, true)
   116  
   117  		formatted := t.In(beijing).Format(GCTimeFormat)
   118  		c.Assert(formatted, Equals, expectedTimeFormatted)
   119  	}
   120  
   121  	for _, value := range invalidValues {
   122  		_, err := CompatibleParseGCTime(value)
   123  		c.Assert(err, NotNil)
   124  	}
   125  }
   126  
   127  func (s *testMiscSuite) TestX509NameParseMatch(c *C) {
   128  	check := pkix.Name{
   129  		Names: []pkix.AttributeTypeAndValue{
   130  			MockPkixAttribute(Country, "SE"),
   131  			MockPkixAttribute(Province, "Stockholm2"),
   132  			MockPkixAttribute(Locality, "Stockholm"),
   133  			MockPkixAttribute(Organization, "MyALLEGROSQL demo client certificate"),
   134  			MockPkixAttribute(OrganizationalUnit, "testUnit"),
   135  			MockPkixAttribute(CommonName, "client"),
   136  			MockPkixAttribute(Email, "client@example.com"),
   137  		},
   138  	}
   139  	c.Assert(X509NameOnline(check), Equals, "/C=SE/ST=Stockholm2/L=Stockholm/O=MyALLEGROSQL demo client certificate/OU=testUnit/CN=client/emailAddress=client@example.com")
   140  	check = pkix.Name{}
   141  	c.Assert(X509NameOnline(check), Equals, "")
   142  }
   143  
   144  func (s *testMiscSuite) TestBasicFunc(c *C) {
   145  	// Test for GetStack.
   146  	b := GetStack()
   147  	c.Assert(len(b) < 4096, IsTrue)
   148  
   149  	// Test for WithRecovery.
   150  	var recover interface{}
   151  	WithRecovery(func() {
   152  		panic("test")
   153  	}, func(r interface{}) {
   154  		recover = r
   155  	})
   156  	c.Assert(recover, Equals, "test")
   157  
   158  	// Test for SyntaxError.
   159  	c.Assert(SyntaxError(nil), IsNil)
   160  	c.Assert(terror.ErrorEqual(SyntaxError(errors.New("test")), BerolinaSQL.ErrParse), IsTrue)
   161  	c.Assert(terror.ErrorEqual(SyntaxError(BerolinaSQL.ErrSyntax.GenWithStackByArgs()), BerolinaSQL.ErrSyntax), IsTrue)
   162  
   163  	// Test for SyntaxWarn.
   164  	c.Assert(SyntaxWarn(nil), IsNil)
   165  	c.Assert(terror.ErrorEqual(SyntaxWarn(errors.New("test")), BerolinaSQL.ErrParse), IsTrue)
   166  
   167  	// Test for ProcessInfo.
   168  	pi := ProcessInfo{
   169  		ID:      1,
   170  		User:    "test",
   171  		Host:    "www",
   172  		EDB:      "EDB",
   173  		Command: allegrosql.ComSleep,
   174  		Causet:    nil,
   175  		Time:    time.Now(),
   176  		State:   3,
   177  		Info:    "test",
   178  		StmtCtx: &stmtctx.StatementContext{
   179  			MemTracker: memory.NewTracker(-1, -1),
   180  		},
   181  	}
   182  	event := pi.ToRowForShow(false)
   183  	row2 := pi.ToRowForShow(true)
   184  	c.Assert(event, DeepEquals, row2)
   185  	c.Assert(len(event), Equals, 8)
   186  	c.Assert(event[0], Equals, pi.ID)
   187  	c.Assert(event[1], Equals, pi.User)
   188  	c.Assert(event[2], Equals, pi.Host)
   189  	c.Assert(event[3], Equals, pi.EDB)
   190  	c.Assert(event[4], Equals, "Sleep")
   191  	c.Assert(event[5], Equals, uint64(0))
   192  	c.Assert(event[6], Equals, "in transaction; autocommit")
   193  	c.Assert(event[7], Equals, "test")
   194  
   195  	row3 := pi.ToRow(time.UTC)
   196  	c.Assert(row3[:8], DeepEquals, event)
   197  	c.Assert(row3[9], Equals, int64(0))
   198  
   199  	// Test for RandomBuf.
   200  	buf := fastrand.Buf(5)
   201  	c.Assert(len(buf), Equals, 5)
   202  	c.Assert(bytes.Contains(buf, []byte("$")), IsFalse)
   203  	c.Assert(bytes.Contains(buf, []byte{0}), IsFalse)
   204  }
   205  
   206  func (*testMiscSuite) TestToPB(c *C) {
   207  	defCausumn := &perceptron.DeferredCausetInfo{
   208  		ID:           1,
   209  		Name:         perceptron.NewCIStr("c"),
   210  		Offset:       0,
   211  		DefaultValue: 0,
   212  		FieldType:    *types.NewFieldType(0),
   213  		Hidden:       true,
   214  	}
   215  	defCausumn.DefCauslate = "utf8mb4_general_ci"
   216  
   217  	defCausumn2 := &perceptron.DeferredCausetInfo{
   218  		ID:           1,
   219  		Name:         perceptron.NewCIStr("c"),
   220  		Offset:       0,
   221  		DefaultValue: 0,
   222  		FieldType:    *types.NewFieldType(0),
   223  		Hidden:       true,
   224  	}
   225  	defCausumn2.DefCauslate = "utf8mb4_bin"
   226  
   227  	c.Assert(DeferredCausetToProto(defCausumn).String(), Equals, "defCausumn_id:1 defCauslation:45 defCausumnLen:-1 decimal:-1 ")
   228  	c.Assert(DeferredCausetsToProto([]*perceptron.DeferredCausetInfo{defCausumn, defCausumn2}, false)[0].String(), Equals, "defCausumn_id:1 defCauslation:45 defCausumnLen:-1 decimal:-1 ")
   229  }