2017-01-17 09:24:29 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2017-03-27 03:55:37 +00:00
|
|
|
"errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-01-17 09:24:29 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-03-27 03:55:37 +00:00
|
|
|
func TestWaitForSpecificOrError(t *testing.T) {
|
2017-01-17 09:24:29 +00:00
|
|
|
|
2017-03-27 03:55:37 +00:00
|
|
|
waitInterval := 100 * time.Millisecond
|
|
|
|
timeout := 10*waitInterval + waitInterval/2
|
2017-01-17 09:24:29 +00:00
|
|
|
times := 0
|
|
|
|
err := WaitForSpecificOrError(func() (bool, error) {
|
2017-03-27 03:55:37 +00:00
|
|
|
times++
|
2017-01-17 09:24:29 +00:00
|
|
|
println("times", times)
|
|
|
|
if times == 3 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}, timeout, waitInterval)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 3, times)
|
|
|
|
|
|
|
|
times = 0
|
|
|
|
err = WaitForSpecificOrError(func() (bool, error) {
|
2017-03-27 03:55:37 +00:00
|
|
|
times++
|
2017-01-17 09:24:29 +00:00
|
|
|
println("times", times)
|
|
|
|
if times == 3 {
|
|
|
|
return false, errors.New("error")
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}, timeout, waitInterval)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, 3, times)
|
|
|
|
|
|
|
|
times = 0
|
|
|
|
err = WaitForSpecificOrError(func() (bool, error) {
|
2017-03-27 03:55:37 +00:00
|
|
|
times++
|
2017-01-17 09:24:29 +00:00
|
|
|
println("times", times)
|
|
|
|
return false, nil
|
|
|
|
}, timeout, waitInterval)
|
|
|
|
assert.Error(t, err)
|
2017-07-12 10:05:43 +00:00
|
|
|
tErr, ok := err.(*TimeoutError)
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, timeout, tErr.timeout)
|
2017-01-17 09:24:29 +00:00
|
|
|
assert.Equal(t, 10, times)
|
2017-03-27 03:55:37 +00:00
|
|
|
}
|