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

import (
4 5
	"testing"

6
	"github.com/ipfs/go-ipfs/blocks"
George Antoniadis's avatar
George Antoniadis committed
7
	"gx/ipfs/Qmce4Y4zg3sYr7xKM5UueS67vhNni6EeWgCRnb7MbLJMew/go-key"
8 9

	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
George Antoniadis's avatar
George Antoniadis committed
10 11
	ds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore"
	syncds "gx/ipfs/QmbzuUusHqaLLoNTDEVLcSF6vZDHZDLPC7p4bztRvvkXxU/go-datastore/sync"
12 13
)

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

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

31
func createStores(t *testing.T) (*arccache, *blockstore, *callbackDatastore) {
32 33
	cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()}
	bs := NewBlockstore(syncds.MutexWrap(cd))
34
	arc, err := testArcCached(bs, nil)
35 36 37
	if err != nil {
		t.Fatal(err)
	}
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	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)
54 55 56 57 58 59 60 61 62

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

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

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

func TestElideDuplicateWrite(t *testing.T) {
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	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)

	arc.Has(exampleBlock.Key())
	trap("has hit datastore", cd, t)
	if has, err := arc.Has(exampleBlock.Key()); has || err != nil {
		t.Fatal("has was true but there is no such block")
	}

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

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

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	if has, err := arc.Has(exampleBlock.Key()); !has || err != nil {
		t.Fatal("has returned invalid result")
	}
}

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

	if bl, err := arc.Get(exampleBlock.Key()); bl != nil || err == nil {
		t.Fatal("block was found or there was no error")
	}

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

	if has, err := arc.Has(exampleBlock.Key()); has || err != nil {
		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)

	if has, err := arc.Has(exampleBlock.Key()); !has || err != nil {
		t.Fatal("has returned invalid result")
	}
}

126
func TestGetAndDeleteFalseShortCircuit(t *testing.T) {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	arc, _, cd := createStores(t)

	arc.Has(exampleBlock.Key())

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

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

	if arc.DeleteBlock(exampleBlock.Key()) != ErrNotFound {
		t.Fatal("expected ErrNotFound error")
	}
}

func TestArcCreationFailure(t *testing.T) {
143
	if arc, err := newARCCachedBS(nil, context.TODO(), -1); arc != nil || err == nil {
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
		t.Fatal("expected error and no cache")
	}
}

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

	bl, err := arc.Get(key.Key(""))

	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)

	arc.Get(exampleBlock.Key())

	trap("has hit datastore", cd, t)
	arc.Has(exampleBlock.Key())
}

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

	trap("has hit datastore", cd, t)
	arc.Has(exampleBlock.Key())
178 179 180 181 182 183
	untrap(cd)
	arc.DeleteBlock(exampleBlock.Key())

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