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