database_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. package ethdb_test
  17. import (
  18. "bytes"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "strconv"
  23. "sync"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. )
  27. func newTestLDB() (*ethdb.LDBDatabase, func()) {
  28. dirname, err := ioutil.TempDir(os.TempDir(), "ethdb_test_")
  29. if err != nil {
  30. panic("failed to create test file: " + err.Error())
  31. }
  32. db, err := ethdb.NewLDBDatabase(dirname, 0, 0)
  33. if err != nil {
  34. panic("failed to create test database: " + err.Error())
  35. }
  36. return db, func() {
  37. db.Close()
  38. os.RemoveAll(dirname)
  39. }
  40. }
  41. var test_values = []string{"", "a", "1251", "\x00123\x00"}
  42. func TestLDB_PutGet(t *testing.T) {
  43. db, remove := newTestLDB()
  44. defer remove()
  45. testPutGet(db, t)
  46. }
  47. func TestMemoryDB_PutGet(t *testing.T) {
  48. testPutGet(ethdb.NewMemDatabase(), t)
  49. }
  50. func testPutGet(db ethdb.Database, t *testing.T) {
  51. t.Parallel()
  52. for _, v := range test_values {
  53. err := db.Put([]byte(v), []byte(v))
  54. if err != nil {
  55. t.Fatalf("put failed: %v", err)
  56. }
  57. }
  58. for _, v := range test_values {
  59. data, err := db.Get([]byte(v))
  60. if err != nil {
  61. t.Fatalf("get failed: %v", err)
  62. }
  63. if !bytes.Equal(data, []byte(v)) {
  64. t.Fatalf("get returned wrong result, got %q expected %q", string(data), v)
  65. }
  66. }
  67. for _, v := range test_values {
  68. err := db.Put([]byte(v), []byte("?"))
  69. if err != nil {
  70. t.Fatalf("put override failed: %v", err)
  71. }
  72. }
  73. for _, v := range test_values {
  74. data, err := db.Get([]byte(v))
  75. if err != nil {
  76. t.Fatalf("get failed: %v", err)
  77. }
  78. if !bytes.Equal(data, []byte("?")) {
  79. t.Fatalf("get returned wrong result, got %q expected ?", string(data))
  80. }
  81. }
  82. for _, v := range test_values {
  83. orig, err := db.Get([]byte(v))
  84. if err != nil {
  85. t.Fatalf("get failed: %v", err)
  86. }
  87. orig[0] = byte(0xff)
  88. data, err := db.Get([]byte(v))
  89. if err != nil {
  90. t.Fatalf("get failed: %v", err)
  91. }
  92. if !bytes.Equal(data, []byte("?")) {
  93. t.Fatalf("get returned wrong result, got %q expected ?", string(data))
  94. }
  95. }
  96. for _, v := range test_values {
  97. err := db.Delete([]byte(v))
  98. if err != nil {
  99. t.Fatalf("delete %q failed: %v", v, err)
  100. }
  101. }
  102. for _, v := range test_values {
  103. _, err := db.Get([]byte(v))
  104. if err == nil {
  105. t.Fatalf("got deleted value %q", v)
  106. }
  107. }
  108. }
  109. func TestLDB_ParallelPutGet(t *testing.T) {
  110. db, remove := newTestLDB()
  111. defer remove()
  112. testParallelPutGet(db, t)
  113. }
  114. func TestMemoryDB_ParallelPutGet(t *testing.T) {
  115. testParallelPutGet(ethdb.NewMemDatabase(), t)
  116. }
  117. func testParallelPutGet(db ethdb.Database, t *testing.T) {
  118. const n = 8
  119. var pending sync.WaitGroup
  120. pending.Add(n)
  121. for i := 0; i < n; i++ {
  122. go func(key string) {
  123. defer pending.Done()
  124. err := db.Put([]byte(key), []byte("v"+key))
  125. if err != nil {
  126. panic("put failed: " + err.Error())
  127. }
  128. }(strconv.Itoa(i))
  129. }
  130. pending.Wait()
  131. pending.Add(n)
  132. for i := 0; i < n; i++ {
  133. go func(key string) {
  134. defer pending.Done()
  135. data, err := db.Get([]byte(key))
  136. if err != nil {
  137. panic("get failed: " + err.Error())
  138. }
  139. if !bytes.Equal(data, []byte("v"+key)) {
  140. panic(fmt.Sprintf("get failed, got %q expected %q", []byte(data), []byte("v"+key)))
  141. }
  142. }(strconv.Itoa(i))
  143. }
  144. pending.Wait()
  145. pending.Add(n)
  146. for i := 0; i < n; i++ {
  147. go func(key string) {
  148. defer pending.Done()
  149. err := db.Delete([]byte(key))
  150. if err != nil {
  151. panic("delete failed: " + err.Error())
  152. }
  153. }(strconv.Itoa(i))
  154. }
  155. pending.Wait()
  156. pending.Add(n)
  157. for i := 0; i < n; i++ {
  158. go func(key string) {
  159. defer pending.Done()
  160. _, err := db.Get([]byte(key))
  161. if err == nil {
  162. panic("get succeeded")
  163. }
  164. }(strconv.Itoa(i))
  165. }
  166. pending.Wait()
  167. }