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.

75 lines
1.7 KiB

8 years ago
8 years ago
  1. // nolint: goimports
  2. package autofile
  3. import (
  4. "os"
  5. "sync/atomic"
  6. "syscall"
  7. "testing"
  8. "time"
  9. . "github.com/tendermint/tmlibs/common"
  10. )
  11. func TestSIGHUP(t *testing.T) {
  12. // First, create an AutoFile writing to a tempfile dir
  13. file, name := Tempfile("sighup_test")
  14. err := file.Close()
  15. if err != nil {
  16. t.Fatalf("Error creating tempfile: %v", err)
  17. }
  18. // Here is the actual AutoFile
  19. af, err := OpenAutoFile(name)
  20. if err != nil {
  21. t.Fatalf("Error creating autofile: %v", err)
  22. }
  23. // Write to the file.
  24. _, err = af.Write([]byte("Line 1\n"))
  25. if err != nil {
  26. t.Fatalf("Error writing to autofile: %v", err)
  27. }
  28. _, err = af.Write([]byte("Line 2\n"))
  29. if err != nil {
  30. t.Fatalf("Error writing to autofile: %v", err)
  31. }
  32. // Move the file over
  33. err = os.Rename(name, name+"_old")
  34. if err != nil {
  35. t.Fatalf("Error moving autofile: %v", err)
  36. }
  37. // Send SIGHUP to self.
  38. oldSighupCounter := atomic.LoadInt32(&sighupCounter)
  39. syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
  40. // Wait a bit... signals are not handled synchronously.
  41. for atomic.LoadInt32(&sighupCounter) == oldSighupCounter {
  42. time.Sleep(time.Millisecond * 10)
  43. }
  44. // Write more to the file.
  45. _, err = af.Write([]byte("Line 3\n"))
  46. if err != nil {
  47. t.Fatalf("Error writing to autofile: %v", err)
  48. }
  49. _, err = af.Write([]byte("Line 4\n"))
  50. if err != nil {
  51. t.Fatalf("Error writing to autofile: %v", err)
  52. }
  53. err = af.Close()
  54. if err != nil {
  55. t.Fatalf("Error closing autofile")
  56. }
  57. // Both files should exist
  58. if body := MustReadFile(name + "_old"); string(body) != "Line 1\nLine 2\n" {
  59. t.Errorf("Unexpected body %s", body)
  60. }
  61. if body := MustReadFile(name); string(body) != "Line 3\nLine 4\n" {
  62. t.Errorf("Unexpected body %s", body)
  63. }
  64. }