timeutil_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. errorExpected bool
  51. } {
  52. "expected" : {
  53. input: "3h",
  54. expected: time.Hour * 3,
  55. errorExpected: false,
  56. },
  57. "white space" : {
  58. input: " 4s ",
  59. expected: time.Second * 4,
  60. errorExpected: false,
  61. },
  62. "prom prefix" : {
  63. input: "offset 3m",
  64. expected: time.Minute * 3,
  65. errorExpected: false,
  66. },
  67. "prom prefix white space" : {
  68. input: " offset 3d ",
  69. expected: 24.0 * time.Hour * 3,
  70. errorExpected: false,
  71. },
  72. "empty" : {
  73. input: "",
  74. expected: time.Second,
  75. errorExpected: true,
  76. },
  77. "bad string" : {
  78. input: "oqwd3dk5hk",
  79. expected: time.Second,
  80. errorExpected: true,
  81. },
  82. "digit" : {
  83. input: "3",
  84. expected: time.Second,
  85. errorExpected: true,
  86. },
  87. "unit" : {
  88. input: "h",
  89. expected: time.Second,
  90. errorExpected: true,
  91. },
  92. }
  93. for name, test := range testCases {
  94. t.Run(name, func(t *testing.T) {
  95. dur, err := ParseDuration(test.input)
  96. if err != nil && test.errorExpected{
  97. return
  98. }
  99. if *dur != test.expected {
  100. t.Errorf("Expected duration %v did not match result %v", test.expected, dur)
  101. }
  102. })
  103. }
  104. }