queue_test.go 3 KB
Newer Older
1
package queue
2 3 4 5 6 7 8 9 10

import (
	"context"
	"testing"
	"time"

	cid "github.com/ipfs/go-cid"
	datastore "github.com/ipfs/go-datastore"
	sync "github.com/ipfs/go-datastore/sync"
11
	"github.com/ipfs/go-ipfs-blocksutil"
12 13
)

14 15
var blockGenerator = blocksutil.NewBlockGenerator()

16
func makeCids(n int) []cid.Cid {
Erik Ingenito's avatar
Erik Ingenito committed
17
	cids := make([]cid.Cid, 0, n)
Michael Avila's avatar
Michael Avila committed
18
	for i := 0; i < n; i++ {
19 20 21 22 23 24 25 26 27
		c := blockGenerator.Next().Cid()
		cids = append(cids, c)
	}
	return cids
}

func assertOrdered(cids []cid.Cid, q *Queue, t *testing.T) {
	for _, c := range cids {
		select {
Erik Ingenito's avatar
Gofmt  
Erik Ingenito committed
28
		case dequeued := <-q.dequeue:
Erik Ingenito's avatar
Erik Ingenito committed
29 30
			if c != dequeued {
				t.Fatalf("Error in ordering of CIDs retrieved from queue. Expected: %s, got: %s", c, dequeued)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
			}

		case <-time.After(time.Second * 1):
			t.Fatal("Timeout waiting for cids to be provided.")
		}
	}
}

func TestBasicOperation(t *testing.T) {
	ctx := context.Background()
	defer ctx.Done()

	ds := sync.MutexWrap(datastore.NewMapDatastore())
	queue, err := NewQueue(ctx, "test", ds)
	if err != nil {
		t.Fatal(err)
	}

	cids := makeCids(10)

	for _, c := range cids {
Erik Ingenito's avatar
Erik Ingenito committed
52
		queue.Enqueue(c)
53 54 55 56 57
	}

	assertOrdered(cids, queue, t)
}

Erik Ingenito's avatar
Erik Ingenito committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
func TestSparseDatastore(t *testing.T) {
	ctx := context.Background()
	defer ctx.Done()

	ds := sync.MutexWrap(datastore.NewMapDatastore())
	queue, err := NewQueue(ctx, "test", ds)
	if err != nil {
		t.Fatal(err)
	}

	cids := makeCids(10)
	for _, c := range cids {
		queue.Enqueue(c)
	}

	// remove entries in the middle
	err = queue.ds.Delete(queue.queueKey(5))
	if err != nil {
		t.Fatal(err)
	}

	err = queue.ds.Delete(queue.queueKey(6))
	if err != nil {
		t.Fatal(err)
	}

	expected := append(cids[:5], cids[7:]...)
	assertOrdered(expected, queue, t)
}

func TestMangledData(t *testing.T) {
89 90 91 92 93 94 95 96 97 98
	ctx := context.Background()
	defer ctx.Done()

	ds := sync.MutexWrap(datastore.NewMapDatastore())
	queue, err := NewQueue(ctx, "test", ds)
	if err != nil {
		t.Fatal(err)
	}

	cids := makeCids(10)
Erik Ingenito's avatar
Erik Ingenito committed
99 100 101 102 103 104
	for _, c := range cids {
		queue.Enqueue(c)
	}

	// remove entries in the middle
	err = queue.ds.Put(queue.queueKey(5), []byte("borked"))
105 106 107
	if err != nil {
		t.Fatal(err)
	}
108

Erik Ingenito's avatar
Erik Ingenito committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	expected := append(cids[:5], cids[6:]...)
	assertOrdered(expected, queue, t)
}

func TestInitialization(t *testing.T) {
	ctx := context.Background()
	defer ctx.Done()

	ds := sync.MutexWrap(datastore.NewMapDatastore())
	queue, err := NewQueue(ctx, "test", ds)
	if err != nil {
		t.Fatal(err)
	}

	cids := makeCids(10)
124
	for _, c := range cids {
Erik Ingenito's avatar
Erik Ingenito committed
125
		queue.Enqueue(c)
126 127 128 129 130 131 132 133 134 135 136 137
	}

	assertOrdered(cids[:5], queue, t)

	// make a new queue, same data
	queue, err = NewQueue(ctx, "test", ds)
	if err != nil {
		t.Fatal(err)
	}

	assertOrdered(cids[5:], queue, t)
}
Michael Avila's avatar
Michael Avila committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

func TestInitializationWithManyCids(t *testing.T) {
	ctx := context.Background()
	defer ctx.Done()

	ds := sync.MutexWrap(datastore.NewMapDatastore())
	queue, err := NewQueue(ctx, "test", ds)
	if err != nil {
		t.Fatal(err)
	}

	cids := makeCids(25)
	for _, c := range cids {
		queue.Enqueue(c)
	}

	// make a new queue, same data
	queue, err = NewQueue(ctx, "test", ds)
	if err != nil {
		t.Fatal(err)
	}

	assertOrdered(cids, queue, t)
}