github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/schema_checker_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 petri
    15  
    16  import (
    17  	"time"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    21  	"github.com/whtcorpsinc/milevadb/causetstore/einsteindb"
    22  )
    23  
    24  func (s *testSuite) TestSchemaCheckerSimple(c *C) {
    25  	lease := 5 * time.Millisecond
    26  	validator := NewSchemaValidator(lease, nil)
    27  	checker := &SchemaChecker{SchemaValidator: validator}
    28  
    29  	// Add some schemaReplicant versions and delta causet IDs.
    30  	ts := uint64(time.Now().UnixNano())
    31  	validator.UFIDelate(ts, 0, 2, &einsteindb.RelatedSchemaChange{PhyTblIDS: []int64{1}, CausetActionTypes: []uint64{1}})
    32  	validator.UFIDelate(ts, 2, 4, &einsteindb.RelatedSchemaChange{PhyTblIDS: []int64{2}, CausetActionTypes: []uint64{2}})
    33  
    34  	// checker's schemaReplicant version is the same as the current schemaReplicant version.
    35  	checker.schemaVer = 4
    36  	_, err := checker.Check(ts)
    37  	c.Assert(err, IsNil)
    38  
    39  	// checker's schemaReplicant version is less than the current schemaReplicant version, and it doesn't exist in validator's items.
    40  	// checker's related causet ID isn't in validator's changed causet IDs.
    41  	checker.schemaVer = 2
    42  	checker.relatedBlockIDs = []int64{3}
    43  	_, err = checker.Check(ts)
    44  	c.Assert(err, IsNil)
    45  	// The checker's schemaReplicant version isn't in validator's items.
    46  	checker.schemaVer = 1
    47  	checker.relatedBlockIDs = []int64{3}
    48  	_, err = checker.Check(ts)
    49  	c.Assert(terror.ErrorEqual(err, ErrSchemaReplicantChanged), IsTrue)
    50  	// checker's related causet ID is in validator's changed causet IDs.
    51  	checker.relatedBlockIDs = []int64{2}
    52  	_, err = checker.Check(ts)
    53  	c.Assert(terror.ErrorEqual(err, ErrSchemaReplicantChanged), IsTrue)
    54  
    55  	// validator's latest schemaReplicant version is expired.
    56  	time.Sleep(lease + time.Microsecond)
    57  	checker.schemaVer = 4
    58  	checker.relatedBlockIDs = []int64{3}
    59  	_, err = checker.Check(ts)
    60  	c.Assert(err, IsNil)
    61  	nowTS := uint64(time.Now().UnixNano())
    62  	// Use checker.SchemaValidator.Check instead of checker.Check here because backoff make CI slow.
    63  	_, result := checker.SchemaValidator.Check(nowTS, checker.schemaVer, checker.relatedBlockIDs)
    64  	c.Assert(result, Equals, ResultUnknown)
    65  }