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