summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorKevin Krakauer <krakauer@google.com>2020-07-30 11:27:47 -0700
committergVisor bot <gvisor-bot@google.com>2020-07-30 11:29:24 -0700
commitbc8201d01bc98e9db4009d250454c5fe73c5d2b4 (patch)
treedddd1974d3d3f588e9b3da979ebb5fd4bd5bfe8b
parentc43305731e04ce8d4f2c9b2949c326abc9c2b77e (diff)
Have dockerutil.Wait* respect the context deadline
PiperOrigin-RevId: 324044634
-rw-r--r--pkg/test/dockerutil/container.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/pkg/test/dockerutil/container.go b/pkg/test/dockerutil/container.go
index 441173ec2..5a2157951 100644
--- a/pkg/test/dockerutil/container.go
+++ b/pkg/test/dockerutil/container.go
@@ -454,15 +454,19 @@ func (c *Container) Wait(ctx context.Context) error {
// WaitTimeout waits for the container to exit with a timeout.
func (c *Container) WaitTimeout(ctx context.Context, timeout time.Duration) error {
- timeoutChan := time.After(timeout)
+ ctx, cancel := context.WithTimeout(ctx, timeout)
+ defer cancel()
statusChan, errChan := c.client.ContainerWait(ctx, c.id, container.WaitConditionNotRunning)
select {
+ case <-ctx.Done():
+ if ctx.Err() == context.DeadlineExceeded {
+ return fmt.Errorf("container %s timed out after %v seconds", c.Name, timeout.Seconds())
+ }
+ return nil
case err := <-errChan:
return err
case <-statusChan:
return nil
- case <-timeoutChan:
- return fmt.Errorf("container %s timed out after %v seconds", c.Name, timeout.Seconds())
}
}
@@ -487,6 +491,12 @@ func (c *Container) WaitForOutputSubmatch(ctx context.Context, pattern string, t
}
for exp := time.Now().Add(timeout); time.Now().Before(exp); {
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ default:
+ }
+
c.streams.Conn.SetDeadline(time.Now().Add(50 * time.Millisecond))
_, err := stdcopy.StdCopy(&c.streamBuf, &c.streamBuf, c.streams.Reader)