| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
// This file is based on code that is (c) 2014 Cenk Altı and governed |
| 6 |
// by the MIT license. |
| 7 |
// See https://github.com/cenkalti/backoff for original source. |
| 8 |
|
| 9 |
package elastic |
| 10 |
|
| 11 |
import ( |
| 12 |
"errors" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
) |
| 16 |
|
| 17 |
func TestRetry(t *testing.T) { |
| 18 |
const successOn = 3 |
| 19 |
var i = 0 |
| 20 |
|
| 21 |
// This function is successfull on "successOn" calls. |
| 22 |
f := func() error { |
| 23 |
i++ |
| 24 |
// t.Logf("function is called %d. time\n", i) |
| 25 |
|
| 26 |
if i == successOn { |
| 27 |
// t.Log("OK") |
| 28 |
return nil |
| 29 |
} |
| 30 |
|
| 31 |
// t.Log("error") |
| 32 |
return errors.New("error") |
| 33 |
} |
| 34 |
|
| 35 |
min := time.Duration(8) * time.Millisecond |
| 36 |
max := time.Duration(256) * time.Millisecond |
| 37 |
err := Retry(f, NewExponentialBackoff(min, max)) |
| 38 |
if err != nil { |
| 39 |
t.Errorf("unexpected error: %s", err.Error()) |
| 40 |
} |
| 41 |
if i != successOn { |
| 42 |
t.Errorf("invalid number of retries: %d", i) |
| 43 |
} |
| 44 |
} |