github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/exchange/bitswap/notifications/notifications_test.go (about)

     1  package notifications
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
     9  	blocks "github.com/ipfs/go-ipfs/blocks"
    10  	blocksutil "github.com/ipfs/go-ipfs/blocks/blocksutil"
    11  	key "github.com/ipfs/go-ipfs/blocks/key"
    12  )
    13  
    14  func TestDuplicates(t *testing.T) {
    15  	b1 := blocks.NewBlock([]byte("1"))
    16  	b2 := blocks.NewBlock([]byte("2"))
    17  
    18  	n := New()
    19  	defer n.Shutdown()
    20  	ch := n.Subscribe(context.Background(), b1.Key(), b2.Key())
    21  
    22  	n.Publish(b1)
    23  	blockRecvd, ok := <-ch
    24  	if !ok {
    25  		t.Fail()
    26  	}
    27  	assertBlocksEqual(t, b1, blockRecvd)
    28  
    29  	n.Publish(b1) // ignored duplicate
    30  
    31  	n.Publish(b2)
    32  	blockRecvd, ok = <-ch
    33  	if !ok {
    34  		t.Fail()
    35  	}
    36  	assertBlocksEqual(t, b2, blockRecvd)
    37  }
    38  
    39  func TestPublishSubscribe(t *testing.T) {
    40  	blockSent := blocks.NewBlock([]byte("Greetings from The Interval"))
    41  
    42  	n := New()
    43  	defer n.Shutdown()
    44  	ch := n.Subscribe(context.Background(), blockSent.Key())
    45  
    46  	n.Publish(blockSent)
    47  	blockRecvd, ok := <-ch
    48  	if !ok {
    49  		t.Fail()
    50  	}
    51  
    52  	assertBlocksEqual(t, blockRecvd, blockSent)
    53  
    54  }
    55  
    56  func TestSubscribeMany(t *testing.T) {
    57  	e1 := blocks.NewBlock([]byte("1"))
    58  	e2 := blocks.NewBlock([]byte("2"))
    59  
    60  	n := New()
    61  	defer n.Shutdown()
    62  	ch := n.Subscribe(context.Background(), e1.Key(), e2.Key())
    63  
    64  	n.Publish(e1)
    65  	r1, ok := <-ch
    66  	if !ok {
    67  		t.Fatal("didn't receive first expected block")
    68  	}
    69  	assertBlocksEqual(t, e1, r1)
    70  
    71  	n.Publish(e2)
    72  	r2, ok := <-ch
    73  	if !ok {
    74  		t.Fatal("didn't receive second expected block")
    75  	}
    76  	assertBlocksEqual(t, e2, r2)
    77  }
    78  
    79  // TestDuplicateSubscribe tests a scenario where a given block
    80  // would be requested twice at the same time.
    81  func TestDuplicateSubscribe(t *testing.T) {
    82  	e1 := blocks.NewBlock([]byte("1"))
    83  
    84  	n := New()
    85  	defer n.Shutdown()
    86  	ch1 := n.Subscribe(context.Background(), e1.Key())
    87  	ch2 := n.Subscribe(context.Background(), e1.Key())
    88  
    89  	n.Publish(e1)
    90  	r1, ok := <-ch1
    91  	if !ok {
    92  		t.Fatal("didn't receive first expected block")
    93  	}
    94  	assertBlocksEqual(t, e1, r1)
    95  
    96  	r2, ok := <-ch2
    97  	if !ok {
    98  		t.Fatal("didn't receive second expected block")
    99  	}
   100  	assertBlocksEqual(t, e1, r2)
   101  }
   102  
   103  func TestSubscribeIsANoopWhenCalledWithNoKeys(t *testing.T) {
   104  	n := New()
   105  	defer n.Shutdown()
   106  	ch := n.Subscribe(context.Background()) // no keys provided
   107  	if _, ok := <-ch; ok {
   108  		t.Fatal("should be closed if no keys provided")
   109  	}
   110  }
   111  
   112  func TestCarryOnWhenDeadlineExpires(t *testing.T) {
   113  
   114  	impossibleDeadline := time.Nanosecond
   115  	fastExpiringCtx, cancel := context.WithTimeout(context.Background(), impossibleDeadline)
   116  	defer cancel()
   117  
   118  	n := New()
   119  	defer n.Shutdown()
   120  	block := blocks.NewBlock([]byte("A Missed Connection"))
   121  	blockChannel := n.Subscribe(fastExpiringCtx, block.Key())
   122  
   123  	assertBlockChannelNil(t, blockChannel)
   124  }
   125  
   126  func TestDoesNotDeadLockIfContextCancelledBeforePublish(t *testing.T) {
   127  
   128  	g := blocksutil.NewBlockGenerator()
   129  	ctx, cancel := context.WithCancel(context.Background())
   130  	n := New()
   131  	defer n.Shutdown()
   132  
   133  	t.Log("generate a large number of blocks. exceed default buffer")
   134  	bs := g.Blocks(1000)
   135  	ks := func() []key.Key {
   136  		var keys []key.Key
   137  		for _, b := range bs {
   138  			keys = append(keys, b.Key())
   139  		}
   140  		return keys
   141  	}()
   142  
   143  	_ = n.Subscribe(ctx, ks...) // ignore received channel
   144  
   145  	t.Log("cancel context before any blocks published")
   146  	cancel()
   147  	for _, b := range bs {
   148  		n.Publish(b)
   149  	}
   150  
   151  	t.Log("publishing the large number of blocks to the ignored channel must not deadlock")
   152  }
   153  
   154  func assertBlockChannelNil(t *testing.T, blockChannel <-chan *blocks.Block) {
   155  	_, ok := <-blockChannel
   156  	if ok {
   157  		t.Fail()
   158  	}
   159  }
   160  
   161  func assertBlocksEqual(t *testing.T, a, b *blocks.Block) {
   162  	if !bytes.Equal(a.Data, b.Data) {
   163  		t.Fatal("blocks aren't equal")
   164  	}
   165  	if a.Key() != b.Key() {
   166  		t.Fatal("block keys aren't equal")
   167  	}
   168  }