github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/meta/autoid/autoid_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 autoid_test 15 16 import ( 17 "testing" 18 19 . "github.com/insionng/yougam/libraries/pingcap/check" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/kv" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/meta" 22 "github.com/insionng/yougam/libraries/pingcap/tidb/meta/autoid" 23 "github.com/insionng/yougam/libraries/pingcap/tidb/model" 24 "github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore" 25 "github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore/goleveldb" 26 ) 27 28 func TestT(t *testing.T) { 29 TestingT(t) 30 } 31 32 var _ = Suite(&testSuite{}) 33 34 type testSuite struct { 35 } 36 37 func (*testSuite) TestT(c *C) { 38 driver := localstore.Driver{Driver: goleveldb.MemoryDriver{}} 39 store, err := driver.Open("memory") 40 c.Assert(err, IsNil) 41 defer store.Close() 42 43 err = kv.RunInNewTxn(store, false, func(txn kv.Transaction) error { 44 m := meta.NewMeta(txn) 45 err = m.CreateDatabase(&model.DBInfo{ID: 1, Name: model.NewCIStr("a")}) 46 c.Assert(err, IsNil) 47 err = m.CreateTable(1, &model.TableInfo{ID: 1, Name: model.NewCIStr("t")}) 48 c.Assert(err, IsNil) 49 err = m.CreateTable(1, &model.TableInfo{ID: 2, Name: model.NewCIStr("t1")}) 50 c.Assert(err, IsNil) 51 err = m.CreateTable(1, &model.TableInfo{ID: 3, Name: model.NewCIStr("t1")}) 52 c.Assert(err, IsNil) 53 return nil 54 }) 55 c.Assert(err, IsNil) 56 57 alloc := autoid.NewAllocator(store, 1) 58 c.Assert(alloc, NotNil) 59 60 id, err := alloc.Alloc(1) 61 c.Assert(err, IsNil) 62 c.Assert(id, Equals, int64(1)) 63 id, err = alloc.Alloc(1) 64 c.Assert(err, IsNil) 65 c.Assert(id, Equals, int64(2)) 66 id, err = alloc.Alloc(0) 67 c.Assert(err, NotNil) 68 69 // rebase 70 err = alloc.Rebase(1, int64(1), true) 71 c.Assert(err, IsNil) 72 id, err = alloc.Alloc(1) 73 c.Assert(err, IsNil) 74 c.Assert(id, Equals, int64(3)) 75 err = alloc.Rebase(1, int64(3), true) 76 c.Assert(err, IsNil) 77 id, err = alloc.Alloc(1) 78 c.Assert(err, IsNil) 79 c.Assert(id, Equals, int64(4)) 80 err = alloc.Rebase(1, int64(10), true) 81 c.Assert(err, IsNil) 82 id, err = alloc.Alloc(1) 83 c.Assert(err, IsNil) 84 c.Assert(id, Equals, int64(11)) 85 err = alloc.Rebase(1, int64(3010), true) 86 c.Assert(err, IsNil) 87 id, err = alloc.Alloc(1) 88 c.Assert(err, IsNil) 89 c.Assert(id, Equals, int64(3011)) 90 91 alloc = autoid.NewAllocator(store, 1) 92 c.Assert(alloc, NotNil) 93 id, err = alloc.Alloc(1) 94 c.Assert(err, IsNil) 95 c.Assert(id, Equals, int64(4011)) 96 97 alloc = autoid.NewAllocator(store, 1) 98 c.Assert(alloc, NotNil) 99 err = alloc.Rebase(2, int64(1), false) 100 c.Assert(err, IsNil) 101 id, err = alloc.Alloc(2) 102 c.Assert(err, IsNil) 103 c.Assert(id, Equals, int64(2)) 104 105 alloc = autoid.NewAllocator(store, 1) 106 c.Assert(alloc, NotNil) 107 err = alloc.Rebase(3, int64(3210), false) 108 c.Assert(err, IsNil) 109 alloc = autoid.NewAllocator(store, 1) 110 c.Assert(alloc, NotNil) 111 err = alloc.Rebase(3, int64(3000), false) 112 c.Assert(err, IsNil) 113 id, err = alloc.Alloc(3) 114 c.Assert(err, IsNil) 115 c.Assert(id, Equals, int64(3211)) 116 err = alloc.Rebase(3, int64(6543), false) 117 c.Assert(err, IsNil) 118 id, err = alloc.Alloc(3) 119 c.Assert(err, IsNil) 120 c.Assert(id, Equals, int64(6544)) 121 }