autobatch_test.go 959 Bytes
Newer Older
1 2 3
package autobatch

import (
4
	"bytes"
5 6 7 8
	"fmt"
	"testing"

	ds "github.com/ipfs/go-datastore"
9
	dstest "github.com/ipfs/go-datastore/test"
10 11
)

12 13
func TestAutobatch(t *testing.T) {
	dstest.SubtestAll(t, NewAutoBatching(ds.NewMapDatastore(), 16))
14 15 16 17 18 19 20 21 22 23
}

func TestFlushing(t *testing.T) {
	child := ds.NewMapDatastore()
	d := NewAutoBatching(child, 16)

	var keys []ds.Key
	for i := 0; i < 16; i++ {
		keys = append(keys, ds.NewKey(fmt.Sprintf("test%d", i)))
	}
24
	v := []byte("hello world")
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

	for _, k := range keys {
		err := d.Put(k, v)
		if err != nil {
			t.Fatal(err)
		}
	}

	_, err := child.Get(keys[0])
	if err != ds.ErrNotFound {
		t.Fatal("shouldnt have found value")
	}

	err = d.Put(ds.NewKey("test16"), v)
	if err != nil {
		t.Fatal(err)
	}

	// should be flushed now, try to get keys from child datastore
	for _, k := range keys {
		val, err := child.Get(k)
		if err != nil {
			t.Fatal(err)
		}

50 51
		if !bytes.Equal(val, v) {
			t.Fatal("wrong value")
52 53 54
		}
	}
}