heartbeat_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package heartbeat
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. )
  8. func TestTouchAndAge(t *testing.T) {
  9. dir := t.TempDir()
  10. path := filepath.Join(dir, "hb")
  11. now := time.Unix(1_000_000, 0)
  12. if err := Touch(path, now); err != nil {
  13. t.Fatalf("Touch: %v", err)
  14. }
  15. age, ok := Age(path, now.Add(17*time.Second))
  16. if !ok || age != 17*time.Second {
  17. t.Fatalf("Age = %v, %v; quiero 17s, true", age, ok)
  18. }
  19. }
  20. func TestAgeSinFichero(t *testing.T) {
  21. age, ok := Age(filepath.Join(t.TempDir(), "noexiste"), time.Now())
  22. if ok || age != 0 {
  23. t.Fatalf("Age(sin fichero) = %v, %v; quiero 0, false", age, ok)
  24. }
  25. }
  26. func TestAgeFicheroCorrupto(t *testing.T) {
  27. path := filepath.Join(t.TempDir(), "hb")
  28. if err := os.WriteFile(path, []byte("no-es-un-ts\n"), 0o644); err != nil {
  29. t.Fatal(err)
  30. }
  31. if _, ok := Age(path, time.Now()); ok {
  32. t.Fatal("Age con contenido no numérico debe devolver ok=false")
  33. }
  34. }
  35. func TestTouchCreaDirectorio(t *testing.T) {
  36. path := filepath.Join(t.TempDir(), "sub", "dir", "hb")
  37. if err := Touch(path, time.Unix(42, 0)); err != nil {
  38. t.Fatalf("Touch con dirs inexistentes: %v", err)
  39. }
  40. }