retrystore_test.go 2.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
package retrystore

import (
	"fmt"
	"strings"
	"testing"

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

func TestRetryFailure(t *testing.T) {
	myErr := fmt.Errorf("this is an actual error")
	var count int
	fstore := failstore.NewFailstore(ds.NewMapDatastore(), func(op string) error {
		count++
		return myErr
	})

	rds := &Datastore{
		Batching: fstore,
		Retries:  5,
		TempErrFunc: func(err error) bool {
			return err == myErr
		},
	}

	k := ds.NewKey("test")

	_, err := rds.Get(k)
	if err == nil {
		t.Fatal("expected this to fail")
	}

	if !strings.Contains(err.Error(), "ran out of retries") {
		t.Fatal("got different error than expected: ", err)
	}

	if count != 6 {
		t.Fatal("expected five retries (six executions), got: ", count)
	}
}

func TestRealErrorGetsThrough(t *testing.T) {
	myErr := fmt.Errorf("this is an actual error")
	fstore := failstore.NewFailstore(ds.NewMapDatastore(), func(op string) error {
		return myErr
	})

	rds := &Datastore{
		Batching: fstore,
		Retries:  5,
		TempErrFunc: func(err error) bool {
			return false
		},
	}

	k := ds.NewKey("test")
	_, err := rds.Get(k)
	if err != myErr {
		t.Fatal("expected my own error")
	}

	_, err = rds.Has(k)
	if err != myErr {
		t.Fatal("expected my own error")
	}

	err = rds.Put(k, nil)
	if err != myErr {
		t.Fatal("expected my own error")
	}
}

func TestRealErrorAfterTemp(t *testing.T) {
	myErr := fmt.Errorf("this is an actual error")
	tempErr := fmt.Errorf("this is a temp error")
	var count int
	fstore := failstore.NewFailstore(ds.NewMapDatastore(), func(op string) error {
		count++
		if count < 3 {
			return tempErr
		}

		return myErr
	})

	rds := &Datastore{
		Batching: fstore,
		Retries:  5,
		TempErrFunc: func(err error) bool {
			return err == tempErr
		},
	}

	k := ds.NewKey("test")
	_, err := rds.Get(k)
	if err != myErr {
		t.Fatal("expected my own error")
	}
}

func TestSuccessAfterTemp(t *testing.T) {
	tempErr := fmt.Errorf("this is a temp error")
	var count int
	fstore := failstore.NewFailstore(ds.NewMapDatastore(), func(op string) error {
		count++
		if count < 3 {
			return tempErr
		}
		count = 0
		return nil
	})

	rds := &Datastore{
		Batching: fstore,
		Retries:  5,
		TempErrFunc: func(err error) bool {
			return err == tempErr
		},
	}

	k := ds.NewKey("test")
	val := []byte("foo")

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

	has, err := rds.Has(k)
	if err != nil {
		t.Fatal(err)
	}

	if !has {
		t.Fatal("should have this thing")
	}

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

	if string(out.([]byte)) != string(val) {
		t.Fatal("got wrong value")
	}
}