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