timeutil_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package timeutil
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestDurationOffsetStrings(t *testing.T) {
  7. dur, off := "", ""
  8. dur, off = DurationOffsetStrings(0, 0)
  9. if dur != "" || off != "" {
  10. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "", "", dur, off)
  11. }
  12. dur, off = DurationOffsetStrings(24*time.Hour, 0)
  13. if dur != "1d" || off != "" {
  14. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "1d", "", dur, off)
  15. }
  16. dur, off = DurationOffsetStrings(24*time.Hour+5*time.Minute, 0)
  17. if dur != "1445m" || off != "" {
  18. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "1445m", "", dur, off)
  19. }
  20. dur, off = DurationOffsetStrings(25*time.Hour, 5*time.Minute)
  21. if dur != "25h" || off != "5m" {
  22. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "25h", "5m", dur, off)
  23. }
  24. dur, off = DurationOffsetStrings(25*time.Hour, 60*time.Minute)
  25. if dur != "25h" || off != "1h" {
  26. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "25h", "1h", dur, off)
  27. }
  28. dur, off = DurationOffsetStrings(72*time.Hour, 1440*time.Minute)
  29. if dur != "3d" || off != "1d" {
  30. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "3d", "1d", dur, off)
  31. }
  32. dur, off = DurationOffsetStrings(25*time.Hour, 1*time.Second)
  33. if dur != "25h" || off != "1s" {
  34. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "25h", "1s", dur, off)
  35. }
  36. dur, off = DurationOffsetStrings(24*time.Hour+time.Second, 1*time.Second)
  37. if dur != "86401s" || off != "1s" {
  38. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "86401s", "1s", dur, off)
  39. }
  40. // Expect empty strings if durations are negative
  41. dur, off = DurationOffsetStrings(-25*time.Hour, -1*time.Second)
  42. if dur != "" || off != "" {
  43. t.Fatalf("DurationOffsetStrings: exp (%s %s); act (%s, %s)", "", "", dur, off)
  44. }
  45. }
  46. func TestParseDuration(t *testing.T) {
  47. testCases := map[string]struct {
  48. input string
  49. expected time.Duration
  50. } {
  51. "expected" : {
  52. input: "3h",
  53. expected: time.Hour * 3,
  54. },
  55. "white space" : {
  56. input: " 4s ",
  57. expected: time.Second * 4,
  58. },
  59. "prom prefix" : {
  60. input: "offset 3m",
  61. expected: time.Minute * 3,
  62. },
  63. "prom prefix white space" : {
  64. input: " offset 3d ",
  65. expected: 24.0 * time.Hour * 3,
  66. },
  67. "zero" : {
  68. input: "0h",
  69. expected: time.Duration(0),
  70. },
  71. "empty" : {
  72. input: "",
  73. expected: time.Duration(0),
  74. },
  75. "bad string" : {
  76. input: "oqwd3dk5hk",
  77. expected: time.Duration(0),
  78. },
  79. "digit" : {
  80. input: "3",
  81. expected: time.Duration(0),
  82. },
  83. "unit" : {
  84. input: "h",
  85. expected: time.Duration(0),
  86. },
  87. }
  88. for name, test := range testCases {
  89. t.Run(name, func(t *testing.T) {
  90. dur, _ := ParseDuration(test.input)
  91. if dur != test.expected {
  92. t.Errorf("Expected duration %v did not match result %v", test.expected, dur)
  93. }
  94. })
  95. }
  96. }