github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/worker/terminationworker/worker_test.go (about)

     1  // Copyright 2014 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package terminationworker_test
     5  
     6  import (
     7  	"os"
     8  	"os/signal"
     9  	"runtime"
    10  	stdtesting "testing"
    11  
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/testing"
    16  	"github.com/juju/juju/worker"
    17  	"github.com/juju/juju/worker/terminationworker"
    18  )
    19  
    20  func TestPackage(t *stdtesting.T) {
    21  	gc.TestingT(t)
    22  }
    23  
    24  var _ = gc.Suite(&TerminationWorkerSuite{})
    25  
    26  type TerminationWorkerSuite struct {
    27  	testing.BaseSuite
    28  	// c is a channel that will wait for the termination
    29  	// signal, to prevent signals terminating the process.
    30  	c chan os.Signal
    31  }
    32  
    33  func (s *TerminationWorkerSuite) SetUpTest(c *gc.C) {
    34  	s.BaseSuite.SetUpTest(c)
    35  	s.c = make(chan os.Signal, 1)
    36  	signal.Notify(s.c, terminationworker.TerminationSignal)
    37  }
    38  
    39  func (s *TerminationWorkerSuite) TearDownTest(c *gc.C) {
    40  	signal.Stop(s.c)
    41  	close(s.c)
    42  	s.BaseSuite.TearDownTest(c)
    43  }
    44  
    45  func (s *TerminationWorkerSuite) TestStartStop(c *gc.C) {
    46  	w := terminationworker.NewWorker()
    47  	w.Kill()
    48  	err := w.Wait()
    49  	c.Assert(err, jc.ErrorIsNil)
    50  }
    51  
    52  func (s *TerminationWorkerSuite) TestSignal(c *gc.C) {
    53  	//TODO(bogdanteleaga): Inspect this further on windows
    54  	if runtime.GOOS == "windows" {
    55  		c.Skip("bug 1403084: sending this signal is not supported on windows")
    56  	}
    57  	w := terminationworker.NewWorker()
    58  	proc, err := os.FindProcess(os.Getpid())
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	defer proc.Release()
    61  	err = proc.Signal(terminationworker.TerminationSignal)
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	err = w.Wait()
    64  	c.Assert(err, gc.Equals, worker.ErrTerminateAgent)
    65  }