retry_test.go 989 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // This file is based on code that is (c) 2014 Cenk Altı and governed
  5. // by the MIT license.
  6. // See https://github.com/cenkalti/backoff for original source.
  7. package elastic
  8. import (
  9. "errors"
  10. "testing"
  11. "time"
  12. )
  13. func TestRetry(t *testing.T) {
  14. const successOn = 3
  15. var i = 0
  16. // This function is successfull on "successOn" calls.
  17. f := func() error {
  18. i++
  19. // t.Logf("function is called %d. time\n", i)
  20. if i == successOn {
  21. // t.Log("OK")
  22. return nil
  23. }
  24. // t.Log("error")
  25. return errors.New("error")
  26. }
  27. min := time.Duration(8) * time.Millisecond
  28. max := time.Duration(256) * time.Millisecond
  29. err := Retry(f, NewExponentialBackoff(min, max))
  30. if err != nil {
  31. t.Errorf("unexpected error: %s", err.Error())
  32. }
  33. if i != successOn {
  34. t.Errorf("invalid number of retries: %d", i)
  35. }
  36. }