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.

64 lines
1.4 KiB

  1. package common
  2. import (
  3. "os"
  4. "syscall"
  5. "testing"
  6. )
  7. func TestSIGHUP(t *testing.T) {
  8. // First, create an AutoFile writing to a tempfile dir
  9. file, name := Tempfile("sighup_test")
  10. err := file.Close()
  11. if err != nil {
  12. t.Fatalf("Error creating tempfile: %v", err)
  13. }
  14. // Here is the actual AutoFile
  15. af, err := OpenAutoFile(name)
  16. if err != nil {
  17. t.Fatalf("Error creating autofile: %v", err)
  18. }
  19. // Write to the file.
  20. _, err = af.Write([]byte("Line 1\n"))
  21. if err != nil {
  22. t.Fatalf("Error writing to autofile: %v", err)
  23. }
  24. _, err = af.Write([]byte("Line 2\n"))
  25. if err != nil {
  26. t.Fatalf("Error writing to autofile: %v", err)
  27. }
  28. // Send SIGHUP to self.
  29. syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
  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. // Write more to the file.
  36. _, err = af.Write([]byte("Line 3\n"))
  37. if err != nil {
  38. t.Fatalf("Error writing to autofile: %v", err)
  39. }
  40. _, err = af.Write([]byte("Line 4\n"))
  41. if err != nil {
  42. t.Fatalf("Error writing to autofile: %v", err)
  43. }
  44. err = af.Close()
  45. if err != nil {
  46. t.Fatalf("Error closing autofile")
  47. }
  48. // Both files should exist
  49. if body := MustReadFile(name + "_old"); string(body) != "Line 1\nLine 2\n" {
  50. t.Errorf("Unexpected body %s", body)
  51. }
  52. if body := MustReadFile(name); string(body) != "Line 3\nLine 4\n" {
  53. t.Errorf("Unexpected body %s", body)
  54. }
  55. }