OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package retry | |
6 | |
7 import ( | |
8 "errors" | |
9 "testing" | |
10 "time" | |
11 | |
12 "github.com/maruel/ut" | |
13 ) | |
14 | |
15 func TestDoOnce(t *testing.T) { | |
16 c := &Config{1, 0, 0, 0} | |
17 r := &retriable{} | |
18 ut.AssertEqual(t, nil, c.Do(r)) | |
19 ut.AssertEqual(t, 1, r.closed) | |
20 ut.AssertEqual(t, 1, r.tries) | |
21 } | |
22 | |
23 func TestDoRetryExceeded(t *testing.T) { | |
24 c := &Config{1, 0, 0, 0} | |
25 r := &retriable{errs: []error{errRetry}} | |
26 ut.AssertEqual(t, errRetry, c.Do(r)) | |
27 ut.AssertEqual(t, 1, r.closed) | |
28 ut.AssertEqual(t, 1, r.tries) | |
29 } | |
30 | |
31 func TestDoRetry(t *testing.T) { | |
32 c := &Config{2, 0, time.Millisecond, 0} | |
33 r := &retriable{errs: []error{errRetry}} | |
34 ut.AssertEqual(t, nil, c.Do(r)) | |
35 ut.AssertEqual(t, 1, r.closed) | |
36 ut.AssertEqual(t, 2, r.tries) | |
37 } | |
38 | |
39 func TestError(t *testing.T) { | |
40 ut.AssertEqual(t, "please try again", errRetry.Error()) | |
41 } | |
42 | |
43 // Private details. | |
44 | |
45 var errYo = errors.New("yo") | |
46 var errRetry = Error{errors.New("please try again")} | |
47 | |
48 type retriable struct { | |
49 closed int | |
50 tries int | |
51 errs []error | |
52 } | |
53 | |
54 func (r *retriable) Close() error { | |
55 r.closed++ | |
56 if r.closed > 1 { | |
57 return errYo | |
58 } | |
59 return nil | |
60 } | |
61 | |
62 func (r *retriable) Do() error { | |
63 r.tries++ | |
64 if len(r.errs) != 0 { | |
65 err := r.errs[0] | |
66 r.errs = r.errs[1:] | |
67 return err | |
68 } | |
69 return nil | |
70 } | |
OLD | NEW |