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.

73 lines
1.7 KiB

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