netstore_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2018 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. "encoding/hex"
  20. "errors"
  21. "io/ioutil"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/swarm/network"
  25. )
  26. var (
  27. errUnknown = errors.New("unknown error")
  28. )
  29. type mockRetrieve struct {
  30. requests map[string]int
  31. }
  32. func NewMockRetrieve() *mockRetrieve {
  33. return &mockRetrieve{requests: make(map[string]int)}
  34. }
  35. func newDummyChunk(addr Address) *Chunk {
  36. chunk := NewChunk(addr, make(chan bool))
  37. chunk.SData = []byte{3, 4, 5}
  38. chunk.Size = 3
  39. return chunk
  40. }
  41. func (m *mockRetrieve) retrieve(ctx context.Context, chunk *Chunk) error {
  42. hkey := hex.EncodeToString(chunk.Addr)
  43. m.requests[hkey] += 1
  44. // on second call return error
  45. if m.requests[hkey] == 2 {
  46. return errUnknown
  47. }
  48. // on third call return data
  49. if m.requests[hkey] == 3 {
  50. *chunk = *newDummyChunk(chunk.Addr)
  51. go func() {
  52. time.Sleep(100 * time.Millisecond)
  53. close(chunk.ReqC)
  54. }()
  55. return nil
  56. }
  57. return nil
  58. }
  59. func TestNetstoreFailedRequest(t *testing.T) {
  60. searchTimeout = 300 * time.Millisecond
  61. // setup
  62. addr := network.RandomAddr() // tested peers peer address
  63. // temp datadir
  64. datadir, err := ioutil.TempDir("", "netstore")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. params := NewDefaultLocalStoreParams()
  69. params.Init(datadir)
  70. params.BaseKey = addr.Over()
  71. localStore, err := NewTestLocalStoreForAddr(params)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. r := NewMockRetrieve()
  76. netStore := NewNetStore(localStore, r.retrieve)
  77. key := Address{}
  78. // first call is done by the retry on ErrChunkNotFound, no need to do it here
  79. // _, err = netStore.Get(key)
  80. // if err == nil || err != ErrChunkNotFound {
  81. // t.Fatalf("expected to get ErrChunkNotFound, but got: %s", err)
  82. // }
  83. // second call
  84. _, err = netStore.Get(context.TODO(), key)
  85. if got := r.requests[hex.EncodeToString(key)]; got != 2 {
  86. t.Fatalf("expected to have called retrieve two times, but got: %v", got)
  87. }
  88. if err != errUnknown {
  89. t.Fatalf("expected to get an unknown error, but got: %s", err)
  90. }
  91. // third call
  92. chunk, err := netStore.Get(context.TODO(), key)
  93. if got := r.requests[hex.EncodeToString(key)]; got != 3 {
  94. t.Fatalf("expected to have called retrieve three times, but got: %v", got)
  95. }
  96. if err != nil || chunk == nil {
  97. t.Fatalf("expected to get a chunk but got: %v, %s", chunk, err)
  98. }
  99. if len(chunk.SData) != 3 {
  100. t.Fatalf("expected to get a chunk with size 3, but got: %v", chunk.SData)
  101. }
  102. }