Debian Codegraph @

exp/lmdbsync/handler_test.go

raw symbols SCIP
1 package lmdbsync
2
3 import (
4 "fmt"
5 "testing"
6 "time"
7
8 "golang.org/x/net/context"
9
10 "github.com/bmatsuo/lmdb-go/internal/lmdbtest"
11 "github.com/bmatsuo/lmdb-go/lmdb"
12 )
13
14 type testHandler struct {
15 called bool
16 ctx context.Context
17 env *Env
18 err error
19 }
20
21 func (h *testHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) {
22 h.called = true
23 h.ctx = ctx
24 h.env = env
25 h.err = err
26 return ctx, err
27 }
28
29 func TestHandlerChain(t *testing.T) {
30 ctx := context.Background()
31 env, err := newEnv(nil)
32 if err != nil {
33 t.Fatal(err)
34 }
35 defer lmdbtest.Destroy(env.Env)
36
37 var chain1 HandlerChain
38 errother := fmt.Errorf("testerr")
39
40 ctx1, err := chain1.HandleTxnErr(ctx, env, errother)
41 if err != errother {
42 t.Error(err)
43 }
44 if ctx1 != ctx {
45 t.Errorf("unexpected ctx: %#v (!= %#v)", ctx1, ctx)
46 }
47
48 chain2 := chain1.Append(&passthroughHandler{})
49 ctx2, err := chain2.HandleTxnErr(ctx, env, errother)
50 if err != errother {
51 t.Error(err)
52 }
53 if ctx2 != ctx {
54 t.Errorf("unexpected ctx: %#v (!= %#v)", ctx2, ctx)
55 }
56 }
57
58 type retryHandler struct{}
59
60 func (*retryHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) {
61 return ctx, ErrTxnRetry
62
63 }
64
65 type passthroughHandler struct{}
66
67 func (*passthroughHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) {
68 return ctx, err
69
70 }
71
72 func TestMapFullHandler(t *testing.T) {
73 ctx := context.Background()
74 env, err := newEnv(nil)
75 if err != nil {
76 t.Fatal(err)
77 }
78 defer lmdbtest.Destroy(env.Env)
79
80 info, err := env.Info()
81 if err != nil {
82 t.Error(err)
83 return
84 }
85 orig := info.MapSize
86
87 doubleSize := func(size int64) (int64, bool) { return size * 2, true }
88 handler := MapFullHandler(doubleSize)
89
90 errother := fmt.Errorf("testerr")
91 ctx1, err := handler.HandleTxnErr(ctx, env, errother)
92 if ctx1 != ctx {
93 t.Errorf("ctx changed: %q (!= %q)", ctx1, ctx)
94 }
95
96 errmapfull := &lmdb.OpError{
97 Op: "lmdbsync_test_op",
98 Errno: lmdb.MapFull,
99 }
100 ctx1, err = handler.HandleTxnErr(ctx, env, errmapfull)
101 if err != ErrTxnRetry {
102 t.Errorf("unexpected error: %v", err)
103 }
104 if ctx1 != ctx {
105 t.Errorf("ctx changed: %q (!= %q)", ctx1, ctx)
106 }
107
108 info, err = env.Info()
109 if err != nil {
110 t.Error(err)
111 return
112 }
113 if info.MapSize <= orig {
114 t.Errorf("unexpected map size: %d (<= %d)", info.MapSize, orig)
115 }
116 }
117
118 func TestMapResizedHandler(t *testing.T) {
119 ctx := context.Background()
120 env, err := newEnv(nil)
121 if err != nil {
122 t.Fatal(err)
123 }
124 defer lmdbtest.Destroy(env.Env)
125
126 handler := MapResizedHandler(2, func(int) time.Duration { return 100 * time.Microsecond })
127
128 errother := fmt.Errorf("testerr")
129 _, err = handler.HandleTxnErr(ctx, env, errother)
130
131 errmapresized := &lmdb.OpError{
132 Op: "lmdbsync_test_op",
133 Errno: lmdb.MapResized,
134 }
135 ctx1, err := handler.HandleTxnErr(ctx, env, errmapresized)
136 if err != ErrTxnRetry {
137 t.Errorf("unexpected error: %v", err)
138 }
139 ctx2, err := handler.HandleTxnErr(ctx1, env, errmapresized)
140 if err != ErrTxnRetry {
141 t.Errorf("unexpected error: %v", err)
142 }
143
144 // after MapResized has been encountered enough times consecutively the
145 // handler starts passing MapResized through to the caller.
146 _, err = handler.HandleTxnErr(ctx2, env, errmapresized)
147 if !lmdb.IsMapResized(err) {
148 t.Errorf("unexpected error: %v", err)
149 }
150 ctx3, err := handler.HandleTxnErr(ctx2, env, errmapresized)
151 if !lmdb.IsMapResized(err) {
152 t.Errorf("unexpected error: %v", err)
153 }
154
155 ctx4, err := handler.HandleTxnErr(ctx3, env, errother)
156 if err != errother {
157 t.Errorf("unexpected error: %v", err)
158 }
159
160 // after encountering an error other than MapResized the handler resets its
161 // failure count and will continue attempting to adopt the new map size
162 // when MapResized is encountered.
163 ctx5, err := handler.HandleTxnErr(ctx4, env, errmapresized)
164 if err != ErrTxnRetry {
165 t.Errorf("unexpected error: %v", err)
166 }
167 _, err = handler.HandleTxnErr(ctx5, env, errmapresized)
168 if err != ErrTxnRetry {
169 t.Errorf("unexpected error: %v", err)
170 }
171 }
172
173 func TestExponentialBackoff(t *testing.T) {
174 base := time.Millisecond
175 max := 3 * time.Millisecond
176 factor := 2.0
177 backoff := ExponentialBackoff(base, max, factor)
178
179 const numtest = 100
180 for i := 0; i < numtest; i++ {
181 n := backoff(0)
182 if n < 0 || n > base {
183 t.Errorf("unexpected backoff: %v", n)
184 }
185 }
186 for i := 0; i < numtest; i++ {
187 n := backoff(1)
188 if n < 0 || n > 2*base {
189 t.Errorf("unexpected backoff: %v", n)
190 }
191 }
192 for i := 0; i < numtest; i++ {
193 n := backoff(2)
194 if n < 0 || n > max {
195 t.Errorf("unexpected backoff: %v", n)
196 }
197 }
198 }