autobatch_test.go 1.12 KB
Newer Older
1 2 3
package autobatch

import (
4
	"bytes"
5 6 7 8 9 10 11 12 13 14
	"fmt"
	"testing"

	ds "github.com/ipfs/go-datastore"
)

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

	k := ds.NewKey("test")
15
	v := []byte("hello world")
16 17 18 19 20 21 22 23 24 25 26

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

	out, err := d.Get(k)
	if err != nil {
		t.Fatal(err)
	}

27
	if !bytes.Equal(out, v) {
28 29 30 31 32 33 34 35 36 37 38 39
		t.Fatal("wasnt the same! ITS NOT THE SAME")
	}
}

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)))
	}
40
	v := []byte("hello world")
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

	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)
		}

66 67
		if !bytes.Equal(val, v) {
			t.Fatal("wrong value")
68 69 70
		}
	}
}