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