github.com/dominant-strategies/go-quai@v0.28.2/ethdb/dbtest/testsuite.go (about) 1 // Copyright 2019 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 dbtest 18 19 import ( 20 "bytes" 21 "reflect" 22 "sort" 23 "testing" 24 25 "github.com/dominant-strategies/go-quai/ethdb" 26 ) 27 28 // TestDatabaseSuite runs a suite of tests against a KeyValueStore database 29 // implementation. 30 func TestDatabaseSuite(t *testing.T, New func() ethdb.KeyValueStore) { 31 t.Run("Iterator", func(t *testing.T) { 32 tests := []struct { 33 content map[string]string 34 prefix string 35 start string 36 order []string 37 }{ 38 // Empty databases should be iterable 39 {map[string]string{}, "", "", nil}, 40 {map[string]string{}, "non-existent-prefix", "", nil}, 41 42 // Single-item databases should be iterable 43 {map[string]string{"key": "val"}, "", "", []string{"key"}}, 44 {map[string]string{"key": "val"}, "k", "", []string{"key"}}, 45 {map[string]string{"key": "val"}, "l", "", nil}, 46 47 // Multi-item databases should be fully iterable 48 { 49 map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"}, 50 "", "", 51 []string{"k1", "k2", "k3", "k4", "k5"}, 52 }, 53 { 54 map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"}, 55 "k", "", 56 []string{"k1", "k2", "k3", "k4", "k5"}, 57 }, 58 { 59 map[string]string{"k1": "v1", "k5": "v5", "k2": "v2", "k4": "v4", "k3": "v3"}, 60 "l", "", 61 nil, 62 }, 63 // Multi-item databases should be prefix-iterable 64 { 65 map[string]string{ 66 "ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3", 67 "kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3", 68 }, 69 "ka", "", 70 []string{"ka1", "ka2", "ka3", "ka4", "ka5"}, 71 }, 72 { 73 map[string]string{ 74 "ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3", 75 "kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3", 76 }, 77 "kc", "", 78 nil, 79 }, 80 // Multi-item databases should be prefix-iterable with start position 81 { 82 map[string]string{ 83 "ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3", 84 "kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3", 85 }, 86 "ka", "3", 87 []string{"ka3", "ka4", "ka5"}, 88 }, 89 { 90 map[string]string{ 91 "ka1": "va1", "ka5": "va5", "ka2": "va2", "ka4": "va4", "ka3": "va3", 92 "kb1": "vb1", "kb5": "vb5", "kb2": "vb2", "kb4": "vb4", "kb3": "vb3", 93 }, 94 "ka", "8", 95 nil, 96 }, 97 } 98 for i, tt := range tests { 99 // Create the key-value data store 100 db := New() 101 for key, val := range tt.content { 102 if err := db.Put([]byte(key), []byte(val)); err != nil { 103 t.Fatalf("test %d: failed to insert item %s:%s into database: %v", i, key, val, err) 104 } 105 } 106 // Iterate over the database with the given configs and verify the results 107 it, idx := db.NewIterator([]byte(tt.prefix), []byte(tt.start)), 0 108 for it.Next() { 109 if len(tt.order) <= idx { 110 t.Errorf("test %d: prefix=%q more items than expected: checking idx=%d (key %q), expecting len=%d", i, tt.prefix, idx, it.Key(), len(tt.order)) 111 break 112 } 113 if !bytes.Equal(it.Key(), []byte(tt.order[idx])) { 114 t.Errorf("test %d: item %d: key mismatch: have %s, want %s", i, idx, string(it.Key()), tt.order[idx]) 115 } 116 if !bytes.Equal(it.Value(), []byte(tt.content[tt.order[idx]])) { 117 t.Errorf("test %d: item %d: value mismatch: have %s, want %s", i, idx, string(it.Value()), tt.content[tt.order[idx]]) 118 } 119 idx++ 120 } 121 if err := it.Error(); err != nil { 122 t.Errorf("test %d: iteration failed: %v", i, err) 123 } 124 if idx != len(tt.order) { 125 t.Errorf("test %d: iteration terminated prematurely: have %d, want %d", i, idx, len(tt.order)) 126 } 127 db.Close() 128 } 129 }) 130 131 t.Run("IteratorWith", func(t *testing.T) { 132 db := New() 133 defer db.Close() 134 135 keys := []string{"1", "2", "3", "4", "6", "10", "11", "12", "20", "21", "22"} 136 sort.Strings(keys) // 1, 10, 11, etc 137 138 for _, k := range keys { 139 if err := db.Put([]byte(k), nil); err != nil { 140 t.Fatal(err) 141 } 142 } 143 144 { 145 it := db.NewIterator(nil, nil) 146 got, want := iterateKeys(it), keys 147 if err := it.Error(); err != nil { 148 t.Fatal(err) 149 } 150 if !reflect.DeepEqual(got, want) { 151 t.Errorf("Iterator: got: %s; want: %s", got, want) 152 } 153 } 154 155 { 156 it := db.NewIterator([]byte("1"), nil) 157 got, want := iterateKeys(it), []string{"1", "10", "11", "12"} 158 if err := it.Error(); err != nil { 159 t.Fatal(err) 160 } 161 if !reflect.DeepEqual(got, want) { 162 t.Errorf("IteratorWith(1,nil): got: %s; want: %s", got, want) 163 } 164 } 165 166 { 167 it := db.NewIterator([]byte("5"), nil) 168 got, want := iterateKeys(it), []string{} 169 if err := it.Error(); err != nil { 170 t.Fatal(err) 171 } 172 if !reflect.DeepEqual(got, want) { 173 t.Errorf("IteratorWith(5,nil): got: %s; want: %s", got, want) 174 } 175 } 176 177 { 178 it := db.NewIterator(nil, []byte("2")) 179 got, want := iterateKeys(it), []string{"2", "20", "21", "22", "3", "4", "6"} 180 if err := it.Error(); err != nil { 181 t.Fatal(err) 182 } 183 if !reflect.DeepEqual(got, want) { 184 t.Errorf("IteratorWith(nil,2): got: %s; want: %s", got, want) 185 } 186 } 187 188 { 189 it := db.NewIterator(nil, []byte("5")) 190 got, want := iterateKeys(it), []string{"6"} 191 if err := it.Error(); err != nil { 192 t.Fatal(err) 193 } 194 if !reflect.DeepEqual(got, want) { 195 t.Errorf("IteratorWith(nil,5): got: %s; want: %s", got, want) 196 } 197 } 198 }) 199 200 t.Run("KeyValueOperations", func(t *testing.T) { 201 db := New() 202 defer db.Close() 203 204 key := []byte("foo") 205 206 if got, err := db.Has(key); err != nil { 207 t.Error(err) 208 } else if got { 209 t.Errorf("wrong value: %t", got) 210 } 211 212 value := []byte("hello world") 213 if err := db.Put(key, value); err != nil { 214 t.Error(err) 215 } 216 217 if got, err := db.Has(key); err != nil { 218 t.Error(err) 219 } else if !got { 220 t.Errorf("wrong value: %t", got) 221 } 222 223 if got, err := db.Get(key); err != nil { 224 t.Error(err) 225 } else if !bytes.Equal(got, value) { 226 t.Errorf("wrong value: %q", got) 227 } 228 229 if err := db.Delete(key); err != nil { 230 t.Error(err) 231 } 232 233 if got, err := db.Has(key); err != nil { 234 t.Error(err) 235 } else if got { 236 t.Errorf("wrong value: %t", got) 237 } 238 }) 239 240 t.Run("Batch", func(t *testing.T) { 241 db := New() 242 defer db.Close() 243 244 b := db.NewBatch() 245 for _, k := range []string{"1", "2", "3", "4"} { 246 if err := b.Put([]byte(k), nil); err != nil { 247 t.Fatal(err) 248 } 249 } 250 251 if has, err := db.Has([]byte("1")); err != nil { 252 t.Fatal(err) 253 } else if has { 254 t.Error("db contains element before batch write") 255 } 256 257 if err := b.Write(); err != nil { 258 t.Fatal(err) 259 } 260 261 { 262 it := db.NewIterator(nil, nil) 263 if got, want := iterateKeys(it), []string{"1", "2", "3", "4"}; !reflect.DeepEqual(got, want) { 264 t.Errorf("got: %s; want: %s", got, want) 265 } 266 } 267 268 b.Reset() 269 270 // Mix writes and deletes in batch 271 b.Put([]byte("5"), nil) 272 b.Delete([]byte("1")) 273 b.Put([]byte("6"), nil) 274 b.Delete([]byte("3")) 275 b.Put([]byte("3"), nil) 276 277 if err := b.Write(); err != nil { 278 t.Fatal(err) 279 } 280 281 { 282 it := db.NewIterator(nil, nil) 283 if got, want := iterateKeys(it), []string{"2", "3", "4", "5", "6"}; !reflect.DeepEqual(got, want) { 284 t.Errorf("got: %s; want: %s", got, want) 285 } 286 } 287 }) 288 289 t.Run("BatchReplay", func(t *testing.T) { 290 db := New() 291 defer db.Close() 292 293 want := []string{"1", "2", "3", "4"} 294 b := db.NewBatch() 295 for _, k := range want { 296 if err := b.Put([]byte(k), nil); err != nil { 297 t.Fatal(err) 298 } 299 } 300 301 b2 := db.NewBatch() 302 if err := b.Replay(b2); err != nil { 303 t.Fatal(err) 304 } 305 306 if err := b2.Replay(db); err != nil { 307 t.Fatal(err) 308 } 309 310 it := db.NewIterator(nil, nil) 311 if got := iterateKeys(it); !reflect.DeepEqual(got, want) { 312 t.Errorf("got: %s; want: %s", got, want) 313 } 314 }) 315 316 // Flush more modifications into the database, ensure the snapshot 317 // isn't affected. 318 var ( 319 update = map[string]string{"k1": "v1-b", "k3": "v3-b"} 320 insert = map[string]string{"k5": "v5-b"} 321 delete = map[string]string{"k2": ""} 322 ) 323 for k, v := range update { 324 db.Put([]byte(k), []byte(v)) 325 } 326 for k, v := range insert { 327 db.Put([]byte(k), []byte(v)) 328 } 329 for k := range delete { 330 db.Delete([]byte(k)) 331 } 332 for k, v := range initial { 333 got, err := snapshot.Get([]byte(k)) 334 if err != nil { 335 t.Fatal(err) 336 } 337 if !bytes.Equal(got, []byte(v)) { 338 t.Fatalf("Unexpected value want: %v, got %v", v, got) 339 } 340 } 341 for k := range insert { 342 got, err := snapshot.Get([]byte(k)) 343 if err == nil || len(got) != 0 { 344 t.Fatal("Unexpected value") 345 } 346 } 347 for k := range delete { 348 got, err := snapshot.Get([]byte(k)) 349 if err != nil || len(got) == 0 { 350 t.Fatal("Unexpected deletion") 351 } 352 } 353 }) 354 355 t.Run("OperatonsAfterClose", func(t *testing.T) { 356 db := New() 357 db.Put([]byte("key"), []byte("value")) 358 db.Close() 359 if _, err := db.Get([]byte("key")); err == nil { 360 t.Fatalf("expected error on Get after Close") 361 } 362 if _, err := db.Has([]byte("key")); err == nil { 363 t.Fatalf("expected error on Get after Close") 364 } 365 if err := db.Put([]byte("key2"), []byte("value2")); err == nil { 366 t.Fatalf("expected error on Put after Close") 367 } 368 if err := db.Delete([]byte("key")); err == nil { 369 t.Fatalf("expected error on Delete after Close") 370 } 371 372 b := db.NewBatch() 373 if err := b.Put([]byte("batchkey"), []byte("batchval")); err != nil { 374 t.Fatalf("expected no error on batch.Put after Close, got %v", err) 375 } 376 if err := b.Write(); err == nil { 377 t.Fatalf("expected error on batch.Write after Close") 378 } 379 }) 380 } 381 382 // BenchDatabaseSuite runs a suite of benchmarks against a KeyValueStore database 383 // implementation. 384 func BenchDatabaseSuite(b *testing.B, New func() ethdb.KeyValueStore) { 385 var ( 386 keys, vals = makeDataset(1_000_000, 32, 32, false) 387 sKeys, sVals = makeDataset(1_000_000, 32, 32, true) 388 ) 389 // Run benchmarks sequentially 390 b.Run("Write", func(b *testing.B) { 391 benchWrite := func(b *testing.B, keys, vals [][]byte) { 392 b.ResetTimer() 393 b.ReportAllocs() 394 395 db := New() 396 defer db.Close() 397 398 for i := 0; i < len(keys); i++ { 399 db.Put(keys[i], vals[i]) 400 } 401 } 402 b.Run("WriteSorted", func(b *testing.B) { 403 benchWrite(b, sKeys, sVals) 404 }) 405 b.Run("WriteRandom", func(b *testing.B) { 406 benchWrite(b, keys, vals) 407 }) 408 }) 409 b.Run("Read", func(b *testing.B) { 410 benchRead := func(b *testing.B, keys, vals [][]byte) { 411 db := New() 412 defer db.Close() 413 414 for i := 0; i < len(keys); i++ { 415 db.Put(keys[i], vals[i]) 416 } 417 b.ResetTimer() 418 b.ReportAllocs() 419 420 for i := 0; i < len(keys); i++ { 421 db.Get(keys[i]) 422 } 423 } 424 b.Run("ReadSorted", func(b *testing.B) { 425 benchRead(b, sKeys, sVals) 426 }) 427 b.Run("ReadRandom", func(b *testing.B) { 428 benchRead(b, keys, vals) 429 }) 430 }) 431 b.Run("Iteration", func(b *testing.B) { 432 benchIteration := func(b *testing.B, keys, vals [][]byte) { 433 db := New() 434 defer db.Close() 435 436 for i := 0; i < len(keys); i++ { 437 db.Put(keys[i], vals[i]) 438 } 439 b.ResetTimer() 440 b.ReportAllocs() 441 442 it := db.NewIterator(nil, nil) 443 for it.Next() { 444 } 445 it.Release() 446 } 447 b.Run("IterationSorted", func(b *testing.B) { 448 benchIteration(b, sKeys, sVals) 449 }) 450 b.Run("IterationRandom", func(b *testing.B) { 451 benchIteration(b, keys, vals) 452 }) 453 }) 454 b.Run("BatchWrite", func(b *testing.B) { 455 benchBatchWrite := func(b *testing.B, keys, vals [][]byte) { 456 b.ResetTimer() 457 b.ReportAllocs() 458 459 db := New() 460 defer db.Close() 461 462 batch := db.NewBatch() 463 for i := 0; i < len(keys); i++ { 464 batch.Put(keys[i], vals[i]) 465 } 466 batch.Write() 467 } 468 b.Run("BenchWriteSorted", func(b *testing.B) { 469 benchBatchWrite(b, sKeys, sVals) 470 }) 471 b.Run("BenchWriteRandom", func(b *testing.B) { 472 benchBatchWrite(b, keys, vals) 473 }) 474 }) 475 >>>>>>> 99394adcb (ethdb/pebble: prevent shutdown-panic (#27238)) 476 } 477 478 func iterateKeys(it ethdb.Iterator) []string { 479 keys := []string{} 480 for it.Next() { 481 keys = append(keys, string(it.Key())) 482 } 483 sort.Strings(keys) 484 it.Release() 485 return keys 486 }