github.com/turingchain2020/turingchain@v1.1.21/system/store/base_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package store 6 7 import ( 8 "os" 9 "testing" 10 11 "github.com/turingchain2020/turingchain/common/log" 12 "github.com/turingchain2020/turingchain/queue" 13 "github.com/turingchain2020/turingchain/types" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 var storecfg0 = &types.Store{Name: "base_test", Driver: "leveldb", DbPath: "/tmp/base_test0", DbCache: 100} 18 var storecfg1 = &types.Store{Name: "base_test", Driver: "leveldb", DbPath: "/tmp/base_test1", DbCache: 100} 19 20 type storeChild struct { 21 } 22 23 func (s *storeChild) Set(datas *types.StoreSet, sync bool) ([]byte, error) { 24 return []byte{}, nil 25 } 26 27 func (s *storeChild) Get(datas *types.StoreGet) [][]byte { 28 return [][]byte{} 29 } 30 31 func (s *storeChild) MemSet(datas *types.StoreSet, sync bool) ([]byte, error) { 32 return []byte{}, nil 33 } 34 35 func (s *storeChild) Commit(hash *types.ReqHash) ([]byte, error) { 36 return []byte{}, nil 37 } 38 39 func (s *storeChild) MemSetUpgrade(datas *types.StoreSet, sync bool) ([]byte, error) { 40 return []byte{}, nil 41 } 42 43 func (s *storeChild) CommitUpgrade(req *types.ReqHash) ([]byte, error) { 44 return []byte{}, nil 45 } 46 47 func (s *storeChild) Rollback(req *types.ReqHash) ([]byte, error) { 48 return []byte{}, nil 49 } 50 51 func (s *storeChild) Del(req *types.StoreDel) ([]byte, error) { 52 return []byte{}, nil 53 } 54 55 func (s *storeChild) IterateRangeByStateHash(statehash []byte, start []byte, end []byte, ascending bool, fn func(key, value []byte) bool) { 56 57 } 58 59 func (s *storeChild) ProcEvent(msg *queue.Message) {} 60 61 func init() { 62 log.SetLogLevel("error") 63 } 64 65 func TestBaseStore_NewClose(t *testing.T) { 66 os.RemoveAll(storecfg0.DbPath) 67 store := NewBaseStore(storecfg0) 68 assert.NotNil(t, store) 69 70 db := store.GetDB() 71 assert.NotNil(t, db) 72 73 store.Close() 74 } 75 76 func TestBaseStore_Queue(t *testing.T) { 77 os.RemoveAll(storecfg1.DbPath) 78 store := NewBaseStore(storecfg1) 79 assert.NotNil(t, store) 80 81 var q = queue.New("channel") 82 store.SetQueueClient(q.Client()) 83 queueClinet := store.GetQueueClient() 84 85 child := &storeChild{} 86 store.SetChild(child) 87 88 var kv []*types.KeyValue 89 kv = append(kv, &types.KeyValue{Key: []byte("k1"), Value: []byte("v1")}) 90 kv = append(kv, &types.KeyValue{Key: []byte("k2"), Value: []byte("v2")}) 91 datas := &types.StoreSet{ 92 StateHash: EmptyRoot[:], 93 KV: kv, 94 } 95 set := &types.StoreSetWithSync{Storeset: datas, Sync: true} 96 msg := queueClinet.NewMessage("store", types.EventStoreSet, set) 97 err := queueClinet.Send(msg, true) 98 assert.Nil(t, err) 99 resp, err := queueClinet.Wait(msg) 100 assert.Nil(t, err) 101 assert.NotNil(t, resp) 102 assert.Equal(t, int64(types.EventStoreSetReply), resp.Ty) 103 104 get := &types.StoreGet{StateHash: EmptyRoot[:], Keys: [][]byte{}} 105 msg = queueClinet.NewMessage("store", types.EventStoreGet, get) 106 err = queueClinet.Send(msg, true) 107 assert.Nil(t, err) 108 resp, err = queueClinet.Wait(msg) 109 assert.Nil(t, err) 110 assert.NotNil(t, resp) 111 assert.Equal(t, int64(types.EventStoreGetReply), resp.Ty) 112 113 memset := set 114 msg = queueClinet.NewMessage("store", types.EventStoreMemSet, memset) 115 err = queueClinet.Send(msg, true) 116 assert.Nil(t, err) 117 resp, err = queueClinet.Wait(msg) 118 assert.Nil(t, err) 119 assert.NotNil(t, resp) 120 assert.Equal(t, int64(types.EventStoreSetReply), resp.Ty) 121 122 commit := &types.ReqHash{Hash: EmptyRoot[:]} 123 msg = queueClinet.NewMessage("store", types.EventStoreCommit, commit) 124 err = queueClinet.Send(msg, true) 125 assert.Nil(t, err) 126 resp, err = queueClinet.Wait(msg) 127 assert.Nil(t, err) 128 assert.NotNil(t, resp) 129 assert.Equal(t, int64(types.EventStoreCommit), resp.Ty) 130 131 rollback := &types.ReqHash{Hash: EmptyRoot[:]} 132 msg = queueClinet.NewMessage("store", types.EventStoreRollback, rollback) 133 err = queueClinet.Send(msg, true) 134 assert.Nil(t, err) 135 resp, err = queueClinet.Wait(msg) 136 assert.Nil(t, err) 137 assert.NotNil(t, resp) 138 assert.Equal(t, int64(types.EventStoreRollback), resp.Ty) 139 140 totalCoins := &types.IterateRangeByStateHash{ 141 StateHash: EmptyRoot[:], 142 Start: []byte(""), 143 End: []byte(""), 144 Count: 100, 145 } 146 msg = queueClinet.NewMessage("store", types.EventStoreGetTotalCoins, totalCoins) 147 err = queueClinet.Send(msg, true) 148 assert.Nil(t, err) 149 resp, err = queueClinet.Wait(msg) 150 assert.Nil(t, err) 151 assert.NotNil(t, resp) 152 assert.Equal(t, int64(types.EventGetTotalCoinsReply), resp.Ty) 153 154 del := &types.StoreDel{StateHash: EmptyRoot[:], Height: 0} 155 msg = queueClinet.NewMessage("store", types.EventStoreDel, del) 156 err = queueClinet.Send(msg, true) 157 assert.Nil(t, err) 158 resp, err = queueClinet.Wait(msg) 159 assert.Nil(t, err) 160 assert.NotNil(t, resp) 161 assert.Equal(t, int64(types.EventStoreDel), resp.Ty) 162 163 list := &types.StoreList{StateHash: EmptyRoot[:]} 164 msg = queueClinet.NewMessage("store", types.EventStoreList, list) 165 err = queueClinet.Send(msg, true) 166 assert.Nil(t, err) 167 resp, err = queueClinet.Wait(msg) 168 assert.Nil(t, err) 169 assert.NotNil(t, resp) 170 assert.Equal(t, int64(types.EventStoreListReply), resp.Ty) 171 172 } 173 174 func TestSubStore(t *testing.T) { 175 storelistQuery := NewStoreListQuery(&storeChild{}, &types.StoreList{StateHash: EmptyRoot[:], Mode: 1}) 176 ok := storelistQuery.IterateCallBack([]byte("abc"), nil) 177 _ = storelistQuery.Run() 178 assert.True(t, ok) 179 180 storelistQuery = NewStoreListQuery(&storeChild{}, &types.StoreList{StateHash: EmptyRoot[:], Count: 2, Mode: 1}) 181 ok = storelistQuery.IterateCallBack([]byte("abc"), nil) 182 assert.False(t, ok) 183 184 storelistQuery = NewStoreListQuery(&storeChild{}, &types.StoreList{StateHash: EmptyRoot[:], Suffix: []byte("bc"), Mode: 2}) 185 ok = storelistQuery.IterateCallBack([]byte("abc"), nil) 186 assert.True(t, ok) 187 } 188 189 func TestRegAndLoad(t *testing.T) { 190 Reg("test", func(cfg *types.Store, sub []byte, turingchaincfg *types.TuringchainConfig) queue.Module { 191 return nil 192 }) 193 194 _, err := Load("test") 195 assert.NoError(t, err) 196 197 _, err = Load("test2") 198 assert.Equal(t, types.ErrNotFound, err) 199 }