github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/blockservice/worker/worker_test.go (about)

     1  package worker
     2  
     3  import (
     4  	blocks "github.com/ipfs/go-ipfs/blocks"
     5  	"testing"
     6  )
     7  
     8  func TestStartClose(t *testing.T) {
     9  	numRuns := 50
    10  	if testing.Short() {
    11  		numRuns = 5
    12  	}
    13  	for i := 0; i < numRuns; i++ {
    14  		w := NewWorker(nil, DefaultConfig)
    15  		w.Close()
    16  	}
    17  }
    18  
    19  func TestQueueDeduplication(t *testing.T) {
    20  	numUniqBlocks := 5 // arbitrary
    21  
    22  	var firstBatch []*blocks.Block
    23  	for i := 0; i < numUniqBlocks; i++ {
    24  		firstBatch = append(firstBatch, blockFromInt(i))
    25  	}
    26  
    27  	// to get different pointer values and prevent the implementation from
    28  	// cheating. The impl must check equality using Key.
    29  	var secondBatch []*blocks.Block
    30  	for i := 0; i < numUniqBlocks; i++ {
    31  		secondBatch = append(secondBatch, blockFromInt(i))
    32  	}
    33  	var workQueue BlockList
    34  
    35  	for _, b := range append(firstBatch, secondBatch...) {
    36  		workQueue.Push(b)
    37  	}
    38  	for i := 0; i < numUniqBlocks; i++ {
    39  		b := workQueue.Pop()
    40  		if b.Key() != firstBatch[i].Key() {
    41  			t.Fatal("list is not FIFO")
    42  		}
    43  	}
    44  	if b := workQueue.Pop(); b != nil {
    45  		t.Fatal("the workQueue did not de-duplicate the blocks")
    46  	}
    47  }
    48  
    49  func TestPushPopPushPop(t *testing.T) {
    50  	var workQueue BlockList
    51  	orig := blockFromInt(1)
    52  	dup := blockFromInt(1)
    53  	workQueue.PushFront(orig)
    54  	workQueue.Pop()
    55  	workQueue.Push(dup)
    56  	if workQueue.Len() != 1 {
    57  		t.Fatal("the block list's internal state is corrupt")
    58  	}
    59  }
    60  
    61  func blockFromInt(i int) *blocks.Block {
    62  	return blocks.NewBlock([]byte(string(i)))
    63  }