common_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "bytes"
  19. "crypto/rand"
  20. "fmt"
  21. "io"
  22. "sync"
  23. "testing"
  24. "github.com/ethereum/go-ethereum/log"
  25. )
  26. type brokenLimitedReader struct {
  27. lr io.Reader
  28. errAt int
  29. off int
  30. size int
  31. }
  32. func brokenLimitReader(data io.Reader, size int, errAt int) *brokenLimitedReader {
  33. return &brokenLimitedReader{
  34. lr: data,
  35. errAt: errAt,
  36. size: size,
  37. }
  38. }
  39. func testDataReader(l int) (r io.Reader) {
  40. return io.LimitReader(rand.Reader, int64(l))
  41. }
  42. func (self *brokenLimitedReader) Read(buf []byte) (int, error) {
  43. if self.off+len(buf) > self.errAt {
  44. return 0, fmt.Errorf("Broken reader")
  45. }
  46. self.off += len(buf)
  47. return self.lr.Read(buf)
  48. }
  49. func testDataReaderAndSlice(l int) (r io.Reader, slice []byte) {
  50. slice = make([]byte, l)
  51. if _, err := rand.Read(slice); err != nil {
  52. panic("rand error")
  53. }
  54. r = io.LimitReader(bytes.NewReader(slice), int64(l))
  55. return
  56. }
  57. func testStore(m ChunkStore, l int64, branches int64, t *testing.T) {
  58. chunkC := make(chan *Chunk)
  59. go func() {
  60. for chunk := range chunkC {
  61. m.Put(chunk)
  62. if chunk.wg != nil {
  63. chunk.wg.Done()
  64. }
  65. }
  66. }()
  67. chunker := NewTreeChunker(&ChunkerParams{
  68. Branches: branches,
  69. Hash: SHA3Hash,
  70. })
  71. swg := &sync.WaitGroup{}
  72. key, _ := chunker.Split(rand.Reader, l, chunkC, swg, nil)
  73. swg.Wait()
  74. close(chunkC)
  75. chunkC = make(chan *Chunk)
  76. quit := make(chan bool)
  77. go func() {
  78. for ch := range chunkC {
  79. go func(chunk *Chunk) {
  80. storedChunk, err := m.Get(chunk.Key)
  81. if err == notFound {
  82. log.Trace(fmt.Sprintf("chunk '%v' not found", chunk.Key.Log()))
  83. } else if err != nil {
  84. log.Trace(fmt.Sprintf("error retrieving chunk %v: %v", chunk.Key.Log(), err))
  85. } else {
  86. chunk.SData = storedChunk.SData
  87. chunk.Size = storedChunk.Size
  88. }
  89. log.Trace(fmt.Sprintf("chunk '%v' not found", chunk.Key.Log()))
  90. close(chunk.C)
  91. }(ch)
  92. }
  93. close(quit)
  94. }()
  95. r := chunker.Join(key, chunkC)
  96. b := make([]byte, l)
  97. n, err := r.ReadAt(b, 0)
  98. if err != io.EOF {
  99. t.Fatalf("read error (%v/%v) %v", n, l, err)
  100. }
  101. close(chunkC)
  102. <-quit
  103. }