You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

  1. // +build !windows
  2. package svc
  3. import (
  4. "os"
  5. "syscall"
  6. "testing"
  7. "github.com/judwhite/go-svc/svc/internal/test"
  8. )
  9. func TestDefaultSignalHandling(t *testing.T) {
  10. signals := []os.Signal{syscall.SIGINT, syscall.SIGTERM} // default signals handled
  11. for _, signal := range signals {
  12. testSignalNotify(t, signal)
  13. }
  14. }
  15. func TestUserDefinedSignalHandling(t *testing.T) {
  16. signals := []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP}
  17. for _, signal := range signals {
  18. testSignalNotify(t, signal, signals...)
  19. }
  20. }
  21. func testSignalNotify(t *testing.T, signal os.Signal, sig ...os.Signal) {
  22. // arrange
  23. // sigChan is the chan we'll send to here. if a signal matches a registered signal
  24. // type in the Run function (in svc_other.go) the signal will be delegated to the
  25. // channel passed to signalNotify, which is created in the Run function in svc_other.go.
  26. // shortly: we send here and the Run function gets it if it matches the filter.
  27. sigChan := make(chan os.Signal)
  28. var startCalled, stopCalled, initCalled int
  29. prg := makeProgram(&startCalled, &stopCalled, &initCalled)
  30. signalNotify = func(c chan<- os.Signal, sig ...os.Signal) {
  31. if c == nil {
  32. panic("os/signal: Notify using nil channel")
  33. }
  34. go func() {
  35. for val := range sigChan {
  36. for _, registeredSig := range sig {
  37. if val == registeredSig {
  38. c <- val
  39. }
  40. }
  41. }
  42. }()
  43. }
  44. go func() {
  45. sigChan <- signal
  46. }()
  47. // act
  48. if err := Run(prg, sig...); err != nil {
  49. t.Fatal(err)
  50. }
  51. // assert
  52. test.Equal(t, 1, startCalled)
  53. test.Equal(t, 1, stopCalled)
  54. test.Equal(t, 1, initCalled)
  55. }