-
Notifications
You must be signed in to change notification settings - Fork 3
/
errs_test.go
266 lines (238 loc) · 6.28 KB
/
errs_test.go
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2021 Airbus Defence and Space
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package errs_test
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"net/url"
"os"
"syscall"
"testing"
"time"
"cloud.google.com/go/compute/metadata"
"github.com/airbusgeo/errs"
"github.com/pkg/errors"
oldcontext "golang.org/x/net/context"
"google.golang.org/api/googleapi"
)
func checkCause(t *testing.T, err error) {
errorcause := errors.Cause(err)
if errorcause.Error() != "initial error" {
t.Errorf("wrapped error cause returns [%s] instead of [initial error]", errorcause.Error())
}
}
func TestTempErr(t *testing.T) {
err := fmt.Errorf("initial error")
if errs.Temporary(err) {
t.Error("expected plain error to be non temporary")
}
checkCause(t, err)
pkgwrappederr := errors.Wrap(err, "pkg wrap 1")
if errs.Temporary(pkgwrappederr) {
t.Error("expected pkg wrapped plain error to be non temporary")
}
checkCause(t, pkgwrappederr)
temperror := errs.MakeTemporary(err)
if !errs.Temporary(temperror) {
t.Error("expected temperr.Wrap'd error to be temporary")
}
checkCause(t, temperror)
pkgwrappedtemperror := errors.Wrap(temperror, "pkg wrap 2")
if !errs.Temporary(pkgwrappedtemperror) {
t.Error("expected pkg wrapped temporary error to be temporary")
}
checkCause(t, pkgwrappedtemperror)
permerror := errs.MakePermanent(err)
if errs.Temporary(permerror) {
t.Error("expected permanent error to be permanent, duh")
}
checkCause(t, permerror)
permtemperror := errs.MakePermanent(temperror)
if errs.Temporary(permtemperror) {
t.Error("expected explicitely marked permanent (previously temp) error to be permanent")
}
checkCause(t, permtemperror)
permwrappedtemperror := errs.MakePermanent(pkgwrappedtemperror)
if errs.Temporary(permwrappedtemperror) {
t.Error("expected explicitely marked permanent wrapped (previously temp) error to be permanent")
}
checkCause(t, permwrappedtemperror)
wrappedpermtemperror := errors.Wrap(permtemperror, "pkg wrap 3")
if errs.Temporary(wrappedpermtemperror) {
t.Error("expected wrapped permtemp error to be permanent")
}
checkCause(t, wrappedpermtemperror)
if errs.Temporary(nil) {
t.Error("nil error is not temporary")
}
}
type temperr struct {
error
t bool
}
func (t temperr) Temporary() bool {
return t.t
}
func wrap(err error) error {
return fmt.Errorf("wrapped %w", err)
}
func cancelTest(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}
func oldCancelTest(ctx oldcontext.Context) error {
<-ctx.Done()
return ctx.Err()
}
func TestTemporary(t *testing.T) {
err := temperr{errors.New("temp"), true}
werr := wrap(err)
if !errs.Temporary(err) || !errs.Temporary(werr) {
t.Error("temp")
}
err.t = false
werr = wrap(err)
if errs.Temporary(err) || errs.Temporary(werr) {
t.Error("temp")
}
ctx, _ := context.WithTimeout(context.Background(), 50*time.Millisecond)
cerr := cancelTest(ctx)
werr = wrap(err)
if !errs.Temporary(cerr) || errs.Temporary(werr) {
t.Error("temp")
}
ctx, cncl := context.WithCancel(context.Background())
go func() {
time.Sleep(50 * time.Millisecond)
cncl()
}()
cerr = cancelTest(ctx)
werr = wrap(err)
if !errs.Temporary(cerr) || errs.Temporary(werr) {
t.Error("temp")
}
}
func TestOldContext(t *testing.T) {
ctx, _ := oldcontext.WithTimeout(oldcontext.Background(), 50*time.Millisecond)
cerr := oldCancelTest(ctx)
werr := wrap(cerr)
if !errs.Temporary(cerr) || !errs.Temporary(werr) {
t.Error("temp")
}
ctx, cncl := oldcontext.WithCancel(oldcontext.Background())
go func() {
time.Sleep(50 * time.Millisecond)
cncl()
}()
cerr = oldCancelTest(ctx)
werr = wrap(cerr)
if !errs.Temporary(cerr) || !errs.Temporary(werr) {
t.Error("temp")
}
}
func TestNil(t *testing.T) {
if errs.Temporary(nil) {
t.Error("nil is temp")
}
}
func TestErrno(t *testing.T) {
err := syscall.EIO
if !errs.Temporary(err) {
t.Error("eio not temp")
}
if !errs.Temporary(wrap(err)) {
t.Error("wrapped eio not temp")
}
err = syscall.EAGAIN
if !errs.Temporary(err) {
t.Error("eagain not temp")
}
if !errs.Temporary(wrap(err)) {
t.Error("wrapped eagain not temp")
}
}
func TestGoogleApiError(t *testing.T) {
gerr := &googleapi.Error{Code: 400}
if errs.Temporary(gerr) {
t.Error("400 is temp")
}
gerr = &googleapi.Error{Code: 429}
if !errs.Temporary(gerr) {
t.Error("429 is not temp")
}
gerr = &googleapi.Error{Code: 500}
if !errs.Temporary(gerr) {
t.Error("500 is not temp")
}
}
func TestDialTimeout(t *testing.T) {
t.Skip() //this test only works if port 22222 is firewalled/dropped
l, _ := net.ListenTCP("tcp4", &net.TCPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 22222,
})
defer l.Close()
d := net.Dialer{Timeout: 50 * time.Millisecond}
_, err := d.Dial("tcp", "127.0.0.1:22222")
if err != nil {
t.Error(err)
}
if !errs.Temporary(err) {
t.Error("not temporary")
}
}
func TestAddTemporaryCheck(t *testing.T) {
err := fmt.Errorf("not temp")
err = errs.AddTemporaryCheck(err)
type tif interface {
Temporary() bool
}
terr, ok := err.(tif)
if !ok {
t.Fatal("AddTemporaryCheck failed")
}
if terr.Temporary() {
t.Error("expected not temporary")
}
err = io.EOF
err = errs.AddTemporaryCheck(err)
if err != io.EOF {
t.Error("EOF was wrapped")
}
eerr := temperr{error: fmt.Errorf("temp"), t: true}
err = errs.AddTemporaryCheck(eerr)
if err != eerr {
t.Error("error already exposing Temporary() was wrapped")
}
}
type mockMetaServer struct{}
func (m mockMetaServer) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(500)
}
func TestGCEMetadataError(t *testing.T) {
ms := mockMetaServer{}
srv := httptest.NewServer(ms)
defer srv.Close()
surl, _ := url.Parse(srv.URL)
os.Setenv("GCE_METADATA_HOST", surl.Host)
mc := metadata.NewClient(nil)
_, err := mc.ProjectID()
if !errs.Temporary(err) {
t.Error("meta not raised")
}
}