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. "io"
  21. "sync"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/logger"
  24. "github.com/ethereum/go-ethereum/logger/glog"
  25. )
  26. type limitedReader struct {
  27. r io.Reader
  28. off int64
  29. size int64
  30. }
  31. func limitReader(r io.Reader, size int) *limitedReader {
  32. return &limitedReader{r, 0, int64(size)}
  33. }
  34. func (self *limitedReader) Read(buf []byte) (int, error) {
  35. limit := int64(len(buf))
  36. left := self.size - self.off
  37. if limit >= left {
  38. limit = left
  39. }
  40. n, err := self.r.Read(buf[:limit])
  41. if err == nil && limit == left {
  42. err = io.EOF
  43. }
  44. self.off += int64(n)
  45. return n, err
  46. }
  47. func testDataReader(l int) (r io.Reader) {
  48. return limitReader(rand.Reader, l)
  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 = limitReader(bytes.NewReader(slice), 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, err := 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. }