github.com/annchain/OG@v0.0.9/ogdb/database_test.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package ogdb_test 18 19 import ( 20 "bytes" 21 "fmt" 22 "io/ioutil" 23 "os" 24 "strconv" 25 "sync" 26 "testing" 27 28 "github.com/annchain/OG/ogdb" 29 ) 30 31 func newTestLDB() (*ogdb.LevelDB, func()) { 32 dirname, err := ioutil.TempDir(os.TempDir(), "ogdb_test_") 33 if err != nil { 34 panic("failed to create test file: " + err.Error()) 35 } 36 db, err := ogdb.NewLevelDB(dirname, 0, 0) 37 if err != nil { 38 panic("failed to create test database: " + err.Error()) 39 } 40 41 return db, func() { 42 db.Close() 43 os.RemoveAll(dirname) 44 } 45 } 46 47 var test_values = []string{"", "a", "1251", "\x00123\x00"} 48 49 func TestLDB_PutGet(t *testing.T) { 50 db, remove := newTestLDB() 51 defer remove() 52 testPutGet(db, t) 53 } 54 55 func TestMemoryDB_PutGet(t *testing.T) { 56 testPutGet(ogdb.NewMemDatabase(), t) 57 } 58 59 func testPutGet(db ogdb.Database, t *testing.T) { 60 t.Parallel() 61 62 for _, v := range test_values { 63 err := db.Put([]byte(v), []byte(v)) 64 if err != nil { 65 t.Fatalf("put failed: %v", err) 66 } 67 } 68 69 for _, v := range test_values { 70 data, err := db.Get([]byte(v)) 71 if err != nil { 72 t.Fatalf("get failed: %v", err) 73 } 74 if !bytes.Equal(data, []byte(v)) { 75 t.Fatalf("get returned wrong result, got %q expected %q", string(data), v) 76 } 77 } 78 79 for _, v := range test_values { 80 err := db.Put([]byte(v), []byte("?")) 81 if err != nil { 82 t.Fatalf("put override failed: %v", err) 83 } 84 } 85 86 for _, v := range test_values { 87 data, err := db.Get([]byte(v)) 88 if err != nil { 89 t.Fatalf("get failed: %v", err) 90 } 91 if !bytes.Equal(data, []byte("?")) { 92 t.Fatalf("get returned wrong result, got %q expected ?", string(data)) 93 } 94 } 95 96 for _, v := range test_values { 97 orig, err := db.Get([]byte(v)) 98 if err != nil { 99 t.Fatalf("get failed: %v", err) 100 } 101 orig[0] = byte(0xff) 102 data, err := db.Get([]byte(v)) 103 if err != nil { 104 t.Fatalf("get failed: %v", err) 105 } 106 if !bytes.Equal(data, []byte("?")) { 107 t.Fatalf("get returned wrong result, got %q expected ?", string(data)) 108 } 109 } 110 111 for _, v := range test_values { 112 err := db.Delete([]byte(v)) 113 if err != nil { 114 t.Fatalf("delete %q failed: %v", v, err) 115 } 116 } 117 118 for _, v := range test_values { 119 _, err := db.Get([]byte(v)) 120 if err == nil { 121 t.Fatalf("got deleted value %q", v) 122 } 123 } 124 } 125 126 func TestLDB_ParallelPutGet(t *testing.T) { 127 db, remove := newTestLDB() 128 defer remove() 129 testParallelPutGet(db, t) 130 } 131 132 func TestMemoryDB_ParallelPutGet(t *testing.T) { 133 testParallelPutGet(ogdb.NewMemDatabase(), t) 134 } 135 136 func testParallelPutGet(db ogdb.Database, t *testing.T) { 137 const n = 8 138 var pending sync.WaitGroup 139 140 pending.Add(n) 141 for i := 0; i < n; i++ { 142 go func(key string) { 143 defer pending.Done() 144 err := db.Put([]byte(key), []byte("v"+key)) 145 if err != nil { 146 panic("put failed: " + err.Error()) 147 } 148 }(strconv.Itoa(i)) 149 } 150 pending.Wait() 151 152 pending.Add(n) 153 for i := 0; i < n; i++ { 154 go func(key string) { 155 defer pending.Done() 156 data, err := db.Get([]byte(key)) 157 if err != nil { 158 panic("get failed: " + err.Error()) 159 } 160 if !bytes.Equal(data, []byte("v"+key)) { 161 panic(fmt.Sprintf("get failed, got %q expected %q", []byte(data), []byte("v"+key))) 162 } 163 }(strconv.Itoa(i)) 164 } 165 pending.Wait() 166 167 pending.Add(n) 168 for i := 0; i < n; i++ { 169 go func(key string) { 170 defer pending.Done() 171 err := db.Delete([]byte(key)) 172 if err != nil { 173 panic("delete failed: " + err.Error()) 174 } 175 }(strconv.Itoa(i)) 176 } 177 pending.Wait() 178 179 pending.Add(n) 180 for i := 0; i < n; i++ { 181 go func(key string) { 182 defer pending.Done() 183 _, err := db.Get([]byte(key)) 184 if err == nil { 185 panic("get succeeded") 186 } 187 }(strconv.Itoa(i)) 188 } 189 pending.Wait() 190 }