Skip to content

Commit

Permalink
Merge pull request #917 from dgageot/test-ctrlc
Browse files Browse the repository at this point in the history
Add test for signal handling
  • Loading branch information
dgageot authored Aug 22, 2018
2 parents 6581755 + 0b3e93b commit 47865e3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
13 changes: 0 additions & 13 deletions cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ package cmd
import (
"context"
"io"
"os"
"os/signal"
"syscall"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
"github.com/pkg/errors"
Expand Down Expand Up @@ -76,13 +73,3 @@ func dev(out io.Writer) error {
}
}
}

func catchCtrlC(cancel context.CancelFunc) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGPIPE)

go func() {
<-signals
cancel()
}()
}
38 changes: 38 additions & 0 deletions cmd/skaffold/app/cmd/signals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2018 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"context"
"os"
"os/signal"
"syscall"
)

func catchCtrlC(cancel context.CancelFunc) {
signals := make(chan os.Signal, 1)
signal.Notify(signals,
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGPIPE,
)

go func() {
<-signals
cancel()
}()
}
43 changes: 43 additions & 0 deletions cmd/skaffold/app/cmd/signals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// +build !windows

/*
Copyright 2018 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"context"
"sync"
"syscall"
"testing"
)

func TestCatchCtrlC(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)

ctx, cancel := context.WithCancel(context.Background())
catchCtrlC(cancel)

go func() {
<-ctx.Done()
wg.Done()
}()

syscall.Kill(syscall.Getpid(), syscall.SIGINT)

wg.Wait()
}

0 comments on commit 47865e3

Please sign in to comment.