Created
October 22, 2016 16:58
-
-
Save jtopjian/2fff393a232a76000dabc732932ef27d to your computer and use it in GitHub Desktop.
WaitFor Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=== RUN TestServersCreateDestroy | |
2016/10/22 16:53:53 Checkpoint 2 | |
2016/10/22 16:53:55 Checkpoint 2 | |
2016/10/22 16:53:57 Checkpoint 2 | |
2016/10/22 16:53:59 Checkpoint 2 | |
2016/10/22 16:54:00 Checkpoint 2 | |
2016/10/22 16:54:02 Checkpoint 2 | |
2016/10/22 16:54:03 Checkpoint 2 | |
2016/10/22 16:54:05 Checkpoint 2 | |
2016/10/22 16:54:07 Checkpoint 2 | |
--- PASS: TestServersCreateDestroy (19.53s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func WaitFor(timeout int, predicate func() (bool, error)) error { | |
// Check condition before first wait | |
satisfied, err := predicate() | |
if err != nil { | |
return err | |
} | |
if satisfied { | |
return nil | |
} | |
for { | |
select { | |
case <-time.After(time.Duration(timeout) * time.Second): | |
// Check if condition was satisfied between | |
// timeout and the last check | |
log.Printf("Checkpoint 1") | |
satisfied, err := predicate() | |
if err != nil { | |
return err | |
} | |
if satisfied { | |
return nil | |
} | |
return errors.New("A timeout occurred") | |
case <-time.After(time.Second): | |
// Execute the function | |
log.Printf("Checkpoint 2") | |
satisfied, err := predicate() | |
if err != nil { | |
return err | |
} | |
if satisfied { | |
return nil | |
} | |
} | |
} | |
} | |
func WaitForComputeStatus(client *gophercloud.ServiceClient, server *servers.Server, status string) error { | |
return gophercloud.WaitFor(2, func() (bool, error) { | |
latest, err := servers.Get(client, server.ID).Extract() | |
if err != nil { | |
return false, err | |
} | |
if latest.Status == status { | |
// Success! | |
return true, nil | |
} | |
if latest.Status == "ERROR" { | |
return false, fmt.Errorf("Instance in ERROR state") | |
} | |
return false, nil | |
}) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment