bloom_cache_test.go 3.12 KB
Newer Older
1 2 3
package blockstore

import (
4
	"fmt"
5 6 7 8
	"sync"
	"testing"
	"time"

9
	"github.com/ipfs/go-ipfs/blocks"
10

11 12 13
	ds "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore"
	dsq "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore/query"
	syncds "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore/sync"
14
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
15 16
)

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

31 32
func TestReturnsErrorWhenSizeNegative(t *testing.T) {
	bs := NewBlockstore(syncds.MutexWrap(ds.NewMapDatastore()))
33
	_, err := bloomCached(bs, context.TODO(), -1, 1)
34 35 36
	if err == nil {
		t.Fail()
	}
37
}
38 39 40 41 42 43 44
func TestHasIsBloomCached(t *testing.T) {
	cd := &callbackDatastore{f: func() {}, ds: ds.NewMapDatastore()}
	bs := NewBlockstore(syncds.MutexWrap(cd))

	for i := 0; i < 1000; i++ {
		bs.Put(blocks.NewBlock([]byte(fmt.Sprintf("data: %d", i))))
	}
45 46
	ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
	cachedbs, err := testBloomCached(bs, ctx)
47 48 49 50 51 52
	if err != nil {
		t.Fatal(err)
	}

	select {
	case <-cachedbs.rebuildChan:
53
	case <-ctx.Done():
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
		t.Fatalf("Timeout wating for rebuild: %d", cachedbs.bloom.ElementsAdded())
	}

	cacheFails := 0
	cd.SetFunc(func() {
		cacheFails++
	})

	for i := 0; i < 1000; i++ {
		cachedbs.Has(blocks.NewBlock([]byte(fmt.Sprintf("data: %d", i+2000))).Key())
	}

	if float64(cacheFails)/float64(1000) > float64(0.05) {
		t.Fatal("Bloom filter has cache miss rate of more than 5%")
	}
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

	cacheFails = 0
	block := blocks.NewBlock([]byte("newBlock"))

	cachedbs.PutMany([]blocks.Block{block})
	if cacheFails != 2 {
		t.Fatalf("expected two datastore hits: %d", cacheFails)
	}
	cachedbs.Put(block)
	if cacheFails != 3 {
		t.Fatalf("expected datastore hit: %d", cacheFails)
	}

	if has, err := cachedbs.Has(block.Key()); !has || err != nil {
		t.Fatal("has gave wrong response")
	}

	bl, err := cachedbs.Get(block.Key())
	if bl.String() != block.String() {
		t.Fatal("block data doesn't match")
	}

	if err != nil {
		t.Fatal("there should't be an error")
	}
94
}
95 96

type callbackDatastore struct {
97
	sync.Mutex
98 99 100 101
	f  func()
	ds ds.Datastore
}

102 103 104 105 106 107 108 109 110 111 112
func (c *callbackDatastore) SetFunc(f func()) {
	c.Lock()
	defer c.Unlock()
	c.f = f
}

func (c *callbackDatastore) CallF() {
	c.Lock()
	defer c.Unlock()
	c.f()
}
113 114

func (c *callbackDatastore) Put(key ds.Key, value interface{}) (err error) {
115
	c.CallF()
116 117 118 119
	return c.ds.Put(key, value)
}

func (c *callbackDatastore) Get(key ds.Key) (value interface{}, err error) {
120
	c.CallF()
121 122 123 124
	return c.ds.Get(key)
}

func (c *callbackDatastore) Has(key ds.Key) (exists bool, err error) {
125
	c.CallF()
126 127 128 129
	return c.ds.Has(key)
}

func (c *callbackDatastore) Delete(key ds.Key) (err error) {
130
	c.CallF()
131 132 133
	return c.ds.Delete(key)
}

134
func (c *callbackDatastore) Query(q dsq.Query) (dsq.Results, error) {
135
	c.CallF()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
136
	return c.ds.Query(q)
137
}
138 139 140 141

func (c *callbackDatastore) Batch() (ds.Batch, error) {
	return ds.NewBasicBatch(c), nil
}