github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/storage/common_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package storage
    13  
    14  import (
    15  	"bytes"
    16  	"crypto/rand"
    17  	"fmt"
    18  	"io"
    19  	"sync"
    20  	"testing"
    21  
    22  	"github.com/Sberex/go-sberex/log"
    23  )
    24  
    25  type brokenLimitedReader struct {
    26  	lr    io.Reader
    27  	errAt int
    28  	off   int
    29  	size  int
    30  }
    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  
    40  func testDataReader(l int) (r io.Reader) {
    41  	return io.LimitReader(rand.Reader, int64(l))
    42  }
    43  
    44  func (self *brokenLimitedReader) Read(buf []byte) (int, error) {
    45  	if self.off+len(buf) > self.errAt {
    46  		return 0, fmt.Errorf("Broken reader")
    47  	}
    48  	self.off += len(buf)
    49  	return self.lr.Read(buf)
    50  }
    51  
    52  func testDataReaderAndSlice(l int) (r io.Reader, slice []byte) {
    53  	slice = make([]byte, l)
    54  	if _, err := rand.Read(slice); err != nil {
    55  		panic("rand error")
    56  	}
    57  	r = io.LimitReader(bytes.NewReader(slice), int64(l))
    58  	return
    59  }
    60  
    61  func testStore(m ChunkStore, l int64, branches int64, t *testing.T) {
    62  
    63  	chunkC := make(chan *Chunk)
    64  	go func() {
    65  		for chunk := range chunkC {
    66  			m.Put(chunk)
    67  			if chunk.wg != nil {
    68  				chunk.wg.Done()
    69  			}
    70  		}
    71  	}()
    72  	chunker := NewTreeChunker(&ChunkerParams{
    73  		Branches: branches,
    74  		Hash:     SHA3Hash,
    75  	})
    76  	swg := &sync.WaitGroup{}
    77  	key, _ := chunker.Split(rand.Reader, l, chunkC, swg, nil)
    78  	swg.Wait()
    79  	close(chunkC)
    80  	chunkC = make(chan *Chunk)
    81  
    82  	quit := make(chan bool)
    83  
    84  	go func() {
    85  		for ch := range chunkC {
    86  			go func(chunk *Chunk) {
    87  				storedChunk, err := m.Get(chunk.Key)
    88  				if err == notFound {
    89  					log.Trace(fmt.Sprintf("chunk '%v' not found", chunk.Key.Log()))
    90  				} else if err != nil {
    91  					log.Trace(fmt.Sprintf("error retrieving chunk %v: %v", chunk.Key.Log(), err))
    92  				} else {
    93  					chunk.SData = storedChunk.SData
    94  					chunk.Size = storedChunk.Size
    95  				}
    96  				log.Trace(fmt.Sprintf("chunk '%v' not found", chunk.Key.Log()))
    97  				close(chunk.C)
    98  			}(ch)
    99  		}
   100  		close(quit)
   101  	}()
   102  	r := chunker.Join(key, chunkC)
   103  
   104  	b := make([]byte, l)
   105  	n, err := r.ReadAt(b, 0)
   106  	if err != io.EOF {
   107  		t.Fatalf("read error (%v/%v) %v", n, l, err)
   108  	}
   109  	close(chunkC)
   110  	<-quit
   111  }