arc_cache_test.go 4.19 KB
Newer Older
1 2 3
package blockstore

import (
4
	"context"
5 6
	"testing"

7 8
	"github.com/ipfs/go-ipfs/blocks"

Jeromy's avatar
Jeromy committed
9 10
	ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
	syncds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore/sync"
11
	cid "gx/ipfs/QmYhQaCYEcaPPjxJX7YcPcVKkQfRy6sJ7B3XmGFk82XYdQ/go-cid"
12 13
)

14 15
var exampleBlock = blocks.NewBlock([]byte("foo"))

16
func testArcCached(ctx context.Context, bs Blockstore) (*arccache, error) {
17 18 19 20 21 22
	if ctx == nil {
		ctx = context.TODO()
	}
	opts := DefaultCacheOpts()
	opts.HasBloomFilterSize = 0
	opts.HasBloomFilterHashes = 0
23
	bbs, err := CachedBlockstore(ctx, bs, opts)
24 25 26
	if err == nil {
		return bbs.(*arccache), nil
	}
27
	return nil, err
28 29
}

30
func createStores(t *testing.T) (*arccache, Blockstore, *callbackDatastore) {
31 32
	cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()}
	bs := NewBlockstore(syncds.MutexWrap(cd))
33
	arc, err := testArcCached(nil, bs)
34 35 36
	if err != nil {
		t.Fatal(err)
	}
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	return arc, bs, cd
}

func trap(message string, cd *callbackDatastore, t *testing.T) {
	cd.SetFunc(func() {
		t.Fatal(message)
	})
}
func untrap(cd *callbackDatastore) {
	cd.SetFunc(func() {})
}

func TestRemoveCacheEntryOnDelete(t *testing.T) {
	arc, _, cd := createStores(t)

	arc.Put(exampleBlock)
53 54 55 56 57 58 59 60 61

	cd.Lock()
	writeHitTheDatastore := false
	cd.Unlock()

	cd.SetFunc(func() {
		writeHitTheDatastore = true
	})

62
	arc.DeleteBlock(exampleBlock.Cid())
63
	arc.Put(exampleBlock)
64 65 66 67 68 69
	if !writeHitTheDatastore {
		t.Fail()
	}
}

func TestElideDuplicateWrite(t *testing.T) {
70 71 72 73 74 75 76 77 78 79
	arc, _, cd := createStores(t)

	arc.Put(exampleBlock)
	trap("write hit datastore", cd, t)
	arc.Put(exampleBlock)
}

func TestHasRequestTriggersCache(t *testing.T) {
	arc, _, cd := createStores(t)

80
	arc.Has(exampleBlock.Cid())
81
	trap("has hit datastore", cd, t)
82
	if has, err := arc.Has(exampleBlock.Cid()); has || err != nil {
83 84 85 86 87
		t.Fatal("has was true but there is no such block")
	}

	untrap(cd)
	err := arc.Put(exampleBlock)
88 89 90 91
	if err != nil {
		t.Fatal(err)
	}

92
	trap("has hit datastore", cd, t)
93

94
	if has, err := arc.Has(exampleBlock.Cid()); !has || err != nil {
95 96 97 98 99 100 101
		t.Fatal("has returned invalid result")
	}
}

func TestGetFillsCache(t *testing.T) {
	arc, _, cd := createStores(t)

102
	if bl, err := arc.Get(exampleBlock.Cid()); bl != nil || err == nil {
103 104 105 106 107
		t.Fatal("block was found or there was no error")
	}

	trap("has hit datastore", cd, t)

108
	if has, err := arc.Has(exampleBlock.Cid()); has || err != nil {
109 110 111 112 113 114 115 116 117 118 119
		t.Fatal("has was true but there is no such block")
	}

	untrap(cd)

	if err := arc.Put(exampleBlock); err != nil {
		t.Fatal(err)
	}

	trap("has hit datastore", cd, t)

120
	if has, err := arc.Has(exampleBlock.Cid()); !has || err != nil {
121 122 123 124
		t.Fatal("has returned invalid result")
	}
}

125
func TestGetAndDeleteFalseShortCircuit(t *testing.T) {
126 127
	arc, _, cd := createStores(t)

128
	arc.Has(exampleBlock.Cid())
129 130 131

	trap("get hit datastore", cd, t)

132
	if bl, err := arc.Get(exampleBlock.Cid()); bl != nil || err != ErrNotFound {
133 134 135
		t.Fatal("get returned invalid result")
	}

136
	if arc.DeleteBlock(exampleBlock.Cid()) != ErrNotFound {
137 138 139 140 141
		t.Fatal("expected ErrNotFound error")
	}
}

func TestArcCreationFailure(t *testing.T) {
142
	if arc, err := newARCCachedBS(context.TODO(), nil, -1); arc != nil || err == nil {
143 144 145 146 147 148 149
		t.Fatal("expected error and no cache")
	}
}

func TestInvalidKey(t *testing.T) {
	arc, _, _ := createStores(t)

150
	bl, err := arc.Get(nil)
151 152 153 154 155 156 157 158 159 160 161 162 163 164

	if bl != nil {
		t.Fatal("blocks should be nil")
	}
	if err == nil {
		t.Fatal("expected error")
	}
}

func TestHasAfterSucessfulGetIsCached(t *testing.T) {
	arc, bs, cd := createStores(t)

	bs.Put(exampleBlock)

165
	arc.Get(exampleBlock.Cid())
166 167

	trap("has hit datastore", cd, t)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	arc.Has(exampleBlock.Cid())
}

func TestDifferentKeyObjectsWork(t *testing.T) {
	arc, bs, cd := createStores(t)

	bs.Put(exampleBlock)

	arc.Get(exampleBlock.Cid())

	trap("has hit datastore", cd, t)
	cidstr := exampleBlock.Cid().String()

	ncid, err := cid.Decode(cidstr)
	if err != nil {
		t.Fatal(err)
	}

	arc.Has(ncid)
187 188 189 190 191 192 193
}

func TestPutManyCaches(t *testing.T) {
	arc, _, cd := createStores(t)
	arc.PutMany([]blocks.Block{exampleBlock})

	trap("has hit datastore", cd, t)
194
	arc.Has(exampleBlock.Cid())
195
	untrap(cd)
196
	arc.DeleteBlock(exampleBlock.Cid())
197 198 199 200

	arc.Put(exampleBlock)
	trap("PunMany has hit datastore", cd, t)
	arc.PutMany([]blocks.Block{exampleBlock})
201
}