| Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 1 | package gophercloud |
| 2 | |||||
| 3 | import ( | ||||
| 4 | "fmt" | ||||
| 5 | "time" | ||||
| 6 | ) | ||||
| 7 | |||||
| 8 | // WaitFor polls a predicate function once per second up to secs times to wait for a certain state to arrive. | ||||
| 9 | func WaitFor(secs int, predicate func() (bool, error)) error { | ||||
| 10 | for i := 0; i < secs; i++ { | ||||
| 11 | time.Sleep(1 * time.Second) | ||||
| 12 | |||||
| 13 | satisfied, err := predicate() | ||||
| 14 | if err != nil { | ||||
| 15 | return err | ||||
| 16 | } | ||||
| 17 | if satisfied { | ||||
| 18 | return nil | ||||
| 19 | } | ||||
| 20 | } | ||||
| 21 | return fmt.Errorf("Time out in WaitFor.") | ||||
| 22 | } | ||||