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 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
package lmdbsync
import (
"fmt"
"testing"
"time"
"golang.org/x/net/context"
"github.com/bmatsuo/lmdb-go/internal/lmdbtest"
"github.com/bmatsuo/lmdb-go/lmdb"
)
type testHandler struct {
called bool
ctx context.Context
env *Env
err error
}
func (h *testHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) {
h.called = true
h.ctx = ctx
h.env = env
h.err = err
return ctx, err
}
func TestHandlerChain(t *testing.T) {
ctx := context.Background()
env, err := newEnv(nil)
if err != nil {
t.Fatal(err)
}
defer lmdbtest.Destroy(env.Env)
var chain1 HandlerChain
errother := fmt.Errorf("testerr")
ctx1, err := chain1.HandleTxnErr(ctx, env, errother)
if err != errother {
t.Error(err)
}
if ctx1 != ctx {
t.Errorf("unexpected ctx: %#v (!= %#v)", ctx1, ctx)
}
chain2 := chain1.Append(&passthroughHandler{})
ctx2, err := chain2.HandleTxnErr(ctx, env, errother)
if err != errother {
t.Error(err)
}
if ctx2 != ctx {
t.Errorf("unexpected ctx: %#v (!= %#v)", ctx2, ctx)
}
}
type retryHandler struct{}
func (*retryHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) {
return ctx, ErrTxnRetry
}
type passthroughHandler struct{}
func (*passthroughHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) {
return ctx, err
}
func TestMapFullHandler(t *testing.T) {
ctx := context.Background()
env, err := newEnv(nil)
if err != nil {
t.Fatal(err)
}
defer lmdbtest.Destroy(env.Env)
info, err := env.Info()
if err != nil {
t.Error(err)
return
}
orig := info.MapSize
doubleSize := func(size int64) (int64, bool) { return size * 2, true }
handler := MapFullHandler(doubleSize)
errother := fmt.Errorf("testerr")
ctx1, err := handler.HandleTxnErr(ctx, env, errother)
if ctx1 != ctx {
t.Errorf("ctx changed: %q (!= %q)", ctx1, ctx)
}
errmapfull := &lmdb.OpError{
Op: "lmdbsync_test_op",
Errno: lmdb.MapFull,
}
ctx1, err = handler.HandleTxnErr(ctx, env, errmapfull)
if err != ErrTxnRetry {
t.Errorf("unexpected error: %v", err)
}
if ctx1 != ctx {
t.Errorf("ctx changed: %q (!= %q)", ctx1, ctx)
}
info, err = env.Info()
if err != nil {
t.Error(err)
return
}
if info.MapSize <= orig {
t.Errorf("unexpected map size: %d (<= %d)", info.MapSize, orig)
}
}
func TestMapResizedHandler(t *testing.T) {
ctx := context.Background()
env, err := newEnv(nil)
if err != nil {
t.Fatal(err)
}
defer lmdbtest.Destroy(env.Env)
handler := MapResizedHandler(2, func(int) time.Duration { return 100 * time.Microsecond })
errother := fmt.Errorf("testerr")
_, err = handler.HandleTxnErr(ctx, env, errother)
errmapresized := &lmdb.OpError{
Op: "lmdbsync_test_op",
Errno: lmdb.MapResized,
}
ctx1, err := handler.HandleTxnErr(ctx, env, errmapresized)
if err != ErrTxnRetry {
t.Errorf("unexpected error: %v", err)
}
ctx2, err := handler.HandleTxnErr(ctx1, env, errmapresized)
if err != ErrTxnRetry {
t.Errorf("unexpected error: %v", err)
}
// after MapResized has been encountered enough times consecutively the
// handler starts passing MapResized through to the caller.
_, err = handler.HandleTxnErr(ctx2, env, errmapresized)
if !lmdb.IsMapResized(err) {
t.Errorf("unexpected error: %v", err)
}
ctx3, err := handler.HandleTxnErr(ctx2, env, errmapresized)
if !lmdb.IsMapResized(err) {
t.Errorf("unexpected error: %v", err)
}
ctx4, err := handler.HandleTxnErr(ctx3, env, errother)
if err != errother {
t.Errorf("unexpected error: %v", err)
}
// after encountering an error other than MapResized the handler resets its
// failure count and will continue attempting to adopt the new map size
// when MapResized is encountered.
ctx5, err := handler.HandleTxnErr(ctx4, env, errmapresized)
if err != ErrTxnRetry {
t.Errorf("unexpected error: %v", err)
}
_, err = handler.HandleTxnErr(ctx5, env, errmapresized)
if err != ErrTxnRetry {
t.Errorf("unexpected error: %v", err)
}
}
func TestExponentialBackoff(t *testing.T) {
base := time.Millisecond
max := 3 * time.Millisecond
factor := 2.0
backoff := ExponentialBackoff(base, max, factor)
const numtest = 100
for i := 0; i < numtest; i++ {
n := backoff(0)
if n < 0 || n > base {
t.Errorf("unexpected backoff: %v", n)
}
}
for i := 0; i < numtest; i++ {
n := backoff(1)
if n < 0 || n > 2*base {
t.Errorf("unexpected backoff: %v", n)
}
}
for i := 0; i < numtest; i++ {
n := backoff(2)
if n < 0 || n > max {
t.Errorf("unexpected backoff: %v", n)
}
}
}
|