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.

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