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.

76 lines
1.8 KiB

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