@cryptotaxi247 / kubo / commits / e4c973164

chore(util) remove forward

License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Nov 21, 2014 at 17:53 UTC e4c97316430e208c81d24d1359cfc4c67671c859
2 files changed -92
util/async/forward.go deleted
-34
@@ -1,34 +0,0 @@
1 -package async
2 -
3 -import (
4 - context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5 - "github.com/jbenet/go-ipfs/blocks"
6 - u "github.com/jbenet/go-ipfs/util"
7 -)
8 -
9 -var log = u.Logger("async")
10 -
11 -// ForwardN forwards up to |num| blocks to the returned channel.
12 -func ForwardN(ctx context.Context, in <-chan *blocks.Block, num int) <-chan *blocks.Block {
13 - out := make(chan *blocks.Block)
14 - go func() {
15 - defer close(out)
16 - for i := 0; i < num; i++ {
17 - select {
18 - case block, ok := <-in:
19 - if !ok {
20 - log.Error("Forwarder exiting early!")
21 - return // otherwise nil value is forwarded to output
22 - }
23 - select {
24 - case out <- block:
25 - case <-ctx.Done():
26 - return
27 - }
28 - case <-ctx.Done():
29 - return
30 - }
31 - }
32 - }()
33 - return out
34 -}
util/async/forward_test.go deleted
-58
@@ -1,58 +0,0 @@
1 -package async
2 -
3 -import (
4 - "testing"
5 -
6 - context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 - "github.com/jbenet/go-ipfs/blocks"
8 -)
9 -
10 -func TestForwardNThenClose(t *testing.T) {
11 - const n = 2
12 - const buf = 2 * n
13 - in := make(chan *blocks.Block, buf)
14 - ctx := context.Background()
15 - out := ForwardN(ctx, in, n)
16 -
17 - for i := 0; i < buf; i++ {
18 - in <- blocks.NewBlock([]byte(""))
19 - }
20 -
21 - for i := 0; i < n; i++ {
22 - _ = <-out
23 - }
24 -
25 - _, ok := <-out // closed
26 - if !ok {
27 - return
28 - }
29 - t.Fatal("channel still open after receiving n blocks")
30 -}
31 -
32 -func TestCloseInput(t *testing.T) {
33 - const n = 2
34 - in := make(chan *blocks.Block, 0)
35 - ctx := context.Background()
36 - out := ForwardN(ctx, in, n)
37 -
38 - close(in)
39 - _, ok := <-out // closed
40 - if !ok {
41 - return
42 - }
43 - t.Fatal("input channel closed, but output channel not")
44 -
45 -}
46 -
47 -func TestContextClosedWhenBlockingOnInput(t *testing.T) {
48 - const n = 1 // but we won't ever send a block
49 - ctx, cancel := context.WithCancel(context.Background())
50 - out := ForwardN(ctx, make(chan *blocks.Block), n)
51 -
52 - cancel() // before sending anything
53 - _, ok := <-out
54 - if !ok {
55 - return
56 - }
57 - t.Fail()
58 -}