batch_test.go 2.15 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
package format

import (
	"context"
	"sync"
	"testing"

	cid "github.com/ipfs/go-cid"
)

// Test dag
type testDag struct {
	mu    sync.Mutex
	nodes map[string]Node
}

func newTestDag() *testDag {
	return &testDag{nodes: make(map[string]Node)}
}

func (d *testDag) Get(ctx context.Context, cid *cid.Cid) (Node, error) {
	d.mu.Lock()
	defer d.mu.Unlock()
	if n, ok := d.nodes[cid.KeyString()]; ok {
		return n, nil
	}
	return nil, ErrNotFound
}

func (d *testDag) GetMany(ctx context.Context, cids []*cid.Cid) <-chan *NodeOption {
	d.mu.Lock()
	defer d.mu.Unlock()
	out := make(chan *NodeOption, len(cids))
	for _, c := range cids {
		if n, ok := d.nodes[c.KeyString()]; ok {
			out <- &NodeOption{Node: n}
		} else {
			out <- &NodeOption{Err: ErrNotFound}
		}
	}
41
	close(out)
Steven Allen's avatar
Steven Allen committed
42 43 44
	return out
}

45
func (d *testDag) Add(ctx context.Context, node Node) error {
Steven Allen's avatar
Steven Allen committed
46 47
	d.mu.Lock()
	defer d.mu.Unlock()
Steven Allen's avatar
Steven Allen committed
48 49
	d.nodes[node.Cid().KeyString()] = node
	return nil
Steven Allen's avatar
Steven Allen committed
50 51
}

52
func (d *testDag) AddMany(ctx context.Context, nodes []Node) error {
Steven Allen's avatar
Steven Allen committed
53 54
	d.mu.Lock()
	defer d.mu.Unlock()
Steven Allen's avatar
Steven Allen committed
55 56
	for _, n := range nodes {
		d.nodes[n.Cid().KeyString()] = n
Steven Allen's avatar
Steven Allen committed
57
	}
Steven Allen's avatar
Steven Allen committed
58
	return nil
Steven Allen's avatar
Steven Allen committed
59 60
}

61
func (d *testDag) Remove(ctx context.Context, c *cid.Cid) error {
Steven Allen's avatar
Steven Allen committed
62 63
	d.mu.Lock()
	defer d.mu.Unlock()
Steven Allen's avatar
Steven Allen committed
64
	delete(d.nodes, c.KeyString())
Steven Allen's avatar
Steven Allen committed
65 66 67
	return nil
}

68
func (d *testDag) RemoveMany(ctx context.Context, cids []*cid.Cid) error {
Steven Allen's avatar
Steven Allen committed
69 70 71 72 73 74 75 76
	d.mu.Lock()
	defer d.mu.Unlock()
	for _, c := range cids {
		delete(d.nodes, c.KeyString())
	}
	return nil
}

Steven Allen's avatar
Steven Allen committed
77 78 79
var _ DAGService = new(testDag)

func TestBatch(t *testing.T) {
Steven Allen's avatar
Steven Allen committed
80 81 82
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

Steven Allen's avatar
Steven Allen committed
83
	d := newTestDag()
Steven Allen's avatar
Steven Allen committed
84
	b := NewBatch(ctx, d)
Steven Allen's avatar
Steven Allen committed
85 86 87 88
	for i := 0; i < 1000; i++ {
		// It would be great if we could use *many* different nodes here
		// but we can't add any dependencies and I don't feel like adding
		// any more testing code.
Steven Allen's avatar
Steven Allen committed
89
		if err := b.Add(new(EmptyNode)); err != nil {
Steven Allen's avatar
Steven Allen committed
90 91 92 93 94 95 96
			t.Fatal(err)
		}
	}
	if err := b.Commit(); err != nil {
		t.Fatal(err)
	}

Steven Allen's avatar
Steven Allen committed
97
	n, err := d.Get(ctx, new(EmptyNode).Cid())
Steven Allen's avatar
Steven Allen committed
98 99 100 101 102 103 104 105 106 107 108 109 110
	if err != nil {
		t.Fatal(err)
	}
	switch n.(type) {
	case *EmptyNode:
	default:
		t.Fatal("expected the node to exist in the dag")
	}

	if len(d.nodes) != 1 {
		t.Fatal("should have one node")
	}
}