github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/tests/table_split_test.go (about)

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tests_test
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/base"
    18  	"github.com/cockroachdb/cockroach/pkg/keys"
    19  	"github.com/cockroachdb/cockroach/pkg/testutils"
    20  	"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
    21  	"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
    22  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    23  	"github.com/cockroachdb/cockroach/pkg/util/log"
    24  	"github.com/cockroachdb/errors"
    25  )
    26  
    27  func TestSplitAtTableBoundary(t *testing.T) {
    28  	defer leaktest.AfterTest(t)()
    29  
    30  	testClusterArgs := base.TestClusterArgs{
    31  		ReplicationMode: base.ReplicationAuto,
    32  	}
    33  	tc := testcluster.StartTestCluster(t, 3, testClusterArgs)
    34  	defer tc.Stopper().Stop(context.Background())
    35  
    36  	runner := sqlutils.MakeSQLRunner(tc.Conns[0])
    37  	runner.Exec(t, `CREATE DATABASE test`)
    38  	runner.Exec(t, `CREATE TABLE test.t (k SERIAL PRIMARY KEY, v INT)`)
    39  
    40  	const tableIDQuery = `
    41  SELECT tables.id FROM system.namespace tables
    42    JOIN system.namespace dbs ON dbs.id = tables."parentID"
    43    WHERE dbs.name = $1 AND tables.name = $2
    44  `
    45  	var tableID uint32
    46  	runner.QueryRow(t, tableIDQuery, "test", "t").Scan(&tableID)
    47  	tableStartKey := keys.SystemSQLCodec.TablePrefix(tableID)
    48  
    49  	// Wait for new table to split.
    50  	testutils.SucceedsSoon(t, func() error {
    51  		desc, err := tc.LookupRange(tableStartKey)
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  		if !desc.StartKey.Equal(tableStartKey) {
    56  			log.Infof(context.Background(), "waiting on split results")
    57  			return errors.Errorf("expected range start key %s; got %s", tableStartKey, desc.StartKey)
    58  		}
    59  		return nil
    60  	})
    61  }