memstore_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2016 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 storage
  17. import (
  18. "context"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/swarm/log"
  21. )
  22. func newTestMemStore() *MemStore {
  23. storeparams := NewDefaultStoreParams()
  24. return NewMemStore(storeparams, nil)
  25. }
  26. func testMemStoreRandom(n int, t *testing.T) {
  27. m := newTestMemStore()
  28. defer m.Close()
  29. testStoreRandom(m, n, t)
  30. }
  31. func testMemStoreCorrect(n int, t *testing.T) {
  32. m := newTestMemStore()
  33. defer m.Close()
  34. testStoreCorrect(m, n, t)
  35. }
  36. func TestMemStoreRandom_1(t *testing.T) {
  37. testMemStoreRandom(1, t)
  38. }
  39. func TestMemStoreCorrect_1(t *testing.T) {
  40. testMemStoreCorrect(1, t)
  41. }
  42. func TestMemStoreRandom_1k(t *testing.T) {
  43. testMemStoreRandom(1000, t)
  44. }
  45. func TestMemStoreCorrect_1k(t *testing.T) {
  46. testMemStoreCorrect(100, t)
  47. }
  48. func TestMemStoreNotFound(t *testing.T) {
  49. m := newTestMemStore()
  50. defer m.Close()
  51. _, err := m.Get(context.TODO(), ZeroAddr)
  52. if err != ErrChunkNotFound {
  53. t.Errorf("Expected ErrChunkNotFound, got %v", err)
  54. }
  55. }
  56. func benchmarkMemStorePut(n int, b *testing.B) {
  57. m := newTestMemStore()
  58. defer m.Close()
  59. benchmarkStorePut(m, n, b)
  60. }
  61. func benchmarkMemStoreGet(n int, b *testing.B) {
  62. m := newTestMemStore()
  63. defer m.Close()
  64. benchmarkStoreGet(m, n, b)
  65. }
  66. func BenchmarkMemStorePut_500(b *testing.B) {
  67. benchmarkMemStorePut(500, b)
  68. }
  69. func BenchmarkMemStoreGet_500(b *testing.B) {
  70. benchmarkMemStoreGet(500, b)
  71. }
  72. func TestMemStoreAndLDBStore(t *testing.T) {
  73. ldb, cleanup := newLDBStore(t)
  74. ldb.setCapacity(4000)
  75. defer cleanup()
  76. cacheCap := 200
  77. memStore := NewMemStore(NewStoreParams(4000, 200, nil, nil), nil)
  78. tests := []struct {
  79. n int // number of chunks to push to memStore
  80. chunkSize int64 // size of chunk (by default in Swarm - 4096)
  81. }{
  82. {
  83. n: 1,
  84. chunkSize: 4096,
  85. },
  86. {
  87. n: 101,
  88. chunkSize: 4096,
  89. },
  90. {
  91. n: 501,
  92. chunkSize: 4096,
  93. },
  94. {
  95. n: 1100,
  96. chunkSize: 4096,
  97. },
  98. }
  99. for i, tt := range tests {
  100. log.Info("running test", "idx", i, "tt", tt)
  101. var chunks []Chunk
  102. for i := 0; i < tt.n; i++ {
  103. c := GenerateRandomChunk(tt.chunkSize)
  104. chunks = append(chunks, c)
  105. }
  106. for i := 0; i < tt.n; i++ {
  107. err := ldb.Put(context.TODO(), chunks[i])
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. err = memStore.Put(context.TODO(), chunks[i])
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. if got := memStore.cache.Len(); got > cacheCap {
  116. t.Fatalf("expected to get cache capacity less than %v, but got %v", cacheCap, got)
  117. }
  118. }
  119. for i := 0; i < tt.n; i++ {
  120. _, err := memStore.Get(context.TODO(), chunks[i].Address())
  121. if err != nil {
  122. if err == ErrChunkNotFound {
  123. _, err := ldb.Get(context.TODO(), chunks[i].Address())
  124. if err != nil {
  125. t.Fatalf("couldn't get chunk %v from ldb, got error: %v", i, err)
  126. }
  127. } else {
  128. t.Fatalf("got error from memstore: %v", err)
  129. }
  130. }
  131. }
  132. }
  133. }