Skip to content

Commit

Permalink
feat(impl.go): maximum time for wait actions
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhapz committed Jul 3, 2020
1 parent c470431 commit 910312c
Showing 1 changed file with 54 additions and 28 deletions.
82 changes: 54 additions & 28 deletions impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,66 +565,92 @@ func sleepAction(ra runtimeAction, act Action) interface{} {
}

ctx := ra.runner.P.GetContext()
asDuration := time.Duration(duration)
t := time.NewTimer(asDuration * time.Second)
asFloat := float64(time.Second) * duration
t := time.After(time.Duration(asFloat))

select {
case <-ctx.Done():
t.Stop()
return ctx.Err()
case <-t.C:
case <-t:
return nil
}
return nil
}

func waitIdleAction(ra runtimeAction, _ Action) interface{} {
run := ra.runner
done := make(chan bool)
ctx := run.P.GetContext()

go func() {
run.P.WaitRequestIdle()()
done <- true
}()

select {
case <-ctx.Done():
return ctx.Err()
case <-done:
return nil
}
return ra.waitWithTimeout(func() {
ra.runner.P.WaitRequestIdle()()
}, "waited too long for waitInvisible action to complete")
}

func waitInvisibleAction(ra runtimeAction, act Action) interface{} {
element, err := ra.createElem(act)
if err != nil {
return *err
}
element.WaitInvisible()
return nil

return ra.waitWithTimeout(func() {
element.WaitInvisible()
}, "waited too long for waitInvisible action to complete")
}

func waitLoadAction(ra runtimeAction, _ Action) interface{} {
ra.runner.P.WaitLoad()
return nil
return ra.waitWithTimeout(func() {
ra.runner.P.WaitLoad()
}, "waited too long for waitLoad action to complete")
}

func waitStableAction(ra runtimeAction, act Action) interface{} {
element, err := ra.createElem(act)
if err != nil {
return *err
}
element.WaitStable()
return nil

return ra.waitWithTimeout(func() {
element.WaitStable()
}, "waited too long for waitStable action to complete")

}

func waitVisibleAction(ra runtimeAction, act Action) interface{} {
element, err := ra.createElem(act)
if err != nil {
return *err
}
element.WaitVisible()
return nil

return ra.waitWithTimeout(func() {
element.WaitVisible()
}, "waited too long for waitVisible action to complete")
}

func (ra runtimeAction) waitWithTimeout(wait func(), timeoutMsg string) interface{} {
run := ra.runner

ctx := run.P.GetContext()

done := make(chan bool, 1)
go func() {
wait()
done <- true
}()

timeout := make(chan bool, 1)
duration, ok := ra.act["duration"].(float64)
if ok {
go func() {
asFloat := float64(time.Second) * duration
time.Sleep(time.Duration(asFloat))
timeout <- true
}()
}

select {
case <-ctx.Done():
return ra.err("context error:", ctx.Err())
case <-timeout:
return ra.err(timeoutMsg)
case <-done:
return nil
}
}

func (ra runtimeAction) err(errs ...interface{}) RuntimeError {
Expand Down

0 comments on commit 910312c

Please sign in to comment.