Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: export async config #78

Merged
merged 2 commits into from Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 11 additions & 11 deletions internal/async/wait.go
Expand Up @@ -15,9 +15,9 @@ type IntervalStrategy func() <-chan time.Time
// WaitSyncConfig defines the waiting options.
type WaitSyncConfig struct {
// This method will be called from another goroutine.
get func() (value interface{}, err error, isTerminal bool)
interval IntervalStrategy
timeout time.Duration
Get func() (value interface{}, err error, isTerminal bool)
IntervalStrategy IntervalStrategy
Timeout time.Duration
}

// LinearIntervalStrategy defines a linear interval duration.
Expand All @@ -40,11 +40,11 @@ func FibonacciIntervalStrategy(base time.Duration, factor float32) IntervalStrat
// WaitSync waits and returns when a given stop condition is true or if an error occurs.
func WaitSync(config *WaitSyncConfig) (terminalValue interface{}, err error) {
// initialize configuration
if config.interval == nil {
config.interval = LinearIntervalStrategy(defaultInterval)
if config.IntervalStrategy == nil {
config.IntervalStrategy = LinearIntervalStrategy(defaultInterval)
}
if config.timeout == 0 {
config.timeout = defaultTimeout
if config.Timeout == 0 {
config.Timeout = defaultTimeout
}

resultValue := make(chan interface{})
Expand All @@ -54,7 +54,7 @@ func WaitSync(config *WaitSyncConfig) (terminalValue interface{}, err error) {
go func() {
for {
// get the payload
value, err, stopCondition := config.get()
value, err, stopCondition := config.Get()

// send the payload
if err != nil {
Expand All @@ -70,7 +70,7 @@ func WaitSync(config *WaitSyncConfig) (terminalValue interface{}, err error) {
select {
case <-timeout:
return
case <-config.interval():
case <-config.IntervalStrategy():
// sleep
}
}
Expand All @@ -82,8 +82,8 @@ func WaitSync(config *WaitSyncConfig) (terminalValue interface{}, err error) {
return val, nil
case err := <-resultErr:
return nil, err
case <-time.After(config.timeout):
case <-time.After(config.Timeout):
timeout <- true
return nil, fmt.Errorf("timeout after %v", config.timeout)
return nil, fmt.Errorf("timeout after %v", config.Timeout)
}
}
24 changes: 12 additions & 12 deletions internal/async/wait_test.go
Expand Up @@ -46,7 +46,7 @@ func TestWaitSync(t *testing.T) {
{
name: "With default timeout and interval",
config: &WaitSyncConfig{
get: getMock(2, 0),
Get: getMock(2, 0),
},
expValue: &value{
doneIterations: 2,
Expand All @@ -56,8 +56,8 @@ func TestWaitSync(t *testing.T) {
{
name: "With useless timeout",
config: &WaitSyncConfig{
get: getMock(2, time.Second),
timeout: 4 * time.Second,
Get: getMock(2, time.Second),
Timeout: 4 * time.Second,
},
expValue: &value{
doneIterations: 2,
Expand All @@ -67,17 +67,17 @@ func TestWaitSync(t *testing.T) {
{
name: "Should timeout",
config: &WaitSyncConfig{
get: getMock(2, 2*time.Second),
timeout: time.Second,
Get: getMock(2, 2*time.Second),
Timeout: time.Second,
},
expValue: nil,
expErr: fmt.Errorf("timeout after 1s"),
},
{
name: "With interval",
config: &WaitSyncConfig{
get: getMock(2, 0),
interval: LinearIntervalStrategy(2 * time.Second),
Get: getMock(2, 0),
IntervalStrategy: LinearIntervalStrategy(2 * time.Second),
},
expValue: &value{
doneIterations: 2,
Expand All @@ -87,8 +87,8 @@ func TestWaitSync(t *testing.T) {
{
name: "With fibonacci interval",
config: &WaitSyncConfig{
get: getMock(5, 0),
interval: FibonacciIntervalStrategy(time.Second, 1),
Get: getMock(5, 0),
IntervalStrategy: FibonacciIntervalStrategy(time.Second, 1),
},
expValue: &value{
doneIterations: 5,
Expand All @@ -98,9 +98,9 @@ func TestWaitSync(t *testing.T) {
{
name: "Should timeout with interval",
config: &WaitSyncConfig{
get: getMock(2, time.Second),
timeout: 2 * time.Second,
interval: LinearIntervalStrategy(2 * time.Second),
Get: getMock(2, time.Second),
Timeout: 2 * time.Second,
IntervalStrategy: LinearIntervalStrategy(2 * time.Second),
},
expValue: nil,
expErr: fmt.Errorf("timeout after 2s"),
Expand Down