github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/exchange/bitswap/notifications/notifications_test.go (about)

     1  package notifications
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
     9  	blocks "github.com/jbenet/go-ipfs/blocks"
    10  )
    11  
    12  func TestPublishSubscribe(t *testing.T) {
    13  	blockSent := blocks.NewBlock([]byte("Greetings from The Interval"))
    14  
    15  	n := New()
    16  	defer n.Shutdown()
    17  	ch := n.Subscribe(context.Background(), blockSent.Key())
    18  
    19  	n.Publish(*blockSent)
    20  	blockRecvd, ok := <-ch
    21  	if !ok {
    22  		t.Fail()
    23  	}
    24  
    25  	assertBlocksEqual(t, blockRecvd, *blockSent)
    26  
    27  }
    28  
    29  func TestCarryOnWhenDeadlineExpires(t *testing.T) {
    30  
    31  	impossibleDeadline := time.Nanosecond
    32  	fastExpiringCtx, _ := context.WithTimeout(context.Background(), impossibleDeadline)
    33  
    34  	n := New()
    35  	defer n.Shutdown()
    36  	block := blocks.NewBlock([]byte("A Missed Connection"))
    37  	blockChannel := n.Subscribe(fastExpiringCtx, block.Key())
    38  
    39  	assertBlockChannelNil(t, blockChannel)
    40  }
    41  
    42  func assertBlockChannelNil(t *testing.T, blockChannel <-chan blocks.Block) {
    43  	_, ok := <-blockChannel
    44  	if ok {
    45  		t.Fail()
    46  	}
    47  }
    48  
    49  func assertBlocksEqual(t *testing.T, a, b blocks.Block) {
    50  	if !bytes.Equal(a.Data, b.Data) {
    51  		t.Fail()
    52  	}
    53  	if a.Key() != b.Key() {
    54  		t.Fail()
    55  	}
    56  }