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

add StopCtx function, to stop containers with context #1094

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions gnomock.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,37 @@ func Stop(cs ...*Container) error {
container := c

eg.Go(func() error {
return g.stop(container)
return g.stop(context.Background(), container)
})
}

return eg.Wait()
}

func (g *g) stop(c *Container) error {
// StopCtx is the same as Stop, but it allows to provide a context to stop
// containers.
func StopCtx(ctx context.Context, cs ...*Container) error {
g, err := newG(isInDocker())
if err != nil {
return err
}

defer func() { _ = g.log.Sync() }()

eg, eCtx := errgroup.WithContext(ctx)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we don't need two copies of the same function. Instead, we need the Stop function to call StopCtx with the background context, and StopCtx should be the only function that has all the errgroup code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've checked that github.com/docker/docker/client.ContainerStop and github.com/docker/docker/client.ContainerRemove both take the same context. Though, I didn't this behavior specifically.


for _, c := range cs {
container := c

eg.Go(func() error {
return g.stop(eCtx, container)
})
}

return eg.Wait()
}

func (g *g) stop(ctx context.Context, c *Container) error {
if c == nil {
return nil
}
Expand All @@ -244,12 +267,12 @@ func (g *g) stop(c *Container) error {
// stop sidecar container when the main one is requested to stop;
// error in this case won't matter, the container has a self-destruct
// timer
_ = cli.stopContainer(context.Background(), sidecar)
_ = cli.stopContainer(ctx, sidecar)
_ = cli.stopClient()
}()
}

err = cli.stopContainer(context.Background(), id)
err = cli.stopContainer(ctx, id)
if err != nil {
return fmt.Errorf("can't stop container: %w", err)
}
Expand All @@ -261,7 +284,7 @@ func (g *g) stop(c *Container) error {
}
}

return cli.removeContainer(context.Background(), id)
return cli.removeContainer(ctx, id)
}

func buildImage(image string) string {
Expand Down
Loading