Skip to content

Commit

Permalink
Add test for signal handling
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Aug 22, 2018
1 parent 6581755 commit 2ebf85d
Show file tree
Hide file tree
Showing 3 changed files with 79 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()
}()
}
41 changes: 41 additions & 0 deletions cmd/skaffold/app/cmd/signals_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
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 2ebf85d

Please sign in to comment.