Skip to content

Commit

Permalink
Add some timeouts
Browse files Browse the repository at this point in the history
- Test suite get 60s default timeout
 - main command context get 10s timeout

Signed-off-by: Zander Mackie <[email protected]>
  • Loading branch information
zmackie committed Mar 30, 2020
1 parent a79ed03 commit 4841580
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PACKAGE_BASE=github.com/buildpacks/pack
PACKAGES:=$(shell $(GOCMD) list ./... | grep -v /testdata/)
SRC:=$(shell find . -type f -name '*.go' -not -path "*/vendor/*")
ARCHIVE_NAME=pack-$(PACK_VERSION)
TEST_TIMEOUT?=0
TEST_TIMEOUT?=60s
UNIT_TIMEOUT?=$(TEST_TIMEOUT)
ACCEPTANCE_TIMEOUT?=$(TEST_TIMEOUT)

Expand Down
3 changes: 2 additions & 1 deletion cmd/pack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"os"
"time"

"github.com/heroku/color"
"github.com/pkg/errors"
Expand Down Expand Up @@ -79,7 +80,7 @@ func main() {
rootCmd.Version = cmd.Version
rootCmd.SetVersionTemplate(`{{.Version}}{{"\n"}}`)

ctx := commands.CreateCancellableContext()
ctx := commands.CreateTimeoutContext(10 * time.Second)
if err := rootCmd.ExecuteContext(ctx); err != nil {
if commands.IsSoftError(err) {
os.Exit(2)
Expand Down
5 changes: 3 additions & 2 deletions internal/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"os/signal"
"syscall"
"time"

"github.com/spf13/cobra"

Expand All @@ -28,10 +29,10 @@ func AddHelpFlag(cmd *cobra.Command, commandName string) {
cmd.Flags().BoolP("help", "h", false, fmt.Sprintf("Help for '%s'", commandName))
}

func CreateCancellableContext() context.Context {
func CreateTimeoutContext(timeout time.Duration) context.Context {
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), timeout)

go func() {
<-signals
Expand Down

0 comments on commit 4841580

Please sign in to comment.