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

worker/terminationworker: fix test hang #5528

Merged
merged 2 commits into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions worker/terminationworker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ type terminationWorker struct {
// TerminationSignal signal, and then exits
// with worker.ErrTerminateAgent.
func NewWorker() worker.Worker {
u := &terminationWorker{}
var w terminationWorker
c := make(chan os.Signal, 1)
signal.Notify(c, TerminationSignal)
go func() {
defer u.tomb.Done()
u.tomb.Kill(u.loop())
defer w.tomb.Done()
defer signal.Stop(c)
w.tomb.Kill(w.loop(c))
}()
return u
return &w
}

func (u *terminationWorker) Kill() {
u.tomb.Kill(nil)
func (w *terminationWorker) Kill() {
w.tomb.Kill(nil)
}

func (u *terminationWorker) Wait() error {
return u.tomb.Wait()
func (w *terminationWorker) Wait() error {
return w.tomb.Wait()
}

func (u *terminationWorker) loop() (err error) {
c := make(chan os.Signal, 1)
signal.Notify(c, TerminationSignal)
defer signal.Stop(c)
func (w *terminationWorker) loop(c <-chan os.Signal) (err error) {
select {
case <-c:
return worker.ErrTerminateAgent
case <-u.tomb.Dying():
case <-w.tomb.Dying():
return tomb.ErrDying
}
}
19 changes: 2 additions & 17 deletions worker/terminationworker/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,28 @@ package terminationworker_test

import (
"os"
"os/signal"
"runtime"
stdtesting "testing"
"testing"

jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"

"github.com/juju/juju/testing"
"github.com/juju/juju/worker"
"github.com/juju/juju/worker/terminationworker"
)

func TestPackage(t *stdtesting.T) {
func TestPackage(t *testing.T) {
gc.TestingT(t)
}

var _ = gc.Suite(&TerminationWorkerSuite{})

type TerminationWorkerSuite struct {
testing.BaseSuite
// c is a channel that will wait for the termination
// signal, to prevent signals terminating the process.
c chan os.Signal
}

func (s *TerminationWorkerSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
s.c = make(chan os.Signal, 1)
signal.Notify(s.c, terminationworker.TerminationSignal)
}

func (s *TerminationWorkerSuite) TearDownTest(c *gc.C) {
signal.Stop(s.c)
close(s.c)
s.BaseSuite.TearDownTest(c)
}

func (s *TerminationWorkerSuite) TestStartStop(c *gc.C) {
w := terminationworker.NewWorker()
w.Kill()
Expand Down