launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	stdtesting "testing"
    10  
    11  	"launchpad.net/errgo/errors"
    12  	gc "launchpad.net/gocheck"
    13  
    14  	"launchpad.net/juju-core/testing/testbase"
    15  	"launchpad.net/juju-core/worker"
    16  	"launchpad.net/juju-core/worker/terminationworker"
    17  )
    18  
    19  func TestPackage(t *stdtesting.T) {
    20  	gc.TestingT(t)
    21  }
    22  
    23  var _ = gc.Suite(&TerminationWorkerSuite{})
    24  
    25  type TerminationWorkerSuite struct {
    26  	testbase.LoggingSuite
    27  	// c is a channel that will wait for the termination
    28  	// signal, to prevent signals terminating the process.
    29  	c chan os.Signal
    30  }
    31  
    32  func (s *TerminationWorkerSuite) SetUpTest(c *gc.C) {
    33  	s.LoggingSuite.SetUpTest(c)
    34  	s.c = make(chan os.Signal, 1)
    35  	signal.Notify(s.c, terminationworker.TerminationSignal)
    36  }
    37  
    38  func (s *TerminationWorkerSuite) TearDownTest(c *gc.C) {
    39  	close(s.c)
    40  	signal.Stop(s.c)
    41  	s.LoggingSuite.TearDownTest(c)
    42  }
    43  
    44  func (s *TerminationWorkerSuite) TestStartStop(c *gc.C) {
    45  	w := terminationworker.NewWorker()
    46  	w.Kill()
    47  	err := w.Wait()
    48  	c.Assert(err, gc.IsNil)
    49  }
    50  
    51  func (s *TerminationWorkerSuite) TestSignal(c *gc.C) {
    52  	w := terminationworker.NewWorker()
    53  	proc, err := os.FindProcess(os.Getpid())
    54  	c.Assert(err, gc.IsNil)
    55  	defer proc.Release()
    56  	err = proc.Signal(terminationworker.TerminationSignal)
    57  	c.Assert(err, gc.IsNil)
    58  	err = w.Wait()
    59  	c.Assert(errors.Cause(err), gc.Equals, worker.ErrTerminateAgent)
    60  }