github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/infoschema/infoschema_test.go (about)

     1  // Copyright 2015 PingCAP, 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 infoschema_test
    15  
    16  import (
    17  	"fmt"
    18  	"sync"
    19  	"testing"
    20  
    21  	"github.com/insionng/yougam/libraries/juju/errors"
    22  	. "github.com/insionng/yougam/libraries/pingcap/check"
    23  	"github.com/insionng/yougam/libraries/pingcap/tidb/infoschema"
    24  	"github.com/insionng/yougam/libraries/pingcap/tidb/kv"
    25  	"github.com/insionng/yougam/libraries/pingcap/tidb/meta"
    26  	"github.com/insionng/yougam/libraries/pingcap/tidb/model"
    27  	"github.com/insionng/yougam/libraries/pingcap/tidb/mysql"
    28  	"github.com/insionng/yougam/libraries/pingcap/tidb/perfschema"
    29  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore"
    30  	"github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore/goleveldb"
    31  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    32  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testutil"
    33  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    34  )
    35  
    36  func TestT(t *testing.T) {
    37  	TestingT(t)
    38  }
    39  
    40  var _ = Suite(&testSuite{})
    41  
    42  type testSuite struct {
    43  }
    44  
    45  func (*testSuite) TestT(c *C) {
    46  	defer testleak.AfterTest(c)()
    47  	driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
    48  	store, err := driver.Open("memory")
    49  	c.Assert(err, IsNil)
    50  	defer store.Close()
    51  
    52  	handle, err := infoschema.NewHandle(store)
    53  	c.Assert(err, IsNil)
    54  	dbName := model.NewCIStr("Test")
    55  	tbName := model.NewCIStr("T")
    56  	colName := model.NewCIStr("A")
    57  	idxName := model.NewCIStr("idx")
    58  	noexist := model.NewCIStr("noexist")
    59  
    60  	colID, err := genGlobalID(store)
    61  	c.Assert(err, IsNil)
    62  	colInfo := &model.ColumnInfo{
    63  		ID:        colID,
    64  		Name:      colName,
    65  		Offset:    0,
    66  		FieldType: *types.NewFieldType(mysql.TypeLonglong),
    67  		State:     model.StatePublic,
    68  	}
    69  
    70  	idxInfo := &model.IndexInfo{
    71  		Name:  idxName,
    72  		Table: tbName,
    73  		Columns: []*model.IndexColumn{
    74  			{
    75  				Name:   colName,
    76  				Offset: 0,
    77  				Length: 10,
    78  			},
    79  		},
    80  		Unique:  true,
    81  		Primary: true,
    82  		State:   model.StatePublic,
    83  	}
    84  
    85  	tbID, err := genGlobalID(store)
    86  	c.Assert(err, IsNil)
    87  	tblInfo := &model.TableInfo{
    88  		ID:      tbID,
    89  		Name:    tbName,
    90  		Columns: []*model.ColumnInfo{colInfo},
    91  		Indices: []*model.IndexInfo{idxInfo},
    92  		State:   model.StatePublic,
    93  	}
    94  
    95  	dbID, err := genGlobalID(store)
    96  	c.Assert(err, IsNil)
    97  	dbInfo := &model.DBInfo{
    98  		ID:     dbID,
    99  		Name:   dbName,
   100  		Tables: []*model.TableInfo{tblInfo},
   101  		State:  model.StatePublic,
   102  	}
   103  
   104  	dbInfos := []*model.DBInfo{dbInfo}
   105  
   106  	handle.Set(dbInfos, 1)
   107  	is := handle.Get()
   108  
   109  	schemaNames := is.AllSchemaNames()
   110  	c.Assert(schemaNames, HasLen, 3)
   111  	c.Assert(testutil.CompareUnorderedStringSlice(schemaNames, []string{infoschema.Name, perfschema.Name, "Test"}), IsTrue)
   112  
   113  	schemas := is.AllSchemas()
   114  	c.Assert(schemas, HasLen, 3)
   115  	schemas = is.Clone()
   116  	c.Assert(schemas, HasLen, 3)
   117  
   118  	c.Assert(is.SchemaExists(dbName), IsTrue)
   119  	c.Assert(is.SchemaExists(noexist), IsFalse)
   120  
   121  	schema, ok := is.SchemaByID(dbID)
   122  	c.Assert(ok, IsTrue)
   123  	c.Assert(schema, NotNil)
   124  
   125  	schema, ok = is.SchemaByID(tbID)
   126  	c.Assert(ok, IsFalse)
   127  	c.Assert(schema, IsNil)
   128  
   129  	schema, ok = is.SchemaByName(dbName)
   130  	c.Assert(ok, IsTrue)
   131  	c.Assert(schema, NotNil)
   132  
   133  	schema, ok = is.SchemaByName(noexist)
   134  	c.Assert(ok, IsFalse)
   135  	c.Assert(schema, IsNil)
   136  
   137  	c.Assert(is.TableExists(dbName, tbName), IsTrue)
   138  	c.Assert(is.TableExists(dbName, noexist), IsFalse)
   139  
   140  	tb, ok := is.TableByID(tbID)
   141  	c.Assert(ok, IsTrue)
   142  	c.Assert(tb, NotNil)
   143  
   144  	tb, ok = is.TableByID(dbID)
   145  	c.Assert(ok, IsFalse)
   146  	c.Assert(tb, IsNil)
   147  
   148  	alloc, ok := is.AllocByID(tbID)
   149  	c.Assert(ok, IsTrue)
   150  	c.Assert(alloc, NotNil)
   151  
   152  	tb, err = is.TableByName(dbName, tbName)
   153  	c.Assert(err, IsNil)
   154  	c.Assert(tb, NotNil)
   155  
   156  	tb, err = is.TableByName(dbName, noexist)
   157  	c.Assert(err, NotNil)
   158  
   159  	c.Assert(is.ColumnExists(dbName, tbName, colName), IsTrue)
   160  	c.Assert(is.ColumnExists(dbName, tbName, noexist), IsFalse)
   161  
   162  	col, ok := is.ColumnByID(colID)
   163  	c.Assert(ok, IsTrue)
   164  	c.Assert(col, NotNil)
   165  
   166  	col, ok = is.ColumnByID(dbID)
   167  	c.Assert(ok, IsFalse)
   168  	c.Assert(col, IsNil)
   169  
   170  	col, ok = is.ColumnByName(dbName, tbName, colName)
   171  	c.Assert(ok, IsTrue)
   172  	c.Assert(col, NotNil)
   173  
   174  	col, ok = is.ColumnByName(dbName, tbName, noexist)
   175  	c.Assert(ok, IsFalse)
   176  	c.Assert(col, IsNil)
   177  
   178  	indices, ok := is.ColumnIndicesByID(colID)
   179  	c.Assert(ok, IsTrue)
   180  	c.Assert(indices, HasLen, 1)
   181  
   182  	tbs := is.SchemaTables(dbName)
   183  	c.Assert(tbs, HasLen, 1)
   184  
   185  	tbs = is.SchemaTables(noexist)
   186  	c.Assert(tbs, HasLen, 0)
   187  
   188  	idx, ok := is.IndexByName(dbName, tbName, idxName)
   189  	c.Assert(ok, IsTrue)
   190  	c.Assert(idx, NotNil)
   191  
   192  	// Make sure partitions table exists
   193  	tb, err = is.TableByName(model.NewCIStr("information_schema"), model.NewCIStr("partitions"))
   194  	c.Assert(err, IsNil)
   195  	c.Assert(tb, NotNil)
   196  
   197  	col, ok = is.ColumnByName(model.NewCIStr("information_schema"), model.NewCIStr("files"), model.NewCIStr("FILE_TYPE"))
   198  	c.Assert(ok, IsTrue)
   199  	c.Assert(col, NotNil)
   200  }
   201  
   202  // Make sure it is safe to concurrently create handle on multiple stores.
   203  func (testSuite) TestConcurrent(c *C) {
   204  	defer testleak.AfterTest(c)()
   205  	storeCount := 5
   206  	stores := make([]kv.Storage, storeCount)
   207  	for i := 0; i < storeCount; i++ {
   208  		driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}}
   209  		store, err := driver.Open(fmt.Sprintf("memory_path_%d", i))
   210  		c.Assert(err, IsNil)
   211  		stores[i] = store
   212  	}
   213  	defer func() {
   214  		for _, store := range stores {
   215  			store.Close()
   216  		}
   217  	}()
   218  	var wg sync.WaitGroup
   219  	wg.Add(storeCount)
   220  	for _, store := range stores {
   221  		go func(s kv.Storage) {
   222  			defer wg.Done()
   223  			_, err := infoschema.NewHandle(s)
   224  			c.Assert(err, IsNil)
   225  		}(store)
   226  	}
   227  	wg.Wait()
   228  }
   229  
   230  func genGlobalID(store kv.Storage) (int64, error) {
   231  	var globalID int64
   232  	err := kv.RunInNewTxn(store, true, func(txn kv.Transaction) error {
   233  		var err error
   234  		globalID, err = meta.NewMeta(txn).GenGlobalID()
   235  		return errors.Trace(err)
   236  	})
   237  	return globalID, errors.Trace(err)
   238  }